This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbbox_trigger.py
More file actions
executable file
·138 lines (112 loc) · 5.03 KB
/
Copy pathbbox_trigger.py
File metadata and controls
executable file
·138 lines (112 loc) · 5.03 KB
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
from skimage.measure import compare_ssim
import cv2
from utils import crop_image
import numpy as np
import collections
class BboxTrigger:
"""
This class is bounding box trigger for a door to build the gallery
Attributes:
_camera_id (str): name of the camera location
_ref_img (cv2 image): reference image (ex. Closed door)
_open_thresh (int): threshold value for opening a door
_close_thresh (int): threshold value for closing a door
_check_coords (list): 2D list of coordinates to check
_sample_coords (list): 2D list of trigger coordinates
_detector (detector.py): object detector (default FasterRCNN)
_check (int): flag for determining when triggering is in process
"""
def __init__(self, camera_id, ref_img, open_thresh, close_thresh,
check_coords, sample_coords, detector):
"""
the constructor for BboxTrigger class
Parameters:
_camera_id (str): name of the camera location
_ref_img (cv2 image): reference image (ex. Closed door)
_open_thresh (int): threshold value for opening a door
_close_thresh (int): threshold value for closing a door
_check_coords (list): 2D list of coordinates to check
_sample_coords (list): 2D list of trigger coordinates
_detector (detector.py): object detector (default FasterRCNN)
"""
self._camera_id = camera_id
self._open_thresh = open_thresh
self._close_thresh = close_thresh
self._check_coords = check_coords
self._sample_coords = sample_coords
self._detector = detector # ideally this is not here in the future either
self._ref_img = cv2.cvtColor(crop_image(ref_img, check_coords),
cv2.COLOR_BGR2GRAY)
self._check = 0
def update(self, frames):
"""
Given a image, find when to trigger and return bounding boxes of people in the trigger region
Parameters:
frames (dict): dictionary of frames from all the cameras
Returns:
bboxes (ndarry): bounding boxes of detected objects
sampimg (ndarray): cropped image of the trigger region
"""
img = frames[self._camera_id]
chkimg = cv2.cvtColor(crop_image(img, self._check_coords),
cv2.COLOR_BGR2GRAY)
score, diff = compare_ssim(self._ref_img, chkimg, full=True)
if self._check == 1:
if score > self._close_thresh:
sampimg = crop_image(img, self._sample_coords)
bboxes, scores = self._detector.get_bboxes(sampimg)
self._check = 0
return True, bboxes, sampimg
else:
if score < self._open_thresh:
self._check = 1
return False, None, None
"use is completely different from BboxTrigger"
class VectorTrigger:
"""
This class is for a Line trigger
"""
def __init__(self, video, vector, inpt, length_thresh, frame_offset):
tmpvec = np.random.randn(2)
maxx = max(vector[2], vector[0])
minx = min(vector[2], vector[0])
maxy = max(vector[3], vector[1])
miny = min(vector[3], vector[1])
invec = np.array([maxx - minx, maxy - miny])
# tmpvec -= tmpvec.dot(invec) * invec
# print(tmpvec.dot(invec))
# self.ovector = tmpvec / np.linalg.norm(tmpvec)
self.ovector = np.array([invec[1], -1 * invec[0]])
self.midpt = np.array([(vector[0] + vector[2])/2, (vector[1] + vector[3])/2])
# 1 is in
if np.sign(self.ovector.dot(self.midpt - inpt)) != 1: self.ovector = np.zeros(2) - self.ovector
self.length_thresh = length_thresh
self.frame_offset = frame_offset
self.video_oi = video
self.flags = collections.defaultdict(float)
self.prev_val = collections.defaultdict(float)
def update(self, peoplebboxes):
"""
:param peoplebboxes: numpy array n x 5 with columns in x1, y1, x2, y2, id order
:return: indexes to capture
"""
retval = list()
feetpoints = np.array([(peoplebboxes[:,0] + peoplebboxes[:,2])/2, np.max(peoplebboxes[:, [1,3]], 1)]).transpose()
displace_vects = self.midpt - feetpoints
disp_mags = np.linalg.norm(displace_vects, axis=1)
inout = np.sign(displace_vects.dot(self.ovector))
for i, (val, bboxes) in enumerate(zip(inout, peoplebboxes)):
if disp_mags[i] > self.length_thresh:
continue
id = bboxes[4]
if self.flags[id] >= 1: self.flags[id] += 1
if self.flags[id] >= self.frame_offset:
retval.append(i)
self.flags[id] = 0
# -1 - 1 is entering
if self.prev_val[id] - val == -2:
self.flags[id] = 1
if val == 0:
self.prev_val[id] = -1
else: self.prev_val[id] = val
return retval