-
Notifications
You must be signed in to change notification settings - Fork 1
/
Video_CharsRecognizer.py
136 lines (102 loc) · 4.37 KB
/
Video_CharsRecognizer.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
import numpy as np
import cv2
from matplotlib import pyplot as plt
from operator import itemgetter
import operator
import os.path
def drawcontour_(image, predictor):
videoimg = image
# if videoimg.shape[2] is 3:
gray = cv2.cvtColor(videoimg, cv2.COLOR_BGR2GRAY)
# imshow_(gray)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
# imshow_(blur)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# imshow_(thresh)
edged = cv2.Canny(gray, 300, 150)
# imshow_(edged)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
# imshow_(closed)
img2, contours, hierarchy = cv2.findContours(closed.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
area = cv2.contourArea(c)
x, y, w, h = cv2.boundingRect(c)
aspectratio = float(w) / h
height, width, channels = videoimg.shape
pick_area = w * h
total_area = width * height
#extent = float(pick_area) / total_area
extent = float(area) / total_area
if (aspectratio) > 6:
pass
elif 6 > (aspectratio) > 1.6:
if len(approx) == 4 and (extent) > 0.005:
cv2.drawContours(videoimg, [approx], -1, (255, 0, 0), 1)
#imshow_(videoimg)
crop_img = videoimg[y-2:y + h+2, x-2:x + w+2]
#imshow_(crop_img)
result = Cropped_Licenseplate(crop_img, predictor)
if len(result) > 0:
print(result)
return True
#result=area
#font = cv2.FONT_HERSHEY_SIMPLEX
#cv2.putText(videoimg, result, (x, y + h + 20), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
#cv2.putText(videoimg, str(result), (x, y + h + 20), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
#cv2.waitKey(0)
else:
pass
return False
def Cropped_Licenseplate(crop_img1, predictor):
imgfile = crop_img1
gray = cv2.GaussianBlur(imgfile, (3, 3), 0)
# imshow_(blur)
#ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# imshow_(thresh)
edged = cv2.Canny(gray, 500, 150)
# imshow_(edged)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
# imshow_(closed)
img2, contours, hierarchy = cv2.findContours(closed.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
height, width, channels = imgfile.shape
total_area = width * height
appropriate_contours = find_appropriate_contours(contours, total_area)
from erode_checker import find_independant_idx
max_area_idxs = find_independant_idx(appropriate_contours)
results = []
for idx in max_area_idxs:
result = puttext_below_contour(crop_img1, imgfile, appropriate_contours[idx], predictor)
if result is not None:
results.append(result)
return(results)
def puttext_below_contour(crop_img1, imgfile, contour, predictor):
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(imgfile, (x, y), (x + w, y + h), (0, 255, 0), 1)
crop_img2 = imgfile[y - 2:y + h + 2, x - 2:x + w + 2]
from LetterRecognizer.recognizer_forContourRecognizer import letter_recognizer_with_opencvimage
crop_img2 = cv2.cvtColor(crop_img2, cv2.COLOR_BGR2GRAY)
result = None
if crop_img2 is not None:
result = letter_recognizer_with_opencvimage(crop_img2, predictor)[0]
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(crop_img1, str(result), (x, y + h + 20), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
return result
def find_appropriate_contours(contours, total_area):
appropriate_contours = []
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
area = cv2.contourArea(c)
x, y, w, h = cv2.boundingRect(c)
aspectratio = float(w) / h
pick_area = w * h
#extent = float(pick_area) / total_area
extent = float(area) / total_area
#if 1.2 > (aspectratio) > 0.25 and (extent) > 0.02:
if 1.2 > (aspectratio) > 0.01 and (extent) > 0.01:
appropriate_contours.append(c)
return appropriate_contours