forked from bwsw/rt-motion-detection-opencv-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker.py
29 lines (22 loc) · 988 Bytes
/
packer.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
import numpy as np
from bounding_boxes import pack
def pack_images(frame, boxes, width, height, box_filter=lambda x: True):
list_rects = list()
list_bins = list()
for b in boxes:
if box_filter(b):
list_rects.append((b[2] - b[0], b[3] - b[1], b,))
list_bins.append((width, height, int((frame.shape[0] + height) / height + 1) * int((frame.shape[1] + width) / width + 1),))
rectangles = pack(rects=list_rects, bins=list_bins)
return copy_images(frame, rectangles, height, width)
def copy_images(frame, rectangles, height, width):
results = []
box_map = []
for rect in rectangles:
b, x, y, w, h, rid = rect
if b + 1 > len(results):
results.append(np.zeros((height, width, 3), dtype=frame.dtype))
img = frame[rid[1]:rid[3], rid[0]:rid[2]]
results[b][x:x + w, y:y + h] = img
box_map.append((b, (x, y, x + w, y + h), (rid[0], rid[1], rid[2], rid[3])))
return results, box_map