-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_1.py
57 lines (47 loc) · 1.43 KB
/
demo_1.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
import cv2
import torch
'''
This demo just detects the bounding boxes and its
center coordinates and displays it on the frame
'''
# Text Parameters
FONT_FACE = cv2.FONT_HERSHEY_SIMPLEX
FONT_SCALE = 0.7
THICKNESS = 10
# Colors
BLACK = (0, 0, 0)
BLUE = (255, 178, 50)
RED = (0, 0, 255)
YELLOW = (0, 255, 255)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
video_path = "highway.mp4"
cap = cv2.VideoCapture(video_path)
center_points = []
while True:
ret, frame = cap.read()
results = model(frame)
readable_results = results.pandas().xyxy # xmin, ymin, xmax, ymax, confidence, class, name
new_points = []
if not ret:
continue
print(readable_results)
for result in readable_results:
for i in range(0, len(result)):
xmin = int(result['xmin'][i])
ymin = int(result['ymin'][i])
xmax = int(result['xmax'][i])
ymax = int(result['ymax'][i])
start_point = (xmin, ymin)
end_point = (xmax, ymax)
center = (int((xmin + xmax) / 2), int((ymin + ymax) / 2))
center_points.append(center)
cv2.rectangle(frame, start_point, end_point, BLUE, THICKNESS)
# cv2.circle(frame, center, 5, RED, -1)
for center in center_points:
cv2.circle(frame, center, 5, RED, -1)
cv2.imshow("Frame", frame)
key = cv2.waitKey(0)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()