Vikas01 commited on
Commit
655fdba
·
1 Parent(s): 6775020

Update app.py

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