forked from ultralytics/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.py
176 lines (152 loc) · 7.88 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from pathlib import Path
import numpy as np
import argparse
import time
import os
import torch.backends.cudnn as cudnn
import torch
import cv2
from emotion import detect_emotion, init
from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, \
scale_coords, set_logging, create_folder
from utils.plots import plot_one_box
from utils.torch_utils import select_device, time_synchronized
def detect(opt):
source, view_img, imgsz, nosave, show_conf, save_path, show_fps = opt.source, not opt.hide_img, opt.img_size, opt.no_save, not opt.hide_conf, opt.output_path, opt.show_fps
webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith(
('rtsp://', 'rtmp://', 'http://', 'https://'))
# Directories
create_folder(save_path)
# Initialize
set_logging()
device = select_device(opt.device)
init(device)
half = device.type != 'cpu' # half precision only supported on CUDA
# Load model
model = attempt_load("weights/yolov7-tiny.pt", map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check img_size
if half:
model.half() # to FP16
# Set Dataloader
vid_path, vid_writer = None, None
if webcam:
view_img = check_imshow()
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=imgsz, stride=stride)
else:
dataset = LoadImages(source, img_size=imgsz, stride=stride)
# Get names and colors
names = model.module.names if hasattr(model, 'module') else model.names
colors = ((0,52,255),(121,3,195),(176,34,118),(87,217,255),(69,199,79),(233,219,155),(203,139,77),(214,246,255))
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
t0 = time.time()
for path, img, im0s, vid_cap in dataset:
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
t1 = time_synchronized()
pred = model(img, augment=opt.augment)[0]
# Apply NMS
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, agnostic=opt.agnostic_nms)
t2 = time_synchronized()
# Process detections
for i, det in enumerate(pred): # detections per image
if webcam: # batch_size >= 1
p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count
else:
p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)
p = Path(p) # to Path
s += '%gx%g ' % img.shape[2:] # print string
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.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
images = []
for *xyxy, conf, cls in reversed(det):
#xyxy = [[x, y, w, h] for x, y, w, h in xyxy]
# xyxy = np.array(xyxy)
#xyxy = np.array(xyxy)
# xyxy = xyxy.tolist()
#x1, y1, x2, y2 = zip(*xyxy)
x1, y1, x2, y2 = xyxy[0], xyxy[1], xyxy[2], xyxy[3]
# x1, y1, x2, y2 = xyxy[:,:4]
images.append(im0.astype(np.uint8)[int(y1):int(y2), int(x1): int(x2)])
if images:
emotions = detect_emotion(images,show_conf)
# Write results
i = 0
#for *xyxy, conf, cls in reversed(det):
#for *xyxy, conf, cls in det[::-1]:
for *xyxy, conf, cls in reversed(det):
if view_img or not nosave:
# Add bbox to image with emotions on
label = emotions[i][0]
colour = colors[emotions[i][1]]
i += 1
plot_one_box(xyxy, im0, label=label, color=colour, line_thickness=opt.line_thickness)
# Stream results
if view_img:
display_img = cv2.resize(im0, (im0.shape[1]*2,im0.shape[0]*2))
cv2.imshow("Emotion Detection",display_img)
cv2.waitKey(1) # 1 millisecond
if not nosave:
# check what the output format is
ext = save_path.split(".")[-1]
if ext in ["mp4","avi"]:
# Save results (image with detections)
if vid_path != save_path: # new video
vid_path = save_path
if isinstance(vid_writer, cv2.VideoWriter):
vid_writer.release() # release previous video writer
if vid_cap: # video
fps = vid_cap.get(cv2.CAP_PROP_FPS)
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
else: # stream
fps, w, h = 30, im0.shape[1], im0.shape[0]
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
vid_writer.write(im0)
elif ext in ["bmp", "pbm", "pgm", "ppm", "sr", "ras", "jpeg", "jpg", "jpe", "jp2", "tiff", "tif", "png"]:
# save image
cv2.imwrite(save_path,im0)
else:
# save to folder
output_path = os.path.join(save_path,os.path.split(path)[1])
create_folder(output_path)
cv2.imwrite(output_path,im0)
if show_fps:
# calculate and display fps
print(f"FPS: {1/(time.time()-t0):.2f}"+" "*5,end="\r")
t0 = time.time()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--source', type=str, default='0', help='source') # file/folder, 0 for webcam
parser.add_argument('--img-size', type=int, default=512, help='inference size (pixels)')
parser.add_argument('--conf-thres', type=float, default=0.5, help='face confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--hide-img', action='store_true', help='hide results')
save = parser.add_mutually_exclusive_group()
save.add_argument('--output-path', default="output.mp4", help='save location')
save.add_argument('--no-save', action='store_true', help='do not save images/videos')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--line-thickness', default=2, type=int, help='bounding box thickness (pixels)')
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
parser.add_argument('--show-fps', default=False, action='store_true', help='print fps to console')
opt = parser.parse_args()
check_requirements(exclude=('pycocotools', 'thop'))
with torch.no_grad():
detect(opt=opt)