forked from bwsw/rt-motion-detection-opencv-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
71 lines (54 loc) · 2.1 KB
/
sample.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
import cv2
import numpy as np
from time import time
from detector import MotionDetector
from packer import pack_images
from numba import jit
@jit(nopython=True)
def filter_fun(b):
return ((b[2] - b[0]) * (b[3] - b[1])) > 1000
if __name__ == "__main__":
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
detector = MotionDetector(bg_history=20,
brightness_discard_level=25,
bg_subs_scale_percent=0.1,
group_boxes=True,
expansion_step=5)
# group_boxes=True can be used if one wants to get less boxes, which include all overlapping boxes
b_height = 512
b_width = 512
res = []
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if frame is None:
break
begin = time()
boxes = detector.detect(frame)
# boxes hold all boxes around motion parts
## this code cuts motion areas from initial image and
## fills "bins" of 512x512 with such motion areas.
##
# results = []
# if boxes:
# results, box_map = pack_images(frame=frame, boxes=boxes, width=b_width, height=b_height,
# box_filter=filter_fun)
# box_map holds list of mapping between image placement in packed bins and original boxes
## end
for b in boxes:
cv2.rectangle(frame, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 1)
end = time()
it = (end - begin) * 1000
res.append(it)
print("StdDev: %.4f" % np.std(res), "Mean: %.4f" % np.mean(res), "Last: %.4f" % it, "Boxes found: ", len(boxes))
idx = 0
# for r in results:
# idx += 1
# cv2.imshow('packed_frame_%d' % idx, r)
cv2.imshow('last_frame', frame)
cv2.imshow('detect_frame', detector.detection_boxed)
cv2.imshow('diff_frame', detector.color_movement)
if cv2.waitKey(1) & 0xFF == ord('q'):
break