Vikas01 commited on
Commit
d11be0b
·
1 Parent(s): 7a3c4a0

Update app.py

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