-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
103 lines (78 loc) · 2.59 KB
/
utils.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
import time
import cv2
import depthai as dai
import numpy as np
def unix_time() -> int:
return int(time.time() * 1000.0 * -1)
def frame_norm(frame: object, bbox: tuple) -> np.ndarray:
"""
This create a norm
Parameters
----------
frame: np.ndarray
Object created with cv2.VideoCapture().read()
bbox: tuple
Tuple
Returns
-------
np.ndarray
Numpy NDArray object that represent the transformation of the arr after
apply shape
"""
norm_vals = np.full(len(bbox), frame.shape[0])
norm_vals[::2] = frame.shape[1]
return (np.clip(np.array(bbox), 0, 1) * norm_vals).astype(int)
def to_planar(arr: np.ndarray, shape: tuple) -> np.ndarray:
"""
Transform a np.ndarray (cv2.VideoCapture().read())
Parameters
----------
frame: np.ndarray
Object created with cv2.VideoCapture().read()
shape: tuple
Tuple
Returns
-------
np.ndarray
Numpy NDArray object that represent the transformation of the arr after
apply shape
"""
return cv2.resize(arr, shape).transpose(2, 0, 1).flatten()
def to_depthai_frame(frame: np.ndarray, size: tuple) -> dai.ImgFrame:
"""
Transform a np.ndarray (cv2.VideoCapture().read()) frame to a depthai.ImgFrame
changing it to BGR888p format given the size tuple represented by (width,height)
Parameters
----------
frame: np.ndarray
Object created with cv2.VideoCapture().read()
size: tuple
Tuple with the width and heigh used to create the depthai.ImgFrame
"""
time_stamp = time.monotonic()
img = dai.ImgFrame()
img.setData(to_planar(frame, size))
img.setTimestamp(time_stamp)
img.setType(dai.RawImgFrame.Type.BGR888p)
img.setWidth(size[0])
img.setHeight(size[1])
return img
def send_frame_to_queue(video_capture: np.ndarray, send_queue: dai.DataInputQueue, size: tuple) -> None:
"""
Send frame read from cv2.VideoCapture() to a dai.DataInputQueue after transform this to a depthai.ImgFrame()
Parameters
----------
video_capture: np.ndarray
Object created with cv2.VideoCapture()
send_queue: dai.DataInputQueue
Object created with depthai.pipeline.create(dai.node.XLinkIn)
size: tuple, required
Tuple with the width and heigh used to create the depthai.ImgFrame
before send this to send_queue
"""
success, frame = video_capture.read()
if success:
sequence = unix_time()
img_frame = to_depthai_frame(frame, size)
img_frame.setSequenceNum(sequence)
send_queue.send(img_frame)