forked from roboflow/pytorch-YOLOv4
-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.py
198 lines (153 loc) · 5.66 KB
/
demo.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
'''
@Time : 20/04/25 15:49
@Author : huguanghao
@File : demo.py
@Noice :
@Modificattion :
@Author :
@Time :
@Detail :
'''
# import sys
# import time
# from PIL import Image, ImageDraw
# from models.tiny_yolo import TinyYoloNet
from tool.utils import *
from tool.darknet2pytorch import Darknet
import argparse
"""hyper parameters"""
use_cuda = True
num_classes = 80
if num_classes == 20:
namesfile = 'data/voc.names'
elif num_classes == 80:
namesfile = 'data/coco.names'
else:
namesfile = 'data/x.names'
def detect(cfgfile, weightfile, imgfile):
m = Darknet(cfgfile)
m.print_network()
m.load_weights(weightfile)
print('Loading weights from %s... Done!' % (weightfile))
if use_cuda:
m.cuda()
img = Image.open(imgfile).convert('RGB')
sized = img.resize((m.width, m.height))
for i in range(2):
start = time.time()
boxes = do_detect(m, sized, 0.5, num_classes, 0.4, use_cuda)
finish = time.time()
if i == 1:
print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))
class_names = load_class_names(namesfile)
plot_boxes(img, boxes, 'predictions.jpg', class_names)
def detect_imges(cfgfile, weightfile, imgfile_list=['data/dog.jpg', 'data/giraffe.jpg']):
m = Darknet(cfgfile)
m.print_network()
m.load_weights(weightfile)
print('Loading weights from %s... Done!' % (weightfile))
if use_cuda:
m.cuda()
imges = []
imges_list = []
for imgfile in imgfile_list:
img = Image.open(imgfile).convert('RGB')
imges_list.append(img)
sized = img.resize((m.width, m.height))
imges.append(np.expand_dims(np.array(sized), axis=0))
images = np.concatenate(imges, 0)
for i in range(2):
start = time.time()
boxes = do_detect(m, images, 0.5, num_classes, 0.4, use_cuda)
finish = time.time()
if i == 1:
print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))
class_names = load_class_names(namesfile)
for i,(img,box) in enumerate(zip(imges_list,boxes)):
plot_boxes(img, box, 'predictions{}.jpg'.format(i), class_names)
def detect_cv2(cfgfile, weightfile, imgfile):
import cv2
m = Darknet(cfgfile)
m.print_network()
m.load_weights(weightfile)
print('Loading weights from %s... Done!' % (weightfile))
if use_cuda:
m.cuda()
img = cv2.imread(imgfile)
sized = cv2.resize(img, (m.width, m.height))
sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)
for i in range(2):
start = time.time()
boxes = do_detect(m, sized, 0.5, m.num_classes, 0.4, use_cuda)
finish = time.time()
if i == 1:
print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))
class_names = load_class_names(namesfile)
plot_boxes_cv2(img, boxes, savename='predictions.jpg', class_names=class_names)
def detect_cv2_camera(cfgfile, weightfile):
import cv2
m = Darknet(cfgfile)
m.print_network()
m.load_weights(weightfile)
print('Loading weights from %s... Done!' % (weightfile))
if use_cuda:
m.cuda()
cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("./test.mp4")
cap.set(3, 1280)
cap.set(4, 720)
print("Starting the YOLO loop...")
while True:
ret, img = cap.read()
sized = cv2.resize(img, (m.width, m.height))
sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)
start = time.time()
boxes = do_detect(m, sized, 0.5, num_classes, 0.4, use_cuda)
finish = time.time()
print('Predicted in %f seconds.' % (finish - start))
class_names = load_class_names(namesfile)
result_img = plot_boxes_cv2(img, boxes, savename=None, class_names=class_names)
cv2.imshow('Yolo demo', result_img)
cv2.waitKey(1)
cap.release()
def detect_skimage(cfgfile, weightfile, imgfile):
from skimage import io
from skimage.transform import resize
m = Darknet(cfgfile)
m.print_network()
m.load_weights(weightfile)
print('Loading weights from %s... Done!' % (weightfile))
if use_cuda:
m.cuda()
img = io.imread(imgfile)
sized = resize(img, (m.width, m.height)) * 255
for i in range(2):
start = time.time()
boxes = do_detect(m, sized, 0.5, m.num_classes, 0.4, use_cuda)
finish = time.time()
if i == 1:
print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))
class_names = load_class_names(namesfile)
plot_boxes_cv2(img, boxes, savename='predictions.jpg', class_names=class_names)
def get_args():
parser = argparse.ArgumentParser('Test your image or video by trained model.')
parser.add_argument('-cfgfile', type=str, default='./cfg/yolov4.cfg',
help='path of cfg file', dest='cfgfile')
parser.add_argument('-weightfile', type=str,
default='./checkpoints/Yolov4_epoch1.pth',
help='path of trained model.', dest='weightfile')
parser.add_argument('-imgfile', type=str,
default='./data/mscoco2017/train2017/190109_180343_00154162.jpg',
help='path of your image file.', dest='imgfile')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = get_args()
if args.imgfile:
detect(args.cfgfile, args.weightfile, args.imgfile)
# detect_imges(args.cfgfile, args.weightfile)
# detect_cv2(args.cfgfile, args.weightfile, args.imgfile)
# detect_skimage(args.cfgfile, args.weightfile, args.imgfile)
else:
detect_cv2_camera(args.cfgfile, args.weightfile)