diff --git a/rerun_py/depthai_viewer/_backend/device.py b/rerun_py/depthai_viewer/_backend/device.py index 74ef15cb3b47..f22388e80518 100644 --- a/rerun_py/depthai_viewer/_backend/device.py +++ b/rerun_py/depthai_viewer/_backend/device.py @@ -9,7 +9,6 @@ from depthai_sdk import OakCamera from depthai_sdk.classes.packet_handlers import ComponentOutput from depthai_sdk.components import CameraComponent, NNComponent, StereoComponent -from depthai_sdk.components.tof_component import Component from depthai_sdk.components.camera_helper import ( getClosestIspScale, ) @@ -32,7 +31,6 @@ compare_dai_camera_configs, get_size_from_resolution, size_to_resolution, - ALL_NEURAL_NETWORKS, ) from depthai_viewer._backend.messages import ( ErrorMessage, @@ -70,8 +68,7 @@ def update(self) -> None: class Device: id: str - intrinsic_matrix: Dict[Tuple[dai.CameraBoardSocket, - int, int], NDArray[np.float32]] = {} + intrinsic_matrix: Dict[Tuple[dai.CameraBoardSocket, int, int], NDArray[np.float32]] = {} calibration_data: Optional[dai.CalibrationHandler] = None use_encoding: bool = False store: Store @@ -92,8 +89,7 @@ def __init__(self, device_id: str, store: Store): self.id = device_id self.set_oak(OakCamera(device_id)) self.store = store - self._packet_handler = PacketHandler( - self.store, self.get_intrinsic_matrix) + self._packet_handler = PacketHandler(self.store, self.get_intrinsic_matrix) print("Oak cam: ", self._oak) # self.start = time.time() # self._profiler.enable() @@ -109,15 +105,13 @@ def is_closed(self) -> bool: def get_intrinsic_matrix(self, board_socket: dai.CameraBoardSocket, width: int, height: int) -> NDArray[np.float32]: if self.intrinsic_matrix.get((board_socket, width, height)) is not None: - # type: ignore[return-value] - return self.intrinsic_matrix.get((board_socket, width, height)) + return self.intrinsic_matrix.get((board_socket, width, height)) # type: ignore[return-value] if self.calibration_data is None: raise Exception("Missing calibration data!") M_right = self.calibration_data.getCameraIntrinsics( # type: ignore[union-attr] board_socket, dai.Size2f(width, height) ) - self.intrinsic_matrix[(board_socket, width, height)] = np.array( - M_right).reshape(3, 3) + self.intrinsic_matrix[(board_socket, width, height)] = np.array(M_right).reshape(3, 3) return self.intrinsic_matrix[(board_socket, width, height)] def _get_possible_stereo_pairs_for_cam( @@ -139,13 +133,11 @@ def _get_possible_stereo_pairs_for_cam( possible_stereo_pairs = [] if cam.name == "right": possible_stereo_pairs.extend( - [features.socket for features in filter( - lambda c: c.name == "left", connected_camera_features)] + [features.socket for features in filter(lambda c: c.name == "left", connected_camera_features)] ) elif cam.name == "left": possible_stereo_pairs.extend( - [features.socket for features in filter( - lambda c: c.name == "right", connected_camera_features)] + [features.socket for features in filter(lambda c: c.name == "right", connected_camera_features)] ) else: possible_stereo_pairs.extend( @@ -155,8 +147,7 @@ def _get_possible_stereo_pairs_for_cam( if camera != cam and all( map( - lambda confs: compare_dai_camera_configs( - confs[0], confs[1]), + lambda confs: compare_dai_camera_configs(confs[0], confs[1]), zip(camera.configs, cam.configs), ) ) @@ -185,8 +176,7 @@ def get_device_properties(self) -> DeviceProperties: else XLinkConnection.USB, mxid=device_info.mxid, ) - device_properties = DeviceProperties( - id=self.id, imu=imu, info=device_info) + device_properties = DeviceProperties(id=self.id, imu=imu, info=device_info) try: calib = self._oak.device.readCalibration2() left_cam = calib.getStereoLeftCameraId() @@ -200,8 +190,7 @@ def get_device_properties(self) -> DeviceProperties: except RuntimeError: print("No calibration found while trying to fetch the default stereo pair.") - ordered_resolutions = list( - sorted(size_to_resolution.keys(), key=lambda res: res[0] * res[1])) + ordered_resolutions = list(sorted(size_to_resolution.keys(), key=lambda res: res[0] * res[1])) for cam in connected_cam_features: prioritized_type = cam.supportedTypes[0] biggest_width_height = [ @@ -214,8 +203,7 @@ def get_device_properties(self) -> DeviceProperties: biggest_width, biggest_height = biggest_width_height[0] if cam.supportedTypes[0] == dai.CameraSensorType.TOF: - all_supported_resolutions = [ - size_to_resolution[(biggest_width, biggest_height)]] + all_supported_resolutions = [size_to_resolution[(biggest_width, biggest_height)]] else: all_supported_resolutions = list( filter( @@ -235,8 +223,7 @@ def get_device_properties(self) -> DeviceProperties: max_fps=60, resolutions=all_supported_resolutions, supported_types=cam.supportedTypes, - stereo_pairs=self._get_possible_stereo_pairs_for_cam( - cam, connected_cam_features), + stereo_pairs=self._get_possible_stereo_pairs_for_cam(cam, connected_cam_features), name=cam.name.capitalize(), ) ) @@ -268,7 +255,8 @@ def reconnect_to_oak(self) -> Message: while time.time() - timeout_start < 10: available_devices = [ # type: ignore[call-arg] - device.getMxId() for device in dai.Device.getAllAvailableDevices() + device.getMxId() + for device in dai.Device.getAllAvailableDevices() ] if self.id in available_devices: break @@ -282,8 +270,7 @@ def reconnect_to_oak(self) -> Message: return ErrorMessage("Failed to create oak camera") def _get_component_by_socket(self, socket: dai.CameraBoardSocket) -> Optional[CameraComponent]: - component = list( - filter(lambda c: c.node.getBoardSocket() == socket, self._cameras)) + component = list(filter(lambda c: c.node.getBoardSocket() == socket, self._cameras)) if not component: return None return component[0] @@ -292,8 +279,7 @@ def _get_camera_config_by_socket( self, config: PipelineConfiguration, socket: dai.CameraBoardSocket ) -> Optional[CameraConfiguration]: print("Getting cam by socket: ", socket, " Cameras: ", config.cameras) - camera = list( - filter(lambda c: c.board_socket == socket, config.cameras)) + camera = list(filter(lambda c: c.board_socket == socket, config.cameras)) if not camera: return None return camera[0] @@ -319,16 +305,14 @@ def _create_auto_pipeline_config(self, config: PipelineConfiguration) -> Message if cam.name == "rgb": # By convention rgb_cam_socket = cam.socket resolution = ( - CameraSensorResolution.THE_1080_P if cam.width >= 1920 else size_to_resolution[( - cam.width, cam.height)] + CameraSensorResolution.THE_1080_P if cam.width >= 1920 else size_to_resolution[(cam.width, cam.height)] ) resolution = CameraSensorResolution.THE_1200_P if cam.height == 1200 else resolution preferred_type = cam.supportedTypes[0] if preferred_type == dai.CameraSensorType.TOF: has_tof = True config.cameras.append( - CameraConfiguration( - resolution=resolution, kind=preferred_type, board_socket=cam.socket, name=cam.name) + CameraConfiguration(resolution=resolution, kind=preferred_type, board_socket=cam.socket, name=cam.name) ) # 2. Create stereo depth if not has_tof: @@ -351,8 +335,7 @@ def _create_auto_pipeline_config(self, config: PipelineConfiguration) -> Message # Try to find a color camera config nnet_cam_sock = next( filter( - # type: ignore[arg-type,union-attr] - lambda cam: cam.kind == dai.CameraSensorType.COLOR, + lambda cam: cam.kind == dai.CameraSensorType.COLOR, # type: ignore[arg-type,union-attr] config.cameras, ), None, @@ -377,8 +360,7 @@ def update_pipeline(self, runtime_only: bool) -> Message: if self._oak.device.isPipelineRunning(): if runtime_only: if config.depth is not None: - self._stereo.control.send_controls( - config.depth.to_runtime_controls()) + self._stereo.control.send_controls(config.depth.to_runtime_controls()) return InfoMessage("") return ErrorMessage("Depth is disabled, can't send runtime controls!") print("Cam running, closing...") @@ -401,25 +383,21 @@ def update_pipeline(self, runtime_only: bool) -> Message: is_usb2 = self._oak.device.getUsbSpeed() == dai.UsbSpeed.HIGH if is_poe: self.store.send_message_to_frontend( - WarningMessage( - "Device is connected via PoE. This may cause performance issues.") + WarningMessage("Device is connected via PoE. This may cause performance issues.") ) print("Connected to a PoE device, camera streams will be JPEG encoded...") elif is_usb2: self.store.send_message_to_frontend( - WarningMessage( - "Device is connected in USB2 mode. This may cause performance issues.") + WarningMessage("Device is connected in USB2 mode. This may cause performance issues.") ) - print( - "Device is connected in USB2 mode, camera streams will be JPEG encoded...") + print("Device is connected in USB2 mode, camera streams will be JPEG encoded...") self.use_encoding = is_poe or is_usb2 connected_camera_features = self._oak.device.getConnectedCameraFeatures() for cam in config.cameras: print("Creating camera: ", cam) - camera_features = next( - filter(lambda feat: feat.socket == cam.board_socket, connected_camera_features)) + camera_features = next(filter(lambda feat: feat.socket == cam.board_socket, connected_camera_features)) # When the resolution is too small, the ISP needs to scale it down res_x, res_y = get_size_from_resolution(cam.resolution) @@ -444,8 +422,7 @@ def update_pipeline(self, runtime_only: bool) -> Message: config for config in camera_features.configs if config.type == camera_features.supportedTypes[0] ][0] sensor_resolution = size_to_resolution.get( - (smallest_supported_resolution.width, - smallest_supported_resolution.height), None + (smallest_supported_resolution.width, smallest_supported_resolution.height), None ) is_used_by_depth = config.depth is not None and ( cam.board_socket == config.depth.align or cam.board_socket in config.depth.stereo_pair @@ -468,17 +445,14 @@ def update_pipeline(self, runtime_only: bool) -> Message: if not does_sensor_support_resolution: sdk_cam.config_color_camera( isp_scale=getClosestIspScale( - (smallest_supported_resolution.width, - smallest_supported_resolution.height), res_x + (smallest_supported_resolution.width, smallest_supported_resolution.height), res_x ) ) self._cameras.append(sdk_cam) else: - print("Skipped creating camera:", cam.board_socket, - "because no valid sensor resolution was found.") + print("Skipped creating camera:", cam.board_socket, "because no valid sensor resolution was found.") continue - self._queues.append( - (sdk_cam, self._oak.queue(sdk_cam.out.main))) + self._queues.append((sdk_cam, self._oak.queue(sdk_cam.out.main))) if config.depth: print("Creating depth") @@ -490,14 +464,11 @@ def update_pipeline(self, runtime_only: bool) -> Message: if left_cam.node.getResolutionWidth() > 1280: print("Left cam width > 1280, setting isp scale to get 800") - left_cam.config_color_camera(isp_scale=calculate_isp_scale( - left_cam.node.getResolutionWidth())) + left_cam.config_color_camera(isp_scale=calculate_isp_scale(left_cam.node.getResolutionWidth())) if right_cam.node.getResolutionWidth() > 1280: print("Right cam width > 1280, setting isp scale to get 800") - right_cam.config_color_camera(isp_scale=calculate_isp_scale( - right_cam.node.getResolutionWidth())) - self._stereo = self._oak.create_stereo( - left=left_cam, right=right_cam, name="depth") + right_cam.config_color_camera(isp_scale=calculate_isp_scale(right_cam.node.getResolutionWidth())) + self._stereo = self._oak.create_stereo(left=left_cam, right=right_cam, name="depth") align_component = self._get_component_by_socket(config.depth.align) if not align_component: @@ -511,16 +482,13 @@ def update_pipeline(self, runtime_only: bool) -> Message: median=config.depth.median, ) - aligned_camera = self._get_camera_config_by_socket( - config, config.depth.align) + aligned_camera = self._get_camera_config_by_socket(config, config.depth.align) if not aligned_camera: return ErrorMessage(f"{config.depth.align} is not configured. Couldn't create stereo pair.") - self._queues.append( - (self._stereo, self._oak.queue(self._stereo.out.main))) + self._queues.append((self._stereo, self._oak.queue(self._stereo.out.main))) if self._oak.device.getConnectedIMU() != "NONE" and self._oak.device.getConnectedIMU() != "": - print("Creating IMU, connected IMU: ", - self._oak.device.getConnectedIMU()) + print("Creating IMU, connected IMU: ", self._oak.device.getConnectedIMU()) # TODO(someone): Handle IMU updates imu = self._oak.create_imu() sensors = [ @@ -537,26 +505,20 @@ def update_pipeline(self, runtime_only: bool) -> Message: print("Connected cam doesn't have IMU, skipping IMU creation...") if config.ai_model and config.ai_model.path: - cam_component = self._get_component_by_socket( - config.ai_model.camera) + cam_component = self._get_component_by_socket(config.ai_model.camera) if not cam_component: return ErrorMessage(f"{config.ai_model.camera} is not configured. Couldn't create NN.") if config.ai_model.path == "age-gender-recognition-retail-0013": - face_detection = self._oak.create_nn( - "face-detection-retail-0004", cam_component) - self._nnet = self._oak.create_nn( - "age-gender-recognition-retail-0013", input=face_detection) + face_detection = self._oak.create_nn("face-detection-retail-0004", cam_component) + self._nnet = self._oak.create_nn("age-gender-recognition-retail-0013", input=face_detection) else: - self._nnet = self._oak.create_nn( - config.ai_model.path, cam_component) + self._nnet = self._oak.create_nn(config.ai_model.path, cam_component) - camera = self._get_camera_config_by_socket( - config, config.ai_model.camera) + camera = self._get_camera_config_by_socket(config, config.ai_model.camera) if not camera: return ErrorMessage(f"{config.ai_model.camera} is not configured. Couldn't create NN.") - self._queues.append( - (self._nnet, self._oak.queue(self._nnet.out.main))) + self._queues.append((self._nnet, self._oak.queue(self._nnet.out.main))) sys_logger_xlink = self._oak.pipeline.createXLinkOut() logger = self._oak.pipeline.createSystemLogger() @@ -573,8 +535,7 @@ def update_pipeline(self, runtime_only: bool) -> Message: running = self._oak.running() if running: self._pipeline_start_t = time.time() - self._sys_info_q = self._oak.device.getOutputQueue( - "sys_logger", 1, False) + self._sys_info_q = self._oak.device.getOutputQueue("sys_logger", 1, False) # We might have modified the config, so store it self.store.set_pipeline_config(config) try: @@ -607,8 +568,7 @@ def update(self) -> None: sys_info = self._sys_info_q.tryGet() # type: ignore[attr-defined] if sys_info is not None and self._pipeline_start_t is not None: print("----------------------------------------") - print( - f"[{int(time.time() - self._pipeline_start_t)}s] System information") + print(f"[{int(time.time() - self._pipeline_start_t)}s] System information") print("----------------------------------------") print_system_information(sys_info) # if time.time() - self.start > 10: diff --git a/rerun_py/depthai_viewer/_backend/device_configuration.py b/rerun_py/depthai_viewer/_backend/device_configuration.py index 0343c6bedbf6..73835f918d4b 100644 --- a/rerun_py/depthai_viewer/_backend/device_configuration.py +++ b/rerun_py/depthai_viewer/_backend/device_configuration.py @@ -37,8 +37,7 @@ def __init__(self, **v) -> None: # type: ignore[no-untyped-def] ) return super().__init__(**v) # type: ignore[no-any-return] - # type: ignore[no-untyped-def] - def dict(self, *args, **kwargs) -> Dict[str, Any]: + def dict(self, *args, **kwargs) -> Dict[str, Any]: # type: ignore[no-untyped-def] return { "median": self.median.name if self.median else None, "lr_check": self.lr_check, @@ -177,15 +176,13 @@ class Config: def __init__(self, **v) -> None: # type: ignore[no-untyped-def] if v.get("board_socket", None): if isinstance(v["board_socket"], str): - v["board_socket"] = getattr( - dai.CameraBoardSocket, v["board_socket"]) + v["board_socket"] = getattr(dai.CameraBoardSocket, v["board_socket"]) if v.get("kind", None): if isinstance(v["kind"], str): v["kind"] = getattr(dai.CameraSensorType, v["kind"]) return super().__init__(**v) # type: ignore[no-any-return] - # type: ignore[no-untyped-def] - def dict(self, *args, **kwargs) -> Dict[str, Any]: + def dict(self, *args, **kwargs) -> Dict[str, Any]: # type: ignore[no-untyped-def] return { "fps": self.fps, "resolution": self.resolution.dict(), @@ -236,8 +233,7 @@ class Config: arbitrary_types_allowed = True use_enum_values = True - # type: ignore[no-untyped-def] - def dict(self, *args, **kwargs) -> Dict[str, Any]: + def dict(self, *args, **kwargs) -> Dict[str, Any]: # type: ignore[no-untyped-def] return { "resolutions": [r for r in self.resolutions], "max_fps": self.max_fps, @@ -274,26 +270,22 @@ class DeviceProperties(BaseModel): # type: ignore[misc] stereo_pairs: List[ Tuple[dai.CameraBoardSocket, dai.CameraBoardSocket] ] = [] # Which cameras can be paired for stereo - default_stereo_pair: Optional[Tuple[dai.CameraBoardSocket, - dai.CameraBoardSocket]] = None + default_stereo_pair: Optional[Tuple[dai.CameraBoardSocket, dai.CameraBoardSocket]] = None info: DeviceInfo = DeviceInfo() class Config: arbitrary_types_allowed = True use_enum_values = True - # type: ignore[no-untyped-def] - def __init__(self, *args, **kwargs) -> None: + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] if kwargs.get("stereo_pairs", None) and all(isinstance(pair[0], str) for pair in kwargs["stereo_pairs"]): kwargs["stereo_pairs"] = [ - (getattr(dai.CameraBoardSocket, pair[0]), getattr( - dai.CameraBoardSocket, pair[1])) + (getattr(dai.CameraBoardSocket, pair[0]), getattr(dai.CameraBoardSocket, pair[1])) for pair in kwargs["stereo_pairs"] ] return super().__init__(*args, **kwargs) # type: ignore[no-any-return] - # type: ignore[no-untyped-def] - def dict(self, *args, **kwargs) -> Dict[str, Any]: + def dict(self, *args, **kwargs) -> Dict[str, Any]: # type: ignore[no-untyped-def] return { "id": self.id, "cameras": [cam.dict() for cam in self.cameras], diff --git a/rerun_py/depthai_viewer/_backend/main.py b/rerun_py/depthai_viewer/_backend/main.py index 7b7274b55685..79639836ace7 100644 --- a/rerun_py/depthai_viewer/_backend/main.py +++ b/rerun_py/depthai_viewer/_backend/main.py @@ -20,7 +20,6 @@ SubscriptionsMessage, ) from depthai_viewer._backend.store import Store -import traceback viewer.init("Depthai Viewer") viewer.connect() diff --git a/rerun_py/depthai_viewer/_backend/packet_handler.py b/rerun_py/depthai_viewer/_backend/packet_handler.py index 14164489553a..e025262bd815 100644 --- a/rerun_py/depthai_viewer/_backend/packet_handler.py +++ b/rerun_py/depthai_viewer/_backend/packet_handler.py @@ -27,8 +27,7 @@ class PacketHandler: store: Store _ahrs: Mahony - _get_camera_intrinsics: Callable[[ - dai.CameraBoardSocket, int, int], NDArray[np.float32]] + _get_camera_intrinsics: Callable[[dai.CameraBoardSocket, int, int], NDArray[np.float32]] def __init__( self, store: Store, intrinsics_getter: Callable[[dai.CameraBoardSocket, int, int], NDArray[np.float32]] @@ -59,8 +58,7 @@ def log_packet( if isinstance(component, CameraComponent): self._on_camera_frame(packet, component._socket) else: - print("Unknown component type:", type( - component), "for packet:", type(packet)) + print("Unknown component type:", type(component), "for packet:", type(packet)) # Create dai.CameraBoardSocket from descriptor elif type(packet) is DepthPacket: if isinstance(component, StereoComponent): @@ -71,8 +69,7 @@ def log_packet( elif isinstance(component, StereoComponent): self._on_stereo_frame(packet, component) else: - print("Unknown component type:", type( - component), "for packet:", type(packet)) + print("Unknown component type:", type(component), "for packet:", type(packet)) elif type(packet) is DetectionPacket: self._on_detections(packet, component) elif type(packet) is TwoStagePacket: @@ -85,12 +82,10 @@ def _on_camera_frame(self, packet: FramePacket, board_socket: dai.CameraBoardSoc f"{board_socket.name}/transform", child_from_parent=([0, 0, 0], [1, 0, 0, 0]), xyz="RDF" ) # TODO(filip): Enable the user to lock the camera rotation in the UI - img_frame = packet.frame if packet.msg.getType( - ) == dai.RawImgFrame.Type.RAW8 else packet.msg.getData() + img_frame = packet.frame if packet.msg.getType() == dai.RawImgFrame.Type.RAW8 else packet.msg.getData() h, w = packet.msg.getHeight(), packet.msg.getWidth() if packet.msg.getType() == dai.ImgFrame.Type.BITSTREAM: - img_frame = cv2.cvtColor(cv2.imdecode( - img_frame, cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB) + img_frame = cv2.cvtColor(cv2.imdecode(img_frame, cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB) h, w = img_frame.shape[:2] child_from_parent: NDArray[np.float32] @@ -100,8 +95,7 @@ def _on_camera_frame(self, packet: FramePacket, board_socket: dai.CameraBoardSoc ) except Exception: f_len = (w * h) ** 0.5 - child_from_parent = np.array( - [[f_len, 0, w / 2], [0, f_len, h / 2], [0, 0, 1]]) + child_from_parent = np.array([[f_len, 0, w / 2], [0, f_len, h / 2], [0, 0, 1]]) cam = cam_kind_from_frame_type(packet.msg.getType()) viewer.log_pinhole( f"{board_socket.name}/transform/{cam}/", @@ -128,13 +122,11 @@ def on_imu(self, packet: IMUPacket) -> None: mag: dai.IMUReportMagneticField = packet.magneticField # TODO(filip): Move coordinate mapping to sdk self._ahrs.Q = self._ahrs.updateIMU( - self._ahrs.Q, np.array([gyro.z, gyro.x, gyro.y]), np.array( - [accel.z, accel.x, accel.y]) + self._ahrs.Q, np.array([gyro.z, gyro.x, gyro.y]), np.array([accel.z, accel.x, accel.y]) ) if Topic.ImuData not in self.store.subscriptions: return - viewer.log_imu([accel.z, accel.x, accel.y], [ - gyro.z, gyro.x, gyro.y], self._ahrs.Q, [mag.x, mag.y, mag.z]) + viewer.log_imu([accel.z, accel.x, accel.y], [gyro.z, gyro.x, gyro.y], self._ahrs.Q, [mag.x, mag.y, mag.z]) def _on_stereo_frame(self, packet: Union[DepthPacket, DisparityDepthPacket], component: StereoComponent) -> None: depth_frame = packet.frame @@ -154,8 +146,7 @@ def _on_tof_packet( viewer.log_rigid3( f"{component.camera_socket.name}/transform", child_from_parent=([0, 0, 0], [1, 0, 0, 0]), xyz="RDF" ) - intrinsics = np.array( - [[471.451, 0.0, 317.897], [0.0, 471.539, 245.027], [0.0, 0.0, 1.0]]) + intrinsics = np.array([[471.451, 0.0, 317.897], [0.0, 471.539, 245.027], [0.0, 0.0, 1.0]]) viewer.log_pinhole( f"{component.camera_socket.name}/transform/tof", child_from_parent=intrinsics, @@ -167,8 +158,7 @@ def _on_tof_packet( viewer.log_depth_image(path, depth_frame, meter=1e3) def _on_detections(self, packet: DetectionPacket, component: NNComponent) -> None: - rects, colors, labels = self._detections_to_rects_colors_labels( - packet, component.get_labels()) + rects, colors, labels = self._detections_to_rects_colors_labels(packet, component.get_labels()) cam = "color_cam" if component._get_camera_comp().is_color() else "mono_cam" viewer.log_rects( f"{component._get_camera_comp()._socket.name}/transform/{cam}/Detections", @@ -185,22 +175,19 @@ def _detections_to_rects_colors_labels( colors = [] labels = [] for detection in packet.detections: - rects.append(self._rect_from_detection( - detection, packet.frame.shape[0], packet.frame.shape[1])) + rects.append(self._rect_from_detection(detection, packet.frame.shape[0], packet.frame.shape[1])) colors.append([0, 255, 0]) label: str = detection.label_str # Open model zoo models output label index if omz_labels is not None and isinstance(label, int): label += omz_labels[label] - label += ", " + \ - str(int(detection.img_detection.confidence * 100)) + "%" + label += ", " + str(int(detection.img_detection.confidence * 100)) + "%" labels.append(label) return rects, colors, labels def _on_age_gender_packet(self, packet: TwoStagePacket, component: NNComponent) -> None: for det, rec in zip(packet.detections, packet.nnData): - age = int( - float(np.squeeze(np.array(rec.getLayerFp16("age_conv3")))) * 100) + age = int(float(np.squeeze(np.array(rec.getLayerFp16("age_conv3")))) * 100) gender = np.squeeze(np.array(rec.getLayerFp16("prob"))) gender_str = "Woman" if gender[0] > gender[1] else "Man" label = f"{gender_str}, {age}" @@ -210,8 +197,7 @@ def _on_age_gender_packet(self, packet: TwoStagePacket, component: NNComponent) cam = "color_cam" if component._get_camera_comp().is_color() else "mono_cam" viewer.log_rect( f"{component._get_camera_comp()._socket.name}/transform/{cam}/Detection", - self._rect_from_detection( - det, packet.frame.shape[0], packet.frame.shape[1]), + self._rect_from_detection(det, packet.frame.shape[0], packet.frame.shape[1]), rect_format=RectFormat.XYXY, color=color, label=label, diff --git a/rerun_py/depthai_viewer/install_requirements.py b/rerun_py/depthai_viewer/install_requirements.py index 1ace066628ca..d810ccade7e4 100644 --- a/rerun_py/depthai_viewer/install_requirements.py +++ b/rerun_py/depthai_viewer/install_requirements.py @@ -13,8 +13,7 @@ script_path = os.path.dirname(os.path.abspath(__file__)) venv_dir = os.path.join(script_path, "venv-" + depthai_viewer_version()) venv_python = ( - os.path.join(venv_dir, "Scripts", "python") if sys.platform == "win32" else os.path.join( - venv_dir, "bin", "python") + os.path.join(venv_dir, "Scripts", "python") if sys.platform == "win32" else os.path.join(venv_dir, "bin", "python") ) @@ -24,21 +23,18 @@ def delete_partially_created_venv(path: str) -> None: print(f"Deleting partially created virtual environment: {path}") shutil.rmtree(path) except Exception as e: - print( - f"Error occurred while attempting to delete the virtual environment: {e}") + print(f"Error occurred while attempting to delete the virtual environment: {e}") print(traceback.format_exc()) -# type: ignore[no-untyped-def] -def sigint_mid_venv_install_handler(signum, frame) -> None: +def sigint_mid_venv_install_handler(signum, frame) -> None: # type: ignore[no-untyped-def] delete_partially_created_venv(venv_dir) def get_site_packages() -> str: """Gets site packages dir of the virtual environment. Throws an exception if site packages could not be fetched.""" return subprocess.run( - [venv_python, "-c", - "import sysconfig; print(sysconfig.get_paths()['purelib'], end='')"], + [venv_python, "-c", "import sysconfig; print(sysconfig.get_paths()['purelib'], end='')"], capture_output=True, text=True, check=True, @@ -81,12 +77,10 @@ def create_venv_and_install_dependencies() -> None: # In case of Ctrl+C during the venv creation, delete the partially created venv signal.signal(signal.SIGINT, sigint_mid_venv_install_handler) print("Creating virtual environment...") - subprocess.run( - [sys.executable, "-m", "venv", venv_dir], check=True) + subprocess.run([sys.executable, "-m", "venv", venv_dir], check=True) # Install dependencies - subprocess.run([venv_python, "-m", "pip", - "install", "-U", "pip"], check=True) + subprocess.run([venv_python, "-m", "pip", "install", "-U", "pip"], check=True) # Install depthai_sdk first, then override depthai version with the one from requirements.txt subprocess.run( [ @@ -102,8 +96,7 @@ def create_venv_and_install_dependencies() -> None: check=True, ) subprocess.run( - [venv_python, "-m", "pip", "install", "-r", - f"{script_path}/requirements.txt"], + [venv_python, "-m", "pip", "install", "-r", f"{script_path}/requirements.txt"], check=True, ) venv_packages_dir = get_site_packages() @@ -122,20 +115,22 @@ def create_venv_and_install_dependencies() -> None: # Download blobs try: subprocess.run( - [sys.executable, "-c", - "from depthai_viewer.install_requirements import download_blobs; download_blobs()"], + [ + sys.executable, + "-c", + "from depthai_viewer.install_requirements import download_blobs; download_blobs()", + ], check=True, env=env, - capture_output=True + capture_output=True, ) except subprocess.CalledProcessError as e: - print("stderr") - print(e.stderr.decode('utf-8')) + print(e.stderr.decode("utf-8")) print("stdout") - print(e.stdout.decode('utf-8')) + print(e.stdout.decode("utf-8")) print("output") - print(e.output.decode('utf-8')) + print(e.output.decode("utf-8")) raise e