-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
41 lines (34 loc) · 1.25 KB
/
detect.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
# Import PyTorch module
import torch
import cv2
# Download model from github
model = torch.hub.load('ultralytics/yolov5', 'yolov5n')
#img = cv2.VideoCapture("Could today's global conflicts bring a Third World War closer. - Inside Story.mp4")
img = cv2.imread('Capture15.JPG')
img = cv2.resize(img,(1000, 650))
# Perform detection on image
result = model(img)
print('result: ', result)
# Convert detected result to pandas data frame
data_frame = result.pandas().xyxy[0]
print('data_frame:')
print(data_frame)
# Get indexes of all of the rows
indexes = data_frame.index
for index in indexes:
# Find the coordinate of top left corner of bounding box
x1 = int(data_frame['xmin'][index])
y1 = int(data_frame['ymin'][index])
# Find the coordinate of right bottom corner of bounding box
x2 = int(data_frame['xmax'][index])
y2 = int(data_frame['ymax'][index ])
# Find label name
label = data_frame['name'][index ]
# Find confidance score of the model
conf = data_frame['confidence'][index]
text = label + ' ' + str(conf.round(decimals= 2))
cv2.rectangle(img, (x1,y1), (x2,y2), (255,255,0), 2)
cv2.putText(img, text, (x1,y1-5), cv2.FONT_HERSHEY_PLAIN, 2,
(255,255,0), 2)
cv2.imshow('IMAGE', img)
cv2.waitKey(0)