Files
AutoControlSystem-G/CU/Detect.py
2025-07-29 13:16:30 +08:00

86 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from enum import Enum
import numpy as np
from PySide6.QtCore import Signal
import Constant
from Model.Position import Real_Position
from Trace.handeye_calibration import getPosition
#from Vision.camera_coordinate_dete import Detection
import configparser
import os
class DetectStatus(Enum):
DNone = 0
DDetect = 1
DOk = 2
class miDetect:
update_detect_image = Signal(np.ndarray)
def __init__(self):
self.detection = Detection(alignmentType='depth2color')
self.detect_status = DetectStatus.DNone
self.detect_position = None
def run(self):
if self.detect_status == DetectStatus.DNone:
return
if self.detect_status == DetectStatus.DDetect:
if Constant.Debug:
self.detect_status = DetectStatus.DOk
return
_, img, xyz, uvw, points = self.detection.get_position(Point_isVision=False, Box_isPoint=True,
First_Depth=True, Iter_Max_Pixel=30,
save_img_point=1, Height_reduce=30, width_reduce=30)
self.detection_image = img.copy()
if xyz == None or uvw == None or points == None:
self.detect_position = None
self.detect_status = DetectStatus.DOk
return
target_position, noraml_base = getPosition(*xyz, *uvw, None, points)
position = Real_Position().init_position(*target_position[:3], *noraml_base[:3])
# position.Z = position.Z
position.a = uvw[0]
position.b = uvw[1]
position.c = uvw[2]
self.detect_position = position
self.detect_status = DetectStatus.DOk
if self.detect_status == DetectStatus.DOk:
return
class Detect:
update_detect_image = Signal(np.ndarray)
def __init__(self):
self.detection = ""
self.detect_status = DetectStatus.DOk
self.detect_position = None
self.position_index = 0 # 默认读取索引为 0 的点位
def run(self):
if self.detect_status == DetectStatus.DOk:
self.detect_position = None
config = configparser.ConfigParser()
config_file = os.path.join(os.path.dirname(__file__), 'list.ini') #配置文件地址
if not os.path.exists(config_file):
print("配置文件 list.ini 不存在")
return
config.read(config_file)
if not config.has_section('positions'):
print("配置文件中没有 [positions] 段")
return
if not config.has_option('positions', str(self.position_index)):
print(f"没有索引为 {self.position_index} 的点位")
return
try:
# 读取配置项
data = config.get('positions', str(self.position_index)).strip().split(',')
if len(data) != 6:
raise ValueError(f"点位数据格式错误应为6个值: {data}")
x, y, z, a, b, c = map(float, data)
# 初始化坐标
self.detect_position = Real_Position()
self.detect_position.init_position(x, y, z, a, b, c)
#print(f"成功加载点位索引 {self.position_index}: ({x}, {y}, {z}, {a}, {b}, {c})")
except Exception as e:
print(f"读取点位时出错: {e}")