-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
187 lines (162 loc) · 7.19 KB
/
main.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
182
183
184
185
186
187
import numpy as np
import cv2
CONFIDENCE = 0.01
THRESHOLD = 0.3
import time
import cv2
import easyocr
from pylab import rcParams
from IPython.display import Image
import string
import numpy as np
import cv2
import predicthelper as predictmodule
#LABELS = open(r'C:\\Users\\Dell\\Downloads\\MosaicPS2\\Automatic-Number-Plate-Recognition-main\\Automatic-Number-Plate-Recognition-main\\models\\plate.names').read().strip().split("\n")
#COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),dtype="uint8")
reader = easyocr.Reader(['en'])
ALLOWED_LIST = string.ascii_uppercase+string.digits
# ----------- load the trained model -----------
plate_net = cv2.dnn.readNetFromDarknet(r'./models/plate.cfg', r'./models/plate.weights')
plate_net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
plate_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
def classify_plate(image):
c=0
idxs = []
layerOutputs =[]
try:
(H, W) = image.shape[:2]
ln = plate_net.getLayerNames()
ln = [ln[i[0] - 1] for i in plate_net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),swapRB=True, crop=False)
plate_net.setInput(blob)
layerOutputs = plate_net.forward(ln)
except Exception as e:
print("PLATE EXTRACTION ERROR ", e)
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > CONFIDENCE:
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE, THRESHOLD)
if len(idxs) > 0:
# loop over the indexes we are keeping
for i in idxs.flatten():
print('cropaya')
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
print(x,y,w,h)
x-=5
if x<0:
x = 0
if y <0:
y = 0
w+=10
crop_img = image[y:y+h, x:x+w]
cv2.imshow(str(c),crop_img)
cv2.imwrite("./outputimage/"+str(c)+".png",crop_img)
c =c+1
return c
return -1
print('nhicropaya')
def classify_plate_in_video(cap):
frame_rate = 10
prev = 0
c=0
cnt=0
while (cap.isOpened()):
ret, frame = cap.read()
image =frame
cnt+=1
if ret == True:
idxs = []
layerOutputs =[]
try:
(H, W) = image.shape[:2]
ln = plate_net.getLayerNames()
ln = [ln[i[0] - 1] for i in plate_net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),swapRB=True, crop=False)
plate_net.setInput(blob)
layerOutputs = plate_net.forward(ln)
except Exception as e:
print("PLATE EXTRACTION ERROR ", e)
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > CONFIDENCE:
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE, THRESHOLD)
if len(idxs) > 0:
# loop over the indexes we are keeping
print('cropaya')
for i in idxs.flatten():
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
if x<0 or y<0 or w<0 or h<0:
break
crop_img = image[y:y+h, x:x+w]
if(cnt%10==0):
cv2.imwrite("./outputimage/"+str(c)+".png",crop_img)
c=c+1
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
return c
c = 0
## To Run on image
#image = cv2.imread('32.jpg')
#cv2.imshow('Image',image)
#c =classify_plate(image)
## To Run on Video
#cap = cv2.VideoCapture('2.mp4')
#c = classify_plate_in_video(cap)
img=cv2.imread("./plate_images/5.jpg")
row, col = img.shape[:2]
bottom = img[row-2:row, 0:col]
mean = cv2.mean(bottom)[0]
bordersize = 15
border = cv2.copyMakeBorder(
img,
top=bordersize,
bottom=bordersize,
left=bordersize,
right=bordersize,
borderType=cv2.BORDER_CONSTANT,
value=[255,255,255]
)
c=classify_plate(border)
if(c==-1):
print("numberplate not found")
else:
last = "0000"
k =1
print(c)
for i in range(c):
image = cv2.imread("./outputimage/"+str(i)+".png")
prediction = predictmodule.predict(image,reader,ALLOWED_LIST)
if prediction[-4:]!=last:
print("Car" + str(k)+ "plate number is " + prediction)
k = k+1
last = prediction[-4:]