Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,77 +12,73 @@ bytes_data=None
|
|
12 |
|
13 |
img_file_buffer=st.camera_input("Take a picture")
|
14 |
if img_file_buffer is not None:
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
image = np.asarray(test_image)
|
19 |
|
20 |
-
|
21 |
|
22 |
-
|
23 |
|
24 |
-
|
25 |
#cv2.imshow("1 - Grayed image", gray)
|
26 |
|
27 |
-
|
28 |
#cv2.imshow("2 - Smoothened image", gray)
|
29 |
|
30 |
-
|
31 |
# cv2.imshow("3 - Edged image", edged)
|
32 |
|
33 |
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
# cv2.imshow("countours",image1)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
cv2.imwrite('./'+str(i)+'.png',new_img)
|
58 |
-
i+=1
|
59 |
-
break
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
Cropped_loc = './1.png'
|
65 |
-
cv2.imshow("cropped", cv2.imread(Cropped_loc))
|
66 |
|
67 |
|
68 |
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
|
73 |
-
|
74 |
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
|
79 |
|
80 |
-
|
81 |
-
|
82 |
|
83 |
|
84 |
-
|
85 |
-
|
86 |
|
87 |
The header=False parameter prevents the column headers from being written repeatedly when appending entries.
|
88 |
|
@@ -91,7 +87,7 @@ if img_file_buffer is not None:
|
|
91 |
|
92 |
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
12 |
|
13 |
img_file_buffer=st.camera_input("Take a picture")
|
14 |
if img_file_buffer is not None:
|
15 |
+
test_image = Image.open(img_file_buffer)
|
16 |
+
st.image(test_image, use_column_width=True)
|
17 |
+
image = np.asarray(test_image)
|
|
|
18 |
|
19 |
+
image = imutils.resize(image, width=500)
|
20 |
|
21 |
+
cv2.imshow("Original Image", image)
|
22 |
|
23 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
24 |
#cv2.imshow("1 - Grayed image", gray)
|
25 |
|
26 |
+
gray = cv2.bilateralFilter(gray, 11, 17, 17)
|
27 |
#cv2.imshow("2 - Smoothened image", gray)
|
28 |
|
29 |
+
edged = cv2.Canny(gray, 170, 200)
|
30 |
# cv2.imshow("3 - Edged image", edged)
|
31 |
|
32 |
|
33 |
|
34 |
+
cnts,new=cv2.findContours(edged.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
|
35 |
+
image1=image.copy()
|
36 |
+
cv2.drawContours(image1,cnts,-1,(0,255,0),3)
|
37 |
# cv2.imshow("countours",image1)
|
38 |
|
39 |
+
cnts=sorted(cnts,key=cv2.contourArea,reverse=True)[:30]
|
40 |
+
screenCnt=None
|
41 |
+
image2=image.copy()
|
42 |
+
cv2.drawContours(image2,cnts,-1,(0,0,255),3)
|
43 |
+
# cv2.imshow("Top 30 contours",image2)
|
44 |
+
|
45 |
+
i=1
|
46 |
+
for c in cnts:
|
47 |
+
perimeter = cv2.arcLength(c, True)
|
48 |
+
approx = cv2.approxPolyDP(c, 0.018 * perimeter, True)
|
49 |
+
if len(approx) == 4:
|
50 |
+
screenCnt = approx
|
51 |
+
x,y,w,h = cv2.boundingRect(c)
|
52 |
+
new_img=image[y:y+h,x:x+w]
|
53 |
+
cv2.imwrite('./'+str(i)+'.png',new_img)
|
54 |
+
i+=1
|
55 |
+
break
|
56 |
|
57 |
+
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
|
58 |
+
# cv2.imshow("image with detected license plate", image)
|
|
|
|
|
|
|
59 |
|
60 |
+
Cropped_loc = './1.png'
|
61 |
+
cv2.imshow("cropped", cv2.imread(Cropped_loc))
|
|
|
|
|
|
|
62 |
|
63 |
|
64 |
|
65 |
+
# Configuration for tesseract
|
66 |
+
pytesseract.pytesseract.tesseract_cmd=r"tessract1\tesseract.exe"
|
67 |
|
68 |
+
# Run tesseract OCR on image
|
69 |
+
text = pytesseract.image_to_string(Cropped_loc,lang="eng")
|
70 |
|
71 |
+
#Data is stored in CSV file
|
72 |
+
raw_data = {'date': [time.asctime( time.localtime(time.time()) )],'v_number': [text] }
|
73 |
|
74 |
+
df = pd.DataFrame(raw_data, columns = ['date', 'v_number'])
|
75 |
|
76 |
+
file_path = 'data.csv'
|
77 |
+
header = not os.path.exists(file_path) # Check if the file exists and set header flag accordingly
|
78 |
|
79 |
|
80 |
+
df.to_csv('data.csv',mode='a',header=header,index=False)
|
81 |
+
"""the mode='a' parameter ensures that each new entry is appended to the existing CSV file instead of replacing it.
|
82 |
|
83 |
The header=False parameter prevents the column headers from being written repeatedly when appending entries.
|
84 |
|
|
|
87 |
|
88 |
|
89 |
|
90 |
+
# Print recognized text
|
91 |
+
st.write(text)
|
92 |
+
if bytes_data is None:
|
93 |
+
st.stop()
|