Vikas01 commited on
Commit
457e283
·
1 Parent(s): 2d0ebcd

Update app.py

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