Files
AutoControlSystem-G/CU/Detect.py

137 lines
5.9 KiB
Python
Raw Permalink Normal View History

2025-08-15 12:08:30 +08:00
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):
#print( f"[调试] 当前丢包点位索引: {self.position_index}")
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}")
class Detect1:
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):
print(f"[调试] 当前丢包点位索引: {self.position_index}")
if self.detect_status == DetectStatus.DNone:
return
if self.detect_status != DetectStatus.DOk:
print("检测状态不允许运行")
return False
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 False
try:
config.read(config_file, encoding='utf-8')
except Exception as e:
print(f"❌ 读取配置文件失败: {e}")
return False
if not config.has_section('positions'):
print("❌ 配置文件中没有 [positions] 段")
return False
if not config.has_option('positions', str(self.position_index)):
print(f"❌ 没有索引为 {self.position_index} 的点位")
return False
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{x} Y{y} Z{z} A{a} B{b} C{c}")
return True # ✅ 成功返回 True
except ValueError as ve:
print(f"❌ 数据转换错误: {ve}")
return False
except Exception as e:
print(f"❌ 未知错误: {e}")
return False