-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.py
More file actions
80 lines (66 loc) · 2.5 KB
/
detector.py
File metadata and controls
80 lines (66 loc) · 2.5 KB
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
import cv2 as cv
import numpy as np
import wget
from os import mkdir, path
from os.path import join, abspath, dirname, exists
file_path = abspath(__file__)
file_parent_dir = dirname(file_path)
config_dir = join(file_parent_dir, 'config')
inputs_dir = join(file_parent_dir, 'inputs')
yolo_weights_path = join(config_dir, 'yolov3.weights')
yolo_names_path = join(config_dir, 'coco.names')
yolo_config_path = join(config_dir, 'yolov3.cfg')
input_image = join(inputs_dir, 'kemy.jpg')
net = cv.dnn.readNet(yolo_weights_path, yolo_config_path)
#To load all objects that have to be detected
classes=[]
with open(yolo_names_path,"r") as file_object:
lines = file_object.readlines()
for line in lines:
classes.append(line.strip("\n"))
#Defining layer names
layer_names = net.getLayerNames()
output_layers = []
for i in net.getUnconnectedOutLayers():
output_layers.append(layer_names[i[0]-1])
img = cv.imread(input_image)
height, width, channels = img.shape
#Extracting features to detect objects
blob = cv.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
#We need to pass the img_blob to the algorithm
net.setInput(blob)
outs = net.forward(output_layers)
#Displaying information on the screen
class_ids = []
confidences = []
boxes = []
for output in outs:
for detection in output:
#Detecting confidence in 3 steps
scores = detection[5:] #1
class_id = np.argmax(scores) #2
confidence = scores[class_id] #3
if confidence > 0.5: #Means if the object is detected
center_x = int(detection[0]*width)
center_y = int(detection[1]*height)
w = int(detection[2]*width)
h = int(detection[3]*height)
#Drawing a rectangle
x = int(center_x-w/2) # top left value
y = int(center_y-h/2) # top left value
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# cv.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
#Removing Double Boxes
indexes = cv.dnn.NMSBoxes(boxes, confidences, 0.3, 0.4)
for i in range(len(boxes)):
if i in indexes[0]:
x, y, w, h = boxes[i]
label = classes[class_ids[i]] # name of the objects
cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv.putText(img, label, (x, y), cv.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2)
title = f"Found <persons={indexes[0]}>"
cv.imshow(title, img)
cv.waitKey(0)
cv.destroyAllWindows()