-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
105 lines (79 loc) · 2.83 KB
/
test.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os, glob
from segmentar import extraction
from tensorflow.keras.models import load_model
TEST = "test_pics"
mapping= {
0 : "0", 1 : "1", 2 : "2", 3 : "3", 4 : "4",
5 : "5", 6 : "6", 7 : "7", 8 : "8", 9 : "9",
10 : "A", 11 : "B", 12 : "C", 13 : "D", 14 : "E",
15 : "F", 16 : "G", 17 : "H", 18 : "I", 19 : "J",
20 : "K", 21 : "L", 22 :"M", 23: "N", 24 : "O",
25 : "P", 26 : "Q", 27 : "R", 28 : "S", 29 : "T",
30 : "U", 31 : "V", 32 : "W", 33 : "X", 34 : "Y", 35: "Z"
}
##-------loading model with weights---
model = load_model("model.h5")
def convention(predictions, length):
"""
Function for implementimg the convention of Indian plates
"""
cls = np.argmax(predictions, axis = 1)
for i in [0,1]: # first 2 district code must represent letters
if cls[i]==0:
cls[i]= 24
# else:
# print(np.argmax(predictions[i,10:]))
# cls[i] = 10+np.argmax(predictions[i,10:])
for i in [-4,-3,-2,-1]: # last 4 numbers
if cls[i]== 24:
cls[i]=0
# else:
# cls[i] = np.argmax(predictions[i,:10])
diff = length-6
for i in range(2, 2+diff):
if i in [2,3]: # registration no.
if cls[i]==24:
cls[i]=0
# else:
# cls[i] = np.argmax(predictions[i,:10])
word = [mapping[predict] for predict in cls]
return word
# print("Enter the path of input image")
# path = input()
for f in os.listdir(TEST):
try :
path = "test_pics/"+f
print("\n","File :",path, "\n")
sample = cv2.imread(path)
# print("Shape :",sample.shape)
cv2.imshow("f", sample)
## resizing image with proper aspect ratio------------
imgs = extraction(path) ## GENERATOR CALLED
imgs = np.asarray(imgs)
imgs = imgs.reshape(-1,64,64,1).astype("float32")/255
if len(imgs)<6:
predictions = model(imgs)
cls = np.argmax(predictions, axis = 1)
print(cls)
word = [mapping[classes] for classes in cls]
elif len(imgs)<8:
predictions = model(imgs)
word = convention(predictions, len(predictions))
else:
predictions = model(imgs)
cls = np.argmax(predictions, axis = 1)
word = convention(predictions, len(predictions))
# print(prediction)
for i in range(len(imgs)):
cv2.imshow("imgs" + str(i), imgs[i,:,:])
#------------Resizing_finished--------------
print("\nPredicted License Plate : ","".join(word),"\n")
print("".join(word))
# cv2.imshow("extract", imgs)
if cv2.waitKey(0)&0Xff ==27:
cv2.destroyAllWindows()
except :
pass