Vikas01 commited on
Commit
3c2f163
·
1 Parent(s): 9f93bc6

Create app.py

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