-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.py
74 lines (65 loc) · 2.04 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
import tqdm
import cv2
import argparse
import numpy as np
import torch
import human_inst_seg
# 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")
parser.add_argument(
'--images', default="", nargs="*")
parser.add_argument(
'--videos', default="", nargs="*")
parser.add_argument(
'--loop', action="store_true")
parser.add_argument(
'--vis', action="store_true")
args = parser.parse_args()
def visulization(data):
image, bboxes, probs = data
image = torch.cat([
image[:, 0:3], image[:, 0:3]*image[:, 3:4]], dim=3)
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)
seg_engine = human_inst_seg.Segmentation()
seg_engine.eval()
if args.camera:
data_stream = streamer.CaptureStreamer()
elif len(args.videos) > 0:
data_stream = streamer.VideoListStreamer(
args.videos * (10000 if args.loop else 1))
elif len(args.images) > 0:
data_stream = streamer.ImageListStreamer(
args.images * (10000 if args.loop else 1))
loader = torch.utils.data.DataLoader(
data_stream,
batch_size=1,
num_workers=1,
pin_memory=False,
)
try:
# no vis: ~ 50 fps
for data in tqdm.tqdm(loader):
outputs, bboxes, probs = seg_engine(data)
if args.vis:
visulization([outputs, bboxes, probs])
except Exception as e:
print (e)
del data_stream