Spaces:
Sleeping
Sleeping
Update classifier.py
Browse files- classifier.py +149 -15
classifier.py
CHANGED
|
@@ -9,10 +9,17 @@ from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
|
| 9 |
from tensorflow.keras.layers import Dense, Flatten, MaxPooling2D, Dropout, Conv2D
|
| 10 |
from tensorflow.keras.models import load_model
|
| 11 |
|
| 12 |
-
# Loads the data required for detecting the license plates from cascade classifier.
|
| 13 |
-
plate_cascade = cv2.CascadeClassifier('indian_license_plate.xml')
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
plate_img = img.copy()
|
| 17 |
roi = img.copy()
|
| 18 |
plate_rect = plate_cascade.detectMultiScale(plate_img, scaleFactor = 1.2, minNeighbors = 7) # detects numberplates and returns the coordinates and dimensions of detected license plate's contours.
|
|
@@ -26,8 +33,10 @@ def detect_plate(img, text=''): # the function detects and perfors blurring on t
|
|
| 26 |
|
| 27 |
return plate_img, plate # returning the processed image.
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
img = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB)
|
| 32 |
fig = plt.figure(figsize=(10,6))
|
| 33 |
ax = plt.subplot(111)
|
|
@@ -36,11 +45,131 @@ def display(img_, title=''):
|
|
| 36 |
plt.title(title)
|
| 37 |
plt.show()
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
dic = {}
|
| 45 |
characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
| 46 |
for i, c in enumerate(characters):
|
|
@@ -60,7 +189,16 @@ def show_results():
|
|
| 60 |
|
| 61 |
return plate_number
|
| 62 |
|
| 63 |
-
print(show_results())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
|
|
@@ -78,17 +216,13 @@ def run():
|
|
| 78 |
|
| 79 |
# Check if any file is uploaded
|
| 80 |
if uploaded_files:
|
| 81 |
-
for
|
| 82 |
-
st.write("filename:",
|
| 83 |
# Close the form
|
| 84 |
submitted = st.form_submit_button('Recognize')
|
| 85 |
|
| 86 |
if submitted:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
|
| 94 |
if __name__ == '__main__':
|
|
|
|
| 9 |
from tensorflow.keras.layers import Dense, Flatten, MaxPooling2D, Dropout, Conv2D
|
| 10 |
from tensorflow.keras.models import load_model
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
|
| 14 |
+
def my_function():
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Loads the data required for detecting the license plates from cascade classifier.
|
| 19 |
+
plate_cascade = cv2.CascadeClassifier('indian_license_plate.xml')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def detect_plate(img, text=''): # the function detects and perfors blurring on the number plate.
|
| 23 |
plate_img = img.copy()
|
| 24 |
roi = img.copy()
|
| 25 |
plate_rect = plate_cascade.detectMultiScale(plate_img, scaleFactor = 1.2, minNeighbors = 7) # detects numberplates and returns the coordinates and dimensions of detected license plate's contours.
|
|
|
|
| 33 |
|
| 34 |
return plate_img, plate # returning the processed image.
|
| 35 |
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Testing the above function
|
| 39 |
+
def display(img_, title=''):
|
| 40 |
img = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB)
|
| 41 |
fig = plt.figure(figsize=(10,6))
|
| 42 |
ax = plt.subplot(111)
|
|
|
|
| 45 |
plt.title(title)
|
| 46 |
plt.show()
|
| 47 |
|
| 48 |
+
img = cv2.imread('car3.jpg')
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Getting plate prom the processed image
|
| 52 |
+
output_img, plate = detect_plate(img)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# Match contours to license plate or character template
|
| 56 |
+
def find_contours(dimensions, img) :
|
| 57 |
+
|
| 58 |
+
# Find all contours in the image
|
| 59 |
+
cntrs, _ = cv2.findContours(img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 60 |
+
|
| 61 |
+
# Retrieve potential dimensions
|
| 62 |
+
lower_width = dimensions[0]
|
| 63 |
+
upper_width = dimensions[1]
|
| 64 |
+
lower_height = dimensions[2]
|
| 65 |
+
upper_height = dimensions[3]
|
| 66 |
+
|
| 67 |
+
# Check largest 5 or 15 contours for license plate or character respectively
|
| 68 |
+
cntrs = sorted(cntrs, key=cv2.contourArea, reverse=True)[:15]
|
| 69 |
+
|
| 70 |
+
ii = cv2.imread('contour.jpg')
|
| 71 |
+
|
| 72 |
+
x_cntr_list = []
|
| 73 |
+
target_contours = []
|
| 74 |
+
img_res = []
|
| 75 |
+
for cntr in cntrs :
|
| 76 |
+
# detects contour in binary image and returns the coordinates of rectangle enclosing it
|
| 77 |
+
intX, intY, intWidth, intHeight = cv2.boundingRect(cntr)
|
| 78 |
+
|
| 79 |
+
# checking the dimensions of the contour to filter out the characters by contour's size
|
| 80 |
+
if intWidth > lower_width and intWidth < upper_width and intHeight > lower_height and intHeight < upper_height :
|
| 81 |
+
x_cntr_list.append(intX) #stores the x coordinate of the character's contour, to used later for indexing the contours
|
| 82 |
+
|
| 83 |
+
char_copy = np.zeros((44,24))
|
| 84 |
+
# extracting each character using the enclosing rectangle's coordinates.
|
| 85 |
+
char = img[intY:intY+intHeight, intX:intX+intWidth]
|
| 86 |
+
char = cv2.resize(char, (20, 40))
|
| 87 |
+
|
| 88 |
+
cv2.rectangle(ii, (intX,intY), (intWidth+intX, intY+intHeight), (50,21,200), 2)
|
| 89 |
+
plt.imshow(ii, cmap='gray')
|
| 90 |
+
|
| 91 |
+
# Make result formatted for classification: invert colors
|
| 92 |
+
char = cv2.subtract(255, char)
|
| 93 |
+
|
| 94 |
+
# Resize the image to 24x44 with black border
|
| 95 |
+
char_copy[2:42, 2:22] = char
|
| 96 |
+
char_copy[0:2, :] = 0
|
| 97 |
+
char_copy[:, 0:2] = 0
|
| 98 |
+
char_copy[42:44, :] = 0
|
| 99 |
+
char_copy[:, 22:24] = 0
|
| 100 |
+
|
| 101 |
+
img_res.append(char_copy) # List that stores the character's binary image (unsorted)
|
| 102 |
+
|
| 103 |
+
# Return characters on ascending order with respect to the x-coordinate (most-left character first)
|
| 104 |
+
|
| 105 |
+
plt.show()
|
| 106 |
+
# arbitrary function that stores sorted list of character indeces
|
| 107 |
+
indices = sorted(range(len(x_cntr_list)), key=lambda k: x_cntr_list[k])
|
| 108 |
+
img_res_copy = []
|
| 109 |
+
for idx in indices:
|
| 110 |
+
img_res_copy.append(img_res[idx])# stores character images according to their index
|
| 111 |
+
img_res = np.array(img_res_copy)
|
| 112 |
+
|
| 113 |
+
return img_res
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# Find characters in the resulting images
|
| 117 |
+
def segment_characters(image) :
|
| 118 |
+
|
| 119 |
+
# Preprocess cropped license plate image
|
| 120 |
+
img_lp = cv2.resize(image, (333, 75))
|
| 121 |
+
img_gray_lp = cv2.cvtColor(img_lp, cv2.COLOR_BGR2GRAY)
|
| 122 |
+
_, img_binary_lp = cv2.threshold(img_gray_lp, 200, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
|
| 123 |
+
img_binary_lp = cv2.erode(img_binary_lp, (3,3))
|
| 124 |
+
img_binary_lp = cv2.dilate(img_binary_lp, (3,3))
|
| 125 |
+
|
| 126 |
+
LP_WIDTH = img_binary_lp.shape[0]
|
| 127 |
+
LP_HEIGHT = img_binary_lp.shape[1]
|
| 128 |
+
|
| 129 |
+
# Make borders white
|
| 130 |
+
img_binary_lp[0:3,:] = 255
|
| 131 |
+
img_binary_lp[:,0:3] = 255
|
| 132 |
+
img_binary_lp[72:75,:] = 255
|
| 133 |
+
img_binary_lp[:,330:333] = 255
|
| 134 |
+
|
| 135 |
+
# Estimations of character contours sizes of cropped license plates
|
| 136 |
+
dimensions = [LP_WIDTH/6,
|
| 137 |
+
LP_WIDTH/2,
|
| 138 |
+
LP_HEIGHT/10,
|
| 139 |
+
2*LP_HEIGHT/3]
|
| 140 |
+
plt.imshow(img_binary_lp, cmap='gray')
|
| 141 |
+
plt.show()
|
| 142 |
+
cv2.imwrite('contour.jpg',img_binary_lp)
|
| 143 |
|
| 144 |
+
# Get contours within cropped license plate
|
| 145 |
+
char_list = find_contours(dimensions, img_binary_lp)
|
| 146 |
|
| 147 |
+
return char_list
|
| 148 |
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# Let's see the segmented characters
|
| 152 |
+
char = segment_characters(plate)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# Using the Model that we Trained on Kaggle Dataset earlier
|
| 158 |
+
model = load_model('licence_trained_model.h5', compile=False)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
# Predicting the output
|
| 163 |
+
def fix_dimension(img):
|
| 164 |
+
new_img = np.zeros((28,28,3))
|
| 165 |
+
for i in range(3):
|
| 166 |
+
new_img[:,:,i] = img
|
| 167 |
+
return new_img
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def show_results():
|
| 173 |
dic = {}
|
| 174 |
characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
| 175 |
for i, c in enumerate(characters):
|
|
|
|
| 189 |
|
| 190 |
return plate_number
|
| 191 |
|
| 192 |
+
print(show_results())
|
| 193 |
+
|
| 194 |
+
my_function()
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
|
| 202 |
|
| 203 |
|
| 204 |
|
|
|
|
| 216 |
|
| 217 |
# Check if any file is uploaded
|
| 218 |
if uploaded_files:
|
| 219 |
+
for uploaded_file in uploaded_files:
|
| 220 |
+
st.write("filename:", uploaded_file.name)
|
| 221 |
# Close the form
|
| 222 |
submitted = st.form_submit_button('Recognize')
|
| 223 |
|
| 224 |
if submitted:
|
| 225 |
+
my_function()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
|
| 228 |
if __name__ == '__main__':
|