-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdemo.py
85 lines (70 loc) · 2.4 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
import glob
import tqdm
import cv2
import argparse
import numpy as np
import torch
import human_det
# this can be install by:
# pip install git+https://github.com/Project-Splinter/streamer_pytorch --upgrade
import streamer_pytorch as streamer
parser = argparse.ArgumentParser(description='.')
parser.add_argument(
'--camera', action="store_true", help="whether to use webcam.")
parser.add_argument(
'--images', default="", nargs="*", help="paths of image.")
parser.add_argument(
'--image_folder', default=None, help="path of image folder.")
parser.add_argument(
'--videos', default="", nargs="*", help="paths of video.")
parser.add_argument(
'--loop', action="store_true", help="whether to repeat images/video.")
parser.add_argument(
'--vis', action="store_true", help="whether to visualize.")
args = parser.parse_args()
def visulization(data):
image, bboxes, probs = data
probs = probs.unsqueeze(3)
bboxes = (bboxes * probs).sum(dim=1, keepdim=True) / probs.sum(dim=1, keepdim=True)
window = image[0].cpu().numpy().transpose(1, 2, 0)
window = (window * 0.5 + 0.5) * 255.0
window = np.uint8(window).copy()
bbox = bboxes[0, 0, 0].cpu().numpy()
window = cv2.rectangle(
window,
(int(bbox[0]), int(bbox[1])),
(int(bbox[2]), int(bbox[3])),
(255,0,0), 2)
window = cv2.cvtColor(window, cv2.COLOR_BGR2RGB)
window = cv2.resize(window, (0, 0), fx=2, fy=2)
cv2.imshow('window', window)
cv2.waitKey(30)
det_engine = human_det.Detection()
if args.camera:
data_stream = streamer.CaptureStreamer(pad=False)
elif len(args.videos) > 0:
data_stream = streamer.VideoListStreamer(
args.videos * (10 if args.loop else 1))
elif len(args.images) > 0:
data_stream = streamer.ImageListStreamer(
args.images * (10000 if args.loop else 1))
elif args.image_folder is not None:
images = sorted(glob.glob(args.image_folder+'/*.jpg'))
images += sorted(glob.glob(args.image_folder+'/*.png'))
data_stream = streamer.ImageListStreamer(
images * (10 if args.loop else 1))
loader = torch.utils.data.DataLoader(
data_stream,
batch_size=1,
num_workers=1,
pin_memory=False,
)
try:
# no vis: ~ 70 fps
for data in tqdm.tqdm(loader):
bboxes, probs = det_engine(data)
if args.vis:
visulization([data, bboxes, probs])
except Exception as e:
print (e)
del data_stream