-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyolo_prediction.py
168 lines (126 loc) · 5.05 KB
/
yolo_prediction.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
import numpy as np
import cv2
import matplotlib.pyplot as plt
import pytesseract as pt
# LOAD YOLO MODEL
INPUT_WIDTH = 640
INPUT_HEIGHT = 640
net = cv2.dnn.readNetFromONNX('./static/models/best.onnx')
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
def get_detections(img,net):
# CONVERT IMAGE TO YOLO FORMAT
image = img.copy()
row, col, d = image.shape
max_rc = max(row,col)
input_image = np.zeros((max_rc,max_rc,3),dtype=np.uint8)
input_image[0:row,0:col] = image
# GET PREDICTION FROM YOLO MODEL
blob = cv2.dnn.blobFromImage(input_image,1/255,(INPUT_WIDTH,INPUT_HEIGHT),swapRB=True,crop=False)
net.setInput(blob)
preds = net.forward()
detections = preds[0]
return input_image, detections
def non_maximum_supression(input_image,detections):
# FILTER DETECTIONS BASED ON CONFIDENCE AND PROBABILIY SCORE
# center x, center y, w , h, conf, proba
boxes = []
confidences = []
image_w, image_h = input_image.shape[:2]
x_factor = image_w/INPUT_WIDTH
y_factor = image_h/INPUT_HEIGHT
for i in range(len(detections)):
row = detections[i]
confidence = row[4] # confidence of detecting license plate
if confidence > 0.4:
class_score = row[5] # probability score of license plate
if class_score > 0.25:
cx, cy , w, h = row[0:4]
left = int((cx - 0.5*w)*x_factor)
top = int((cy-0.5*h)*y_factor)
width = int(w*x_factor)
height = int(h*y_factor)
box = np.array([left,top,width,height])
confidences.append(confidence)
boxes.append(box)
# clean
boxes_np = np.array(boxes).tolist()
confidences_np = np.array(confidences).tolist()
# NMS
index = np.array(cv2.dnn.NMSBoxes(boxes_np,confidences_np,0.25,0.45)).flatten()
return boxes_np, confidences_np, index
def extract_text(image,bbox):
x,y,w,h = bbox
roi = image[y:y+h, x:x+w]
if 0 in roi.shape:
return ''
else:
roi_bgr = cv2.cvtColor(roi,cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(roi_bgr,cv2.COLOR_BGR2GRAY)
magic_color = apply_brightness_contrast(gray,brightness=40,contrast=70)
#text = pt.image_to_string(magic_color)
text = pt.image_to_string(magic_color,lang='eng',config='--psm 6')
text = text.strip()
return text
def drawings(image,boxes_np,confidences_np,index):
# drawings
text_list = []
for ind in index:
x,y,w,h = boxes_np[ind]
bb_conf = confidences_np[ind]
conf_text = 'plate: {:.0f}%'.format(bb_conf*100)
license_text = extract_text(image,boxes_np[ind])
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
cv2.rectangle(image,(x,y-30),(x+w,y),(255,0,255),-1)
cv2.rectangle(image,(x,y+h),(x+w,y+h+30),(0,0,0),-1)
cv2.putText(image,conf_text,(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.7,(255,255,255),1)
cv2.putText(image,license_text,(x,y+h+27),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),1)
text_list.append(license_text)
return image, text_list
# predictions
def yolo_predictions(img,net):
## step-1: detections
input_image, detections = get_detections(img,net)
## step-2: NMS
boxes_np, confidences_np, index = non_maximum_supression(input_image, detections)
## step-3: Drawings
result_img, text = drawings(img,boxes_np,confidences_np,index)
return result_img, text
def object_detection(path,filename):
# read image
image = cv2.imread(path) # PIL object
image = np.array(image,dtype=np.uint8) # 8 bit array (0,255)
result_img, text_list = yolo_predictions(image,net)
cv2.imwrite('./static/predict/{}'.format(filename),result_img)
return text_list
# def OCR(path,filename):
# img = np.array(load_img(path))
# cods = object_detection(path,filename)
# xmin ,xmax,ymin,ymax = cods[0]
# roi = img[ymin:ymax,xmin:xmax]
# roi_bgr = cv2.cvtColor(roi,cv2.COLOR_RGB2BGR)
# gray = cv2.cvtColor(roi_bgr,cv2.COLOR_BGR2GRAY)
# magic_color = apply_brightness_contrast(gray,brightness=40,contrast=70)
# cv2.imwrite('./static/roi/{}'.format(filename),roi_bgr)
# print(text)
# save_text(filename,text)
# return text
def apply_brightness_contrast(input_img, brightness = 0, contrast = 0):
if brightness != 0:
if brightness > 0:
shadow = brightness
highlight = 255
else:
shadow = 0
highlight = 255 + brightness
alpha_b = (highlight - shadow)/255
gamma_b = shadow
buf = cv2.addWeighted(input_img, alpha_b, input_img, 0, gamma_b)
else:
buf = input_img.copy()
if contrast != 0:
f = 131*(contrast + 127)/(127*(131-contrast))
alpha_c = f
gamma_c = 127*(1-f)
buf = cv2.addWeighted(buf, alpha_c, buf, 0, gamma_c)
return buf