-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathwebcam_demo.py
177 lines (147 loc) · 5.46 KB
/
webcam_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
# --------------------------------------------------------
# THOR
# Licensed under The MIT License
# Written by Axel Sauer ([email protected])
# --------------------------------------------------------
import pdb
import argparse, cv2, os
import numpy as np
import sys
from imutils.video import FPS
import json
from trackers.tracker import SiamFC_Tracker, SiamRPN_Tracker, SiamMask_Tracker
from benchmark.bench_utils.bbox_helper import cxy_wh_2_rect, xyxy_to_xywh
# constants
BRIGHTGREEN = [102, 255, 0]
RED = [0, 0, 255]
YELLOW = [0, 255, 255]
np.set_printoptions(precision=6, suppress=True)
OUTPUT_WIDTH = 740
OUTPUT_HEIGHT = 555
PADDING = 2
parser = argparse.ArgumentParser(description='Webcam Test')
parser.add_argument('-t', '--tracker', dest='tracker', required=True,
help='Name of the tracker [SiamFC, SiamRPN, SiamMask]')
parser.add_argument('--vanilla', action='store_true',
help='run the tracker without memory')
parser.add_argument('-v', '--viz', action='store_true',
help='whether visualize result')
parser.add_argument('--verbose', action='store_true',
help='print info about temp mem')
parser.add_argument('--lb_type', type=str, default='ensemble',
help='Specify the type of lower bound [dynamic, ensemble]')
drawnBox = np.zeros(4)
boxToDraw = np.zeros(4)
mousedown = False
mouseupdown = False
initialize = False
def on_mouse(event, x, y, flags, params):
global mousedown, mouseupdown, drawnBox, boxToDraw, initialize, boxToDraw_xywh
if event == cv2.EVENT_LBUTTONDOWN:
drawnBox[[0,2]] = x
drawnBox[[1,3]] = y
mousedown = True
mouseupdown = False
elif mousedown and event == cv2.EVENT_MOUSEMOVE:
drawnBox[2] = x
drawnBox[3] = y
elif event == cv2.EVENT_LBUTTONUP:
drawnBox[2] = x
drawnBox[3] = y
mousedown = False
mouseupdown = True
initialize = True
boxToDraw = drawnBox.copy()
boxToDraw[[0, 2]] = np.sort(boxToDraw[[0, 2]])
boxToDraw[[1, 3]] = np.sort(boxToDraw[[1, 3]])
boxToDraw_xywh = xyxy_to_xywh(boxToDraw)
def bb_on_im(im, location, mask):
location = [int(l) for l in location] #
if len(mask):
im[:, :, 2] = mask * 255 + (1 - mask) * im[:, :, 2]
# prediction
cv2.rectangle(im, (location[0], location[1]),
(location[0] + location[2], location[1] + location[3]),
(0, 255, 255), 3)
return im
def show_webcam(tracker, mirror=False, viz=False):
global initialize
vs = cv2.VideoCapture(0)
cv2.namedWindow('Webcam', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Webcam', OUTPUT_WIDTH, OUTPUT_HEIGHT)
cv2.setMouseCallback('Webcam', on_mouse, 0)
outputBoxToDraw = None
bbox = None
fps = None
state = None
mask = []
# loop over video stream ims
while True:
_, im = vs.read()
if mirror:
im = cv2.flip(im, 1)
if mousedown:
(x1, y1, x2, y2) = [int(l) for l in boxToDraw]
cv2.rectangle(im, (x1, y1), (x2, y2),
BRIGHTGREEN, PADDING)
elif mouseupdown:
if initialize:
init_pos = boxToDraw_xywh[[0, 1]]
init_sz = boxToDraw_xywh[[2, 3]]
state = tracker.setup(im, init_pos, init_sz)
initialize = False
fps = FPS().start()
else:
state = tracker.track(im, state)
location = cxy_wh_2_rect(state['target_pos'], state['target_sz'])
(cx, cy, w, h) = [int(l) for l in location]
fps.update()
fps.stop()
# Display the image
info = [
("Score:", f"{state['score']:.4f}"),
("FPS:", f"{fps.fps():.2f}"),
]
if not state['score'] > 0.8:
info.insert(0, ("Object lost since", ""))
else:
if 'mask' in state.keys():
mask = state['mask'] > state['p'].seg_thr
im = bb_on_im(im, location, mask)
for (i, (k, v)) in enumerate(info):
text = "{}: {}".format(k, v)
cv2.putText(im, text, (10, OUTPUT_HEIGHT - ((i * 20) + 20)),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
cv2.imshow("Webcam", im)
# check for escape key
key = cv2.waitKey(1)
if key==27 or key==1048603:
break
# release the pointer
cv2.destroyAllWindows()
def load_cfg(args):
json_path = f"configs/{args.tracker}/VOT2018_"
if args.vanilla:
json_path += "vanilla.json"
else:
json_path += f"THOR_{args.lb_type}.json"
cfg = json.load(open(json_path))
return cfg
if __name__ == '__main__':
args = parser.parse_args()
cfg = load_cfg(args)
cfg['THOR']['viz'] = args.viz
cfg['THOR']['verbose'] = args.verbose
print("[INFO] Initializing the tracker.")
if args.tracker == 'SiamFC':
tracker = SiamFC_Tracker(cfg)
elif args.tracker == 'SiamRPN':
tracker = SiamRPN_Tracker(cfg)
elif args.tracker == 'SiamMask':
tracker = SiamMask_Tracker(cfg)
elif args.tracker == 'SiamRPN_PP':
tracker = SiamRPN_PP_Tracker(cfg)
else:
raise ValueError(f"Tracker {args.tracker} does not exist.")
print("[INFO] Starting video stream.")
show_webcam(tracker, mirror=True, viz=args.viz)