-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdetection.py
65 lines (59 loc) · 2.66 KB
/
detection.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
import cv2
import pytesseract
import numpy as np
import glob
class PlateDetector:
def load_model(self, weight_path: str, cfg_path: str):
self.net = cv2.dnn.readNet(weight_path, cfg_path)
with open("classes-detection.names", "r") as f:
self.classes = [line.strip() for line in f.readlines()]
self.layers_names = self.net.getLayerNames()
self.output_layers = [self.layers_names[i[0]-1] for i in self.net.getUnconnectedOutLayers()]
def load_image(self, img_path):
img = cv2.imread(img_path)
height, width, channels = img.shape
return img, height, width, channels
def detect_plates(self, img):
blob = cv2.dnn.blobFromImage(img, scalefactor=0.00392, size=(320, 320), mean=(0, 0, 0), swapRB=True, crop=False)
self.net.setInput(blob)
outputs = self.net.forward(self.output_layers)
return blob, outputs
def get_boxes(self, outputs, width, height, threshold=0.3):
boxes = []
confidences = []
class_ids = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > threshold:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
return boxes, confidences, class_ids
def draw_labels(self, boxes, confidences, class_ids, img):
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.1, 0.1)
font = cv2.FONT_HERSHEY_PLAIN
plats = []
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(self.classes[class_ids[i]])
color_green = (0, 255, 0)
crop_img = img[y:y+h, x:x+w]
try:
crop_resized = cv2.resize(crop_img, dsize=(470, 110))
plats.append(crop_resized)
cv2.rectangle(img, (x, y), (x + w, y + h), color_green, 8)
confidence = round(confidences[i], 3) * 100
cv2.putText(img, str(confidence) + "%", (x + 20, y - 20), font, 12, (0, 255, 0), 6)
except cv2.error as err:
print(err)
return img,plats