Skip to content

Commit d4824a4

Browse files
committed
ANPR Code
0 parents  commit d4824a4

15 files changed

+2803
-0
lines changed

Diff for: Images/car.jpg

148 KB
Loading

Diff for: Images/car1.jpg

64.1 KB
Loading

Diff for: Images/car10.jpg

192 KB
Loading

Diff for: Images/car2.jpg

99.2 KB
Loading

Diff for: Images/car2a.jpg

105 KB
Loading

Diff for: Images/car3.jpg

126 KB
Loading

Diff for: Images/car4.jpg

94.6 KB
Loading

Diff for: Images/car5.jpg

152 KB
Loading

Diff for: Images/car6.jpg

46.2 KB
Loading

Diff for: Images/car7.jpg

45.5 KB
Loading

Diff for: Images/car8.jpg

76.6 KB
Loading

Diff for: Images/car9.jpg

67.1 KB
Loading

Diff for: anpr_code.py

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import cv2
2+
import re
3+
import numpy as np
4+
import pytesseract
5+
6+
plateCascade = cv2.CascadeClassifier(r"C:\Users\ASUS\Documents\anpr work\ANPR\haarcascade_plate_number.xml")
7+
8+
def textExtract( img ) :
9+
10+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
11+
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
12+
13+
kernel = np.ones((3,3), np.uint8)
14+
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
15+
sure_bg = cv2.dilate(opening, kernel, iterations=3)
16+
17+
contours, hierarchy = cv2.findContours(sure_bg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
18+
19+
for cnt in contours:
20+
x,y,w,h = cv2.boundingRect(cnt)
21+
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
22+
roi = sure_bg[y:y+h, x:x+w]
23+
text = pytesseract.image_to_string(roi)
24+
25+
return text
26+
27+
def checkPlate( imgRoi, img ) :
28+
29+
global count
30+
while True :
31+
32+
# if cv2.waitKey(1) & 0xFF ==ord('s'):
33+
34+
cv2.imwrite(r"C:\Users\ASUS\Documents\anpr work\ANPR\result\out"+str(count)+".jpg",imgRoi)
35+
36+
# image = cv2.imread(r"C:\Users\ASUS\Documents\anpr work\out"+str(count)+".jpg")
37+
38+
text1 = pytesseract.image_to_string(imgRoi)
39+
text2 = textExtract( imgRoi )
40+
print(text1)
41+
print(text2)
42+
ntext1 = ""
43+
ntext2 = ""
44+
45+
for i in range( 0, len(text1) ) :
46+
if ( text1[i].isalpha() ) :
47+
ntext1 = text1[i:i+10]
48+
break
49+
print(ntext1)
50+
51+
for i in range( 0, len(text2) ) :
52+
if ( text2[i].isalpha() ) :
53+
ntext2 = text2[i:i+10]
54+
break
55+
print(ntext2)
56+
57+
plate_format1 = re.compile("^([A-Z]{2})(\d{2})[A-Z]{2}(\d{4})$")
58+
plate_format2 = re.compile("^([A-Z]{2})(\d{1})[A-Z]{3}(\d{4})$")
59+
60+
if ( plate_format1.match(ntext1) or plate_format2.match(ntext1) ) :
61+
print( "Valid" )
62+
63+
else :
64+
print("Invalid")
65+
66+
if ( plate_format1.match(ntext2) or plate_format2.match(ntext2) ) :
67+
print( "Valid" )
68+
69+
else :
70+
print("Invalid")
71+
72+
# digits , character = 0, 0
73+
74+
# for i in ntext :
75+
# if( i.isalpha() ) :
76+
# character = character + 1
77+
# elif ( i.isdigit() ) :
78+
# digits = digits + 1
79+
80+
# if ( character == 4 and digits == 6):
81+
# print('valid')
82+
83+
# else:
84+
# print('not valid')
85+
86+
cv2.rectangle(img,(0,200),(640,300),(0,255,0),cv2.FILLED)
87+
cv2.putText(img,"Saved",(15,265),cv2.FONT_HERSHEY_COMPLEX,2,(0,0,255),2)
88+
cv2.imshow("Result",img)
89+
cv2.waitKey(50)
90+
count+=1
91+
return
92+
93+
94+
def findPlate( img ) :
95+
96+
minArea = 500
97+
imgRoi = 0
98+
# img = cv2.imread( file )
99+
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
100+
numberPlates = plateCascade .detectMultiScale(imgGray, 1.1, 4)
101+
102+
for (x, y, w, h) in numberPlates:
103+
area = w*h
104+
if area > minArea:
105+
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
106+
cv2.putText(img,"NumberPlate",(x,y-5), cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
107+
imgRoi = img[y:y+h,x:x+w]
108+
cv2.imshow("ROI",imgRoi)
109+
cv2.waitKey( 100 )
110+
111+
cv2.imshow("Result",img)
112+
cv2.waitKey( 100 )
113+
114+
if ( type(imgRoi) == type(np.array([1])) ) :
115+
checkPlate(imgRoi, img)
116+
117+
else :
118+
print("Not Found")
119+
120+
def frameOut( vid ) :
121+
122+
cap = cv2.VideoCapture( vid )
123+
skip = 20
124+
while cap.isOpened():
125+
126+
ret, frame = cap.read()
127+
if not ret:
128+
break
129+
130+
if cap.get(cv2.CAP_PROP_POS_FRAMES) % skip != 0:
131+
continue
132+
133+
findPlate( frame )
134+
135+
cap.release()
136+
137+
138+
if __name__ == "__main__" :
139+
140+
global count
141+
count = 0
142+
file = r'C:\Users\ASUS\Documents\anpr work\ANPR\Images\car.jpg'
143+
img = cv2.imread( file )
144+
vid1 = r'C:\Users\ASUS\Documents\anpr work\ANPR\sample_video.mp4'
145+
vid2 = r'C:\Users\ASUS\Documents\anpr work\sample_video_Trim.mp4'
146+
frameOut( vid1 )
147+
# findPlate( img )

0 commit comments

Comments
 (0)