-
Notifications
You must be signed in to change notification settings - Fork 3
/
Code1.txt
152 lines (129 loc) · 6.08 KB
/
Code1.txt
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
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
from twilio.rest import Client
match=FrameDivide.frame()
if(match==True):
i=0
if i==12:
print("Possible")
while(True):
try:
img= cv2.imread('frame'+str(i)+".jpg")
#cv2.namedWindow('Original Image',cv2.WINDOW_NORMAL)
#cv2.imshow('Original Image',img)
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#cv2.namedWindow('Gray Converted Image',cv2.WINDOW_NORMAL)
#cv2.imshow('Gray converted Image',img_gray)
noise_removal=cv2.bilateralFilter(img_gray,9,75,75)
#cv2.namedWindow('Noise Removed image',cv2.WINDOW_NORMAL)
#cv2.imshow('Noise removed image',noise_removal)
equal_histogram = cv2.equalizeHist(noise_removal)
#cv2.namedWindow('After histogram ',cv2.WINDOW_NORMAL)
#cv2.imshow('After histogram',equal_histogram)
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)
sub_morph_image=cv2.subtract(equal_histogram,morph_image)
#cv2.namedWindow("Subtraction image",cv2.WINDOW_NORMAL)
#cv2.imshow("Subtract image",sub_morph_image)
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)
canny_image= cv2.Canny(thresh_image,250,255)
#cv2.namedWindow("Image after applying canny",cv2.WINDOW_NORMAL)
canny_image= cv2.convertScaleAbs(canny_image)
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)
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)
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)
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)
#cv2.namedWindow("Enhanced Number Plate",cv2.WINDOW_NORMAL)
#cv2.imshow("Enhanced Number plate",final_image)
cv2.imwrite('final.png',final_image)
test_file,d =stringOCR.ocr_space_file(filename='final.png', language='pol')
print(d)
if "KAT" in d or "AT" in d or "056" in d or "KAT 056" in d:
os.system("Cars.mp4")
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. Contact AABBCC Toll Tax Soon"
account_sid = "AC5adda6db593a0f369a427283c8436959"
auth_token = "fc647306d1cd37646ac0f1f23c55e612"
client = Client(account_sid, auth_token)
client.api.account.messages.create(to="+13153538299",from_="+13153229683",body=msg)
print("Message Sent")
break
break
else:
i+=3
print("Not Found")
if i==27:
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:
pass
for i in range(20):
os.remove("frame"+str(i)+".jpg")
os.remove("final.png")