-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_with_camera.py
118 lines (90 loc) · 3.21 KB
/
detect_with_camera.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import time
import cv2
import torch
import numpy as np
from numpy import random
from models.experimental import attempt_load
from utils.datasets import letterbox
from utils.general import check_img_size, check_requirements, non_max_suppression, scale_coords
from utils.plots import plot_one_box
from utils.torch_utils import select_device, time_synchronized
WEIGHTS = 'testtttttttt.pt'
IMG_SIZE = 640
DEVICE = ''
AUGMENT = False
CONF_THRES = 0.25
IOU_THRES = 0.45
CLASSES = None
AGNOSTIC_NMS = False
# Webcam
cap = cv2.VideoCapture(0)
# Initialize
device = select_device(DEVICE)
half = device.type != 'cpu' # half precision only supported on CUDA
print('device:', device)
# Load model
model = attempt_load(WEIGHTS, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(IMG_SIZE, s=stride) # check img_size
if half:
model.half() # to FP16
# Get names and colors
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
# Detect function
def detect(frame):
# Load image
img0 = frame
# Padded resize
img = letterbox(img0, imgsz, stride=stride)[0]
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
t0 = time_synchronized()
pred = model(img, augment=AUGMENT)[0]
# Apply NMS
pred = non_max_suppression(pred, CONF_THRES, IOU_THRES, classes=CLASSES, agnostic=AGNOSTIC_NMS)
# Process detections
det = pred[0]
s = ''
s += '%gx%g ' % img.shape[2:] # print string
if len(det):
# Rescale boxes from img_size to img0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
for *xyxy, conf, cls in reversed(det):
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=3)
print(f'Inferencing and Processing Done. ({time.time() - t0:.3f}s)')
# return results
return img0
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
size = (int(width), int(height))
# main
out = cv2.VideoWriter('C:/users/elena/Desktop/개연2/yolov7-tiny.avi', fourcc, fps, size)
check_requirements(exclude=('pycocotools', 'thop'))
with torch.no_grad():
while(True):
ret, frame = cap.read()
result = detect(frame)
cv2.imshow('pred_image', result)
out.write(result)
if cv2.waitKey(1) == ord('q'):
break
out.release()