-
Notifications
You must be signed in to change notification settings - Fork 3
/
DetectNumberPlate.py
181 lines (177 loc) · 7.64 KB
/
DetectNumberPlate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import cv2
import numpy as np
import pytesseract
#import argparse
#import os
#import urllib
from PIL import Image
#from google.cloud import vision
#from google.cloud.vision import types
import stringOCR
import FrameDivide
import os
import MySQLdb
import sys
import zulip
import zulipcall
v=sys.argv[1]
u=sys.argv[2]
match=FrameDivide.frame()
number=str(v)
number=str(number)
s=[]
for i in range(len(number)+1):
if i>len(number)-2:
s.append(number[:i])
print(s)
frto=0
if(match==True):
i=0
doper=0
while(True):
try:
img= cv2.imread('frame'+str(i)+".jpg")
print(i)
#cv2.namedWindow('Original Image',cv2.WINDOW_NORMAL)
#cv2.imshow('Original Image',img)
#print("Check1")
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#cv2.namedWindow('Gray Converted Image',cv2.WINDOW_NORMAL)
#cv2.imshow('Gray converted Image',img_gray)
#print("Check2")
noise_removal=cv2.bilateralFilter(img_gray,9,75,75)
#cv2.namedWindow('Noise Removed image',cv2.WINDOW_NORMAL)
#cv2.imshow('Noise removed image',noise_removal)
##print("Check3")
equal_histogram = cv2.equalizeHist(noise_removal)
#cv2.namedWindow('After histogram ',cv2.WINDOW_NORMAL)
#cv2.imshow('After histogram',equal_histogram)
#print("Check4")
kernel= cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
morph_image=cv2.morphologyEx(equal_histogram,cv2.MORPH_OPEN,kernel,iterations=15)
#cv2.namedWindow("Morphological opening", cv2.WINDOW_NORMAL)
#cv2.imshow("Morphological opening",morph_image)
#print("Check5")
sub_morph_image=cv2.subtract(equal_histogram,morph_image)
#cv2.namedWindow("Subtraction image",cv2.WINDOW_NORMAL)
#cv2.imshow("Subtract image",sub_morph_image)
#print("Check6")
ret,thresh_image=cv2.threshold(sub_morph_image,0,255,cv2.THRESH_OTSU)
#cv2.namedWindow("Image after thresholding",cv2.WINDOW_NORMAL)
#cv2.imshow("Image after thresholding",thresh_image)
#print("Check7")
canny_image= cv2.Canny(thresh_image,250,255)
#cv2.namedWindow("Image after applying canny",cv2.WINDOW_NORMAL)
canny_image= cv2.convertScaleAbs(canny_image)
#print("Check8")
kernel=np.ones((3,3),np.uint8)
dilated_image=cv2.dilate(canny_image,kernel,iterations=1)
#cv2.namedWindow("Dilation",cv2.WINDOW_NORMAL)
#cv2.imshow("Dilation",dilated_image)
#print("Check9")
new,contours,hierarchy =cv2.findContours(dilated_image,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours=sorted(contours,key =cv2.contourArea, reverse = True)[:10]
screenCnt = None
for c in contours:
peri = cv2.arcLength(c,True)
approx =cv2.approxPolyDP(c,0.06*peri,True)
if len(approx)==4:
screenCnt = approx
break
final=cv2.drawContours(img,[screenCnt],-1,(0,255,0),3)
#cv2.namedWindow("Image with selected contour",cv2.WINDOW_NORMAL)
#cv2.imshow("Image with selected contour",final)
#print("Check10")
mask=np.zeros(img_gray.shape,np.uint8)
new_image=cv2.drawContours(mask,[screenCnt],0,255,-1)
new_image=cv2.bitwise_and(img,img,mask=mask)
#cv2.namedWindow("Final Image,",cv2.WINDOW_NORMAL)
#cv2.imshow("Final_image",new_image)
#print("Check11")
y,cr,cb = cv2.split(cv2.cvtColor(new_image,cv2.COLOR_RGB2YCrCb))
y=cv2.equalizeHist(y)
final_image=cv2.cvtColor(cv2.merge([y,cr,cb]),cv2.COLOR_YCrCb2RGB)
#print("Check12")
#cv2.namedWindow("Enhanced Number Plate",cv2.WINDOW_NORMAL)
#cv2.imshow("Enhanced Number plate",final_image)
print("Check12")
cv2.imwrite('final.png',final_image)
try:
test_file,d =stringOCR.ocr_space_file(filename='final.png', language='pol')
print(d)
except:
print("Check")
i+=1
print("Check",i)
print(d)
print(s[1])
print(s[1] in d)
if s[0] in d or s[1] in d and frto!=1:
#os.system("Cars.mp4")
cv2.namedWindow("Enhanced Number Plate",cv2.WINDOW_NORMAL)
cv2.imshow("Enhanced Number plate",final_image)
print("Found")
msg="Your Vehicle Found with the number plate "
f=open("Speech1.vbs","w")
k="Dim sapi \nSet sapi=Createobject(\"sapi.spvoice\") \nsapi.Speak "+'"'+msg+'"'
f.write(k)
f.close()
os.system("Speech1.vbs")
msg="Your Vehicle with the number plate "+d+" Found at "+u+". Contact "+u+" Toll Tax Soon"
print("pat check")
print("pat check 1")
zulipcall.caller(msg)
break
break
else:
i+=3
print("Not Found")
if i>18:
msg="Scanning is active for now no there is no record of the car. We are searching for you so please be patient. We will contact you soon"
f=open("Speech1.vbs","w")
k="Dim sapi \nSet sapi=Createobject(\"sapi.spvoice\") \nsapi.Speak "+'"'+msg+'"'
f.write(k)
f.close()
os.system("Speech1.vbs")
zulipcall.caller(msg)
print("Message Sent")
db=MySQLdb.connect(host="localhost",user="root",password="amit",db="rajasthan")
cur=db.cursor()
num="'"+str(number)+"'"
query="update user set status='Waiting' where vehicle="+str(num);
print(query)
cur.execute(query)
db.commit()
db.close()
break
"""
client=vision.ImageAnnotatorClient()
with io.open(final_image,'rb') as image_file:
content=image_file.read()
image=types.Image(content=content)
response =client.text_detection(image=image)
texts=response.text_annotations
print('Texts:')
for text in texts:
print('\n"{}"'.format(text.description))
vetices =(['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
"""
"""ap=argparse.ArgumentParser()
ap.add_argument('-i',"--image",required=True, help="path to input image to be OCR'd")
ap.add_argument("-p","--preprocess", type=str,default="thresh",
help ="type of preprocessing to be done")
agrs =vars(ap.parse_args())
image=final_image
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
if args["preprocess"]=="thresh":
gray = cv2.threshold(gray,0,255,cv2.THRESH_BINARY |cv2.THRESH_OTSU)[1]
elif args["preprocess"]=="blur":
gray=cv2.medianBlur(gray,3)
filename="{}.png".format(os.getpid())
cv2.imwrite(file.txt,gray)
"""
cv2.waitKey()
except:
print("takeyear")
break