UPDATE Vision 图漾相机代码融合

This commit is contained in:
HJW
2024-09-27 15:29:07 +08:00
parent 65a649cc21
commit ac3606b8b3
4 changed files with 35 additions and 16 deletions

View File

@ -11,8 +11,8 @@
import numpy as np import numpy as np
import cv2 import cv2
import open3d as o3d import open3d as o3d
# from Vision.tool.CameraRVC import camera from Vision.tool.CameraRVC import camera_rvc
from Vision.tool.CameraPe import camera from Vision.tool.CameraPe import camera_pe
from Vision.yolo.yolov8_pt_seg import yolov8_segment from Vision.yolo.yolov8_pt_seg import yolov8_segment
from Vision.yolo.yolov8_openvino import yolov8_segment_openvino from Vision.yolo.yolov8_openvino import yolov8_segment_openvino
from Vision.tool.utils import find_position from Vision.tool.utils import find_position
@ -25,23 +25,33 @@ import os
class Detection: class Detection:
def __init__(self, use_openvino_model=True): def __init__(self, use_openvino_model=True, cameraIsRVC = True):
self.use_openvino_model = use_openvino_model self.use_openvino_model = use_openvino_model
if self.use_openvino_model==False: if self.use_openvino_model==False:
model_path = ''.join([os.getcwd(), '/Vision/model/pt/best.pt']) model_path = ''.join([os.getcwd(), '/Vision/model/pt/best.pt'])
device = 'cpu' device = 'cpu'
self.camera_rvc = camera() if cameraIsRVC == True:
self.camera_rvc = camera_rvc()
self.seg_distance_threshold = 0.005
else:
self.camera_rvc = camera_pe()
self.seg_distance_threshold = 10
self.model = yolov8_segment() self.model = yolov8_segment()
self.model.load_model(model_path, device) self.model.load_model(model_path, device)
else: else:
model_path = ''.join([os.getcwd(), '/Vision/model/openvino/best.xml']) model_path = ''.join([os.getcwd(), '/Vision/model/openvino/best.xml'])
device = 'CPU' device = 'CPU'
self.camera_rvc = camera() if cameraIsRVC == True:
self.camera_rvc = camera_rvc()
self.seg_distance_threshold = 0.005
else:
self.camera_rvc = camera_pe()
self.seg_distance_threshold = 10
self.model = yolov8_segment_openvino(model_path, device, conf_thres=0.3, iou_thres=0.3) self.model = yolov8_segment_openvino(model_path, device, conf_thres=0.3, iou_thres=0.3)
def get_position(self, Point_isVision=False, save_img_point=False, seg_distance_threshold= 10): def get_position(self, Point_isVision=False, save_img_point=False):
"" ""
''' '''
:param api: None :param api: None
@ -158,7 +168,7 @@ class Detection:
''' '''
pcd = o3d.geometry.PointCloud() pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(pm_seg) pcd.points = o3d.utility.Vector3dVector(pm_seg)
plane_model, inliers = pcd.segment_plane(distance_threshold=seg_distance_threshold, plane_model, inliers = pcd.segment_plane(distance_threshold=self.seg_distance_threshold,
ransac_n=5, ransac_n=5,
num_iterations=5000) num_iterations=5000)
[a, b, c, d] = plane_model [a, b, c, d] = plane_model
@ -223,7 +233,7 @@ class Detection:
if Point_isVision==True: if Point_isVision==True:
pcd = o3d.geometry.PointCloud() pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(seg_point[_idx]) pcd.points = o3d.utility.Vector3dVector(seg_point[_idx])
plane_model, inliers = pcd.segment_plane(distance_threshold=seg_distance_threshold, plane_model, inliers = pcd.segment_plane(distance_threshold=self.seg_distance_threshold,
ransac_n=5, ransac_n=5,
num_iterations=5000) num_iterations=5000)
inlier_cloud = pcd.select_by_index(inliers) # 点云可视化 inlier_cloud = pcd.select_by_index(inliers) # 点云可视化

View File

@ -12,7 +12,8 @@ import numpy as np
import cv2 import cv2
import open3d as o3d import open3d as o3d
import time import time
from Vision.tool.CameraRVC import camera from Vision.tool.CameraRVC import camera_rvc
from Vision.tool.CameraPe import camera_pe
from Vision.yolo.yolov8_pt_seg import yolov8_segment from Vision.yolo.yolov8_pt_seg import yolov8_segment
from Vision.yolo.yolov8_openvino import yolov8_segment_openvino from Vision.yolo.yolov8_openvino import yolov8_segment_openvino
from Vision.tool.utils import find_position from Vision.tool.utils import find_position
@ -24,7 +25,7 @@ import os
class Detection: class Detection:
def __init__(self, use_openvino_model = True): def __init__(self, use_openvino_model = True, cameraIsRVC = True):
self.use_openvino_model = use_openvino_model self.use_openvino_model = use_openvino_model
if self.use_openvino_model == False: if self.use_openvino_model == False:
model_path = ''.join([os.getcwd(), '/Vision/model/pt/best.pt']) model_path = ''.join([os.getcwd(), '/Vision/model/pt/best.pt'])
@ -36,14 +37,15 @@ class Detection:
device = 'CPU' device = 'CPU'
self.model = yolov8_segment_openvino(model_path, device, conf_thres=0.3, iou_thres=0.3) self.model = yolov8_segment_openvino(model_path, device, conf_thres=0.3, iou_thres=0.3)
img_path = ''.join([os.getcwd(), '/Vision/model/data/test0911.png']) img_path = ''.join([os.getcwd(), '/Vision/model/data/1.png'])
point_path = ''.join([os.getcwd(), '/Vision/model/data/test0911.xyz']) point_path = ''.join([os.getcwd(), '/Vision/model/data/1.xyz'])
self.img = cv2.imread(img_path) self.img = cv2.imread(img_path)
self.point = np.loadtxt(point_path).reshape((1080, 1440, 3)) self.point = np.loadtxt(point_path).reshape((1080, 1440, 3))
pass
def get_position(self, Point_isVision=False, save_img_point=True, seg_distance_threshold = 10): def get_position(self, Point_isVision=False, save_img_point=True, seg_distance_threshold = 0.01):
"" ""
''' '''
:param api: None :param api: None
@ -186,8 +188,15 @@ class Detection:
# 这一步不影响后面的画图,但是可以保证四个角点坐标为顺时针 # 这一步不影响后面的画图,但是可以保证四个角点坐标为顺时针
startidx = box.sum(axis=1).argmin() startidx = box.sum(axis=1).argmin()
box1 = np.roll(box, 4 - startidx, 0) box = np.roll(box, 4 - startidx, 0)
box = np.int0(box)
# 在原图上画出预测的外接矩形 # 在原图上画出预测的外接矩形
'''
拟合最大内接矩形,计算矩形中心
'''
box = box.reshape((-1, 1, 2)).astype(np.int32) box = box.reshape((-1, 1, 2)).astype(np.int32)
box = box + [[[box_x1, box_y1]], [[box_x1, box_y1]], [[box_x1, box_y1]], [[box_x1, box_y1]]] box = box + [[[box_x1, box_y1]], [[box_x1, box_y1]], [[box_x1, box_y1]], [[box_x1, box_y1]]]

View File

@ -26,7 +26,7 @@ class PythonPercipioDeviceEvent(pcammls.DeviceEvent):
def IsOffline(self): def IsOffline(self):
return self.Offline return self.Offline
class camera(): class camera_pe():
def __init__(self): def __init__(self):
super().__init__() super().__init__()

View File

@ -10,7 +10,7 @@
import PyRVC as RVC import PyRVC as RVC
import numpy as np import numpy as np
class camera: class camera_rvc:
def __init__(self): def __init__(self):
self.caminit_isok = False self.caminit_isok = False