From 9b76f5db6fb07fa3af2bb4905aaba0b12ec0a4e2 Mon Sep 17 00:00:00 2001 From: liangyuxuan <16126883+liangyuxuan123@user.noreply.gitee.com> Date: Wed, 3 Dec 2025 11:11:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A9=B9=E6=B5=8B=E6=8A=80=E8=83=BD=E5=8C=85?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BE=93=E5=85=A5=E6=96=B9=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E6=AC=A1=E6=A3=80=E6=B5=8B=E7=9A=84?= =?UTF-8?q?=E7=89=A9=E4=BD=93id=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +-- vision_detect/.idea/.gitignore | 8 ----- vision_detect/.idea/cv_part.iml | 11 ------- vision_detect/.idea/dictionaries/project.xml | 7 ---- .../inspectionProfiles/profiles_settings.xml | 6 ---- vision_detect/.idea/misc.xml | 7 ---- vision_detect/.idea/modules.xml | 8 ----- .../VisionDetect/node/detect_node.py | 4 ++- .../VisionDetect/utils/draw_tools.py | 32 +++++++++++++++++-- .../flexivaidk_detect_service.py | 31 ++++++++++++------ 10 files changed, 56 insertions(+), 62 deletions(-) delete mode 100644 vision_detect/.idea/.gitignore delete mode 100644 vision_detect/.idea/cv_part.iml delete mode 100644 vision_detect/.idea/dictionaries/project.xml delete mode 100644 vision_detect/.idea/inspectionProfiles/profiles_settings.xml delete mode 100644 vision_detect/.idea/misc.xml delete mode 100644 vision_detect/.idea/modules.xml diff --git a/.gitignore b/.gitignore index 6ac09ee..d0c7579 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -.idea/ -.vscode/ +**/.idea/ +**/.vscode/ **/build/ **/install/ **/log/ \ No newline at end of file diff --git a/vision_detect/.idea/.gitignore b/vision_detect/.idea/.gitignore deleted file mode 100644 index 35410ca..0000000 --- a/vision_detect/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/vision_detect/.idea/cv_part.iml b/vision_detect/.idea/cv_part.iml deleted file mode 100644 index 5b05384..0000000 --- a/vision_detect/.idea/cv_part.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/vision_detect/.idea/dictionaries/project.xml b/vision_detect/.idea/dictionaries/project.xml deleted file mode 100644 index 7f00eca..0000000 --- a/vision_detect/.idea/dictionaries/project.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - rclpy - - - \ No newline at end of file diff --git a/vision_detect/.idea/inspectionProfiles/profiles_settings.xml b/vision_detect/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/vision_detect/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/vision_detect/.idea/misc.xml b/vision_detect/.idea/misc.xml deleted file mode 100644 index 9de2865..0000000 --- a/vision_detect/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/vision_detect/.idea/modules.xml b/vision_detect/.idea/modules.xml deleted file mode 100644 index 9bcae4c..0000000 --- a/vision_detect/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/vision_detect/vision_detect/VisionDetect/node/detect_node.py b/vision_detect/vision_detect/VisionDetect/node/detect_node.py index 5cc51d4..93c6e2d 100644 --- a/vision_detect/vision_detect/VisionDetect/node/detect_node.py +++ b/vision_detect/vision_detect/VisionDetect/node/detect_node.py @@ -487,7 +487,9 @@ class DetectNode(Node): # mask_img and box_img is or not output if self.output_boxes and not self.output_masks: - draw_box(rgb_img, result) + home = os.path.expanduser("~") + save_path = os.path.join(home, "detect_image.png") + draw_box(rgb_img, result, save_path=save_path) return self.cv_bridge.cv2_to_imgmsg(rgb_img, "bgr8"), pose_list elif self.output_boxes and self.output_masks: draw_box(rgb_img, result) diff --git a/vision_detect/vision_detect/VisionDetect/utils/draw_tools.py b/vision_detect/vision_detect/VisionDetect/utils/draw_tools.py index 4a4918c..ee62fb5 100644 --- a/vision_detect/vision_detect/VisionDetect/utils/draw_tools.py +++ b/vision_detect/vision_detect/VisionDetect/utils/draw_tools.py @@ -1,15 +1,23 @@ +import os import logging +from typing import Union import cv2 import numpy as np import open3d as o3d + __all__ = [ "draw_box", "draw_mask", "draw_pointcloud", ] -def draw_box(rgb_img: np.ndarray, segmentation_result, put_text: bool = True): +def draw_box( + rgb_img: np.ndarray, + segmentation_result, + put_text: bool = True, + save_path: Union[bool, str] = False +): """ 绘制目标检测边界框 """ @@ -26,9 +34,23 @@ def draw_box(rgb_img: np.ndarray, segmentation_result, put_text: bool = True): cv2.rectangle(rgb_img, p1, p2, (255, 255, 0), 2) if put_text: - cv2.putText(rgb_img, f'{labels[class_ids[i]]}: {confidences[i]*100:.2f}', (p1[0], p1[1] - 10), + cv2.putText(rgb_img, f'{labels[class_ids[i]]}: {confidences[i]*100:.2f}', + (p1[0], p1[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, + (255, 255, 0), 2) + + cv2.putText(rgb_img, f"{i}", (p1[0] + 5, p1[1] + 15), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2) + if save_path: + if isinstance(save_path, str): + dir_path = os.path.dirname(save_path) + if dir_path: + os.makedirs(dir_path, exist_ok=True) + else: + home_path = os.path.expanduser("~") + save_path = os.path.join(home_path, "detect_color_img.png") + cv2.imwrite(save_path, rgb_img) + def draw_mask(rgb_img: np.ndarray, segmentation_result): """ @@ -57,7 +79,11 @@ def draw_pointcloud(pcd, axis: bool = True): [1, 0, 0], [0, 1, 0], [0, 0, 1] ] # 构造open3d中的LineSet对象,用于主成分显示 - axis_set = o3d.geometry.LineSet(points=o3d.utility.Vector3dVector(axis_point), lines=o3d.utility.Vector2iVector(axis)) + axis_set = o3d.geometry.LineSet( + points=o3d.utility.Vector3dVector(axis_point), + lines=o3d.utility.Vector2iVector(axis) + ) + axis_set.colors = o3d.utility.Vector3dVector(axis_colors) pcd.append(axis_set) diff --git a/vision_detect/vision_detect/flexivaidk_detect_service.py b/vision_detect/vision_detect/flexivaidk_detect_service.py index 7b8ef93..4fb957c 100644 --- a/vision_detect/vision_detect/flexivaidk_detect_service.py +++ b/vision_detect/vision_detect/flexivaidk_detect_service.py @@ -322,29 +322,42 @@ class DetectNode(Node): time3 = time.time() - rgb_bytes = cv2.imencode('.png', rgb_img)[1] - depth_bytes = cv2.imencode('.png', depth_img)[1] + # rgb_bytes = cv2.imencode('.png', rgb_img)[1] + # depth_bytes = cv2.imencode('.png', depth_img)[1] for i, (mask, box) in enumerate(zip(masks, boxes)): mask = cv2.resize(mask.astype(np.uint8), orig_shape[::-1], interpolation=cv2.INTER_NEAREST) + rgb_crop, depth_crop, mask_crop, (x_min, y_min) = crop_mask_bbox(rgb_img, depth_img, mask, box) + + if depth_crop is None: + self.get_logger().error("depth_crop is None") + continue + + depth_img_crop_mask = np.zeros_like(depth_crop) + depth_img_crop_mask[mask_crop > 0] = depth_crop[mask_crop > 0] + + rgb_bytes = cv2.imencode('.png', rgb_crop)[1] + depth_bytes = cv2.imencode('.png', depth_img_crop_mask)[1] x_center, y_center, width, height = box[:4] p1 = [int((x_center - width / 2)), int((y_center - height / 2))] p2 = [int((x_center + width / 2)), int((y_center + height / 2))] cv2.rectangle(img, p1, p2, (255, 255, 0), 2) + cv2.putText(img, f"{i}", (p1[0], p1[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, + 1, (255, 255, 0), 2) - contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - mask_contours = contours[0].reshape(1, -1, 2) - res = self.aidk_client.set_direct_setting_variables({"one_mask": f"{mask_contours}"}) + # contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + # mask_contours = contours[0].reshape(1, -1, 2) + # res = self.aidk_client.set_direct_setting_variables({"one_mask": f"{mask_contours}"}) intrinsics = [ int(self.camera_size[0]), int(self.camera_size[1]), - # self.K[2] - x_min, - # self.K[5] - y_min, - self.K[2], - self.K[5], + self.K[2] - x_min, + self.K[5] - y_min, + # self.K[2], + # self.K[5], self.K[0], self.K[4] ]