-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDetector.py
74 lines (51 loc) · 2.45 KB
/
Detector.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
from pyexpat import model
import cv2
import numpy as np
import time
class Detector:
def __init__(self, vidPath, configPath, modelPath, classesPath):
self.vidPath = vidPath
self.configPath = configPath
self.modelPath = modelPath
self.classesPath = classesPath
self.network = cv2.dnn_DetectionModel(self.modelPath, self.configPath)
self.network.setInputSize(320, 320)
self.network.setInputScale(1.0/127.5)
self.network.setInputMean((127.5, 127.5, 127.5))
self.network.setInputSwapRB(True)
self.readClasses()
def readClasses(self):
with open(self.classesPath, "r") as f:
self.classesList = f.read().splitlines()
self.classesList.insert(0, "__Background__")
self.colorList = np.random.uniform(low=0, high=255, size=(len(self.classesList), 3))
#print(self.classesList)
def onVideo(self):
capture = cv2.VideoCapture(self.vidPath)
if not capture.isOpened():
print("Error loading file...")
return
success, image = capture.read()
while success:
classLabelIDs, confs, bboxs = self.network.detect(image, confThreshold = 0.5)
bboxs = list(bboxs)
confs = list(np.array(confs).reshape(1,-1)[0])
confs = list(map(float, confs))
bboxIds = cv2.dnn.NMSBoxes(bboxs, confs, score_threshold = 0.5, nms_threshold = 0.2)
if len(bboxIds) != 0:
for i in range(0, len(bboxIds)):
bbox = bboxs[np.squeeze(bboxIds[i])]
classConfidence = confs[np.squeeze(bboxIds[i])]
classLabelID = np.squeeze(classLabelIDs[np.squeeze(bboxIds[i])])
classLabel = self.classesList[classLabelID]
classColor = [int(c) for c in self.colorList[classLabelID]]
displayText = "{}:{:.4f}".format(classLabel, classConfidence)
x, y, w, h = bbox
cv2.rectangle(image, (x,y), (x+w, y+h), color=classColor, thickness=2)
cv2.putText(image, displayText, (x,y-10), cv2.FONT_HERSHEY_PLAIN, 1, classColor, 2)
cv2.imshow("Realtime Object Detection (Press Q to Quit)", image)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
success, image = capture.read()
cv2.destroyAllWindows()