-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (35 loc) · 1.45 KB
/
main.py
File metadata and controls
44 lines (35 loc) · 1.45 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
import cv2
import torch
from models.experimental import attempt_load
from utils.general import non_max_suppression, scale_coords
from utils.torch_utils import select_device
import numpy as np
device = select_device('cpu') # GPU
model = attempt_load('models/yolov7.pt', map_location=device)
cap = cv2.VideoCapture('data/test1.mp4')
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
img = cv2.resize(frame, (640, 640))
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR -> RGB, HWC -> CHW
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device).float() / 255.0
img = img.unsqueeze(0) # (batch_size, channels, height, width)
with torch.no_grad():
predictions = model(img)
if isinstance(predictions, tuple):
predictions = predictions[0]
results = non_max_suppression(predictions)
for det in results:
if det is not None:
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round()
for *xyxy, conf, cls in det:
label = f"{int(cls)} {conf:.2f}"
cv2.rectangle(frame, (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3])), (0, 255, 0), 2)
cv2.putText(frame, label, (int(xyxy[0]), int(xyxy[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow('YOLO Detection', frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()