-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathutils.py
More file actions
159 lines (133 loc) · 5.67 KB
/
Copy pathutils.py
File metadata and controls
159 lines (133 loc) · 5.67 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# ******************************************************************************
# Copyright (c) 2024 Orbbec 3D Technology, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ******************************************************************************
from typing import Any, Optional, Union
import cv2
import numpy as np
from pyorbbecsdk import (
Device,
FormatConvertFilter,
OBConvertFormat,
OBFormat,
OBSensorType,
VideoFrame,
)
def is_astra_mini_device(vid: int, pid: int) -> bool:
if (vid == 0x2BC5) and (pid == 0x069D or pid == 0x065B or pid == 0x065E):
return True
return False
def is_gemini305_device(vid: int, pid: int) -> bool:
if (vid == 0x2BC5) and (pid in (0x0840, 0x0841, 0x0842, 0x0843, 0x0845)):
return True
return False
def is_gemini305g_device(vid: int, pid: int, connection_type: str) -> bool:
return is_gemini305_device(vid, pid) and connection_type == "GMSL2"
def is_lidar_device(device: Device) -> bool:
sensor_list = device.get_sensor_list()
count = sensor_list.get_count()
for index in range(count):
sensor_type = sensor_list.get_sensor_by_index(index).get_type()
if sensor_type == OBSensorType.LIDAR_SENSOR:
return True
return False
def yuyv_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
yuyv = frame.reshape((height, width, 2))
bgr_image = cv2.cvtColor(yuyv, cv2.COLOR_YUV2BGR_YUY2)
return bgr_image
def uyvy_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
uyvy = frame.reshape((height, width, 2))
bgr_image = cv2.cvtColor(uyvy, cv2.COLOR_YUV2BGR_UYVY)
return bgr_image
def i420_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
y = frame[0:height, :]
u = frame[height : height + height // 4].reshape(height // 2, width // 2)
v = frame[height + height // 4 :].reshape(height // 2, width // 2)
yuv_image = cv2.merge([y, u, v])
bgr_image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR_I420)
return bgr_image
def nv21_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
y = frame[0:height, :]
uv = frame[height : height + height // 2].reshape(height // 2, width)
yuv_image = cv2.merge([y, uv])
bgr_image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR_NV21)
return bgr_image
def nv12_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
y = frame[0:height, :]
uv = frame[height : height + height // 2].reshape(height // 2, width)
yuv_image = cv2.merge([y, uv])
bgr_image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR_NV12)
return bgr_image
def determine_convert_format(frame: VideoFrame):
if frame.get_format() == OBFormat.I420:
return OBConvertFormat.I420_TO_RGB888
elif frame.get_format() == OBFormat.MJPG:
return OBConvertFormat.MJPG_TO_RGB888
elif frame.get_format() == OBFormat.YUYV:
return OBConvertFormat.YUYV_TO_RGB888
elif frame.get_format() == OBFormat.NV21:
return OBConvertFormat.NV21_TO_RGB888
elif frame.get_format() == OBFormat.NV12:
return OBConvertFormat.NV12_TO_RGB888
elif frame.get_format() == OBFormat.UYVY:
return OBConvertFormat.UYVY_TO_RGB888
else:
return None
def frame_to_rgb_frame(frame: VideoFrame) -> Union[Optional[VideoFrame], Any]:
if frame.get_format() == OBFormat.RGB:
return frame
convert_format = determine_convert_format(frame)
if convert_format is None:
print("Unsupported format")
return None
print("covert format: {}".format(convert_format))
convert_filter = FormatConvertFilter()
convert_filter.set_format_convert_format(convert_format)
rgb_frame = convert_filter.process(frame)
if rgb_frame is None:
print("Convert {} to RGB failed".format(frame.get_format()))
return rgb_frame
def frame_to_bgr_image(frame: VideoFrame) -> Union[Optional[np.array], Any]:
width = frame.get_width()
height = frame.get_height()
color_format = frame.get_format()
data = np.asanyarray(frame.get_data())
image = np.zeros((height, width, 3), dtype=np.uint8)
if color_format == OBFormat.RGB:
image = np.resize(data, (height, width, 3))
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
elif color_format == OBFormat.BGR:
image = np.resize(data, (height, width, 3))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
elif color_format == OBFormat.YUYV:
image = np.resize(data, (height, width, 2))
image = cv2.cvtColor(image, cv2.COLOR_YUV2BGR_YUYV)
elif color_format == OBFormat.MJPG:
image = cv2.imdecode(data, cv2.IMREAD_COLOR)
elif color_format == OBFormat.I420:
image = i420_to_bgr(data, width, height)
return image
elif color_format == OBFormat.NV12:
image = nv12_to_bgr(data, width, height)
return image
elif color_format == OBFormat.NV21:
image = nv21_to_bgr(data, width, height)
return image
elif color_format == OBFormat.UYVY:
image = np.resize(data, (height, width, 2))
image = cv2.cvtColor(image, cv2.COLOR_YUV2BGR_UYVY)
else:
print("Unsupported color format: {}".format(color_format))
return None
return image