diff --git a/components/image_framework.py b/components/image_framework.py index edeb3bb..323a996 100644 --- a/components/image_framework.py +++ b/components/image_framework.py @@ -8,6 +8,7 @@ from numpy.ctypeslib import as_array import platform import time import cv2 +from paho.mqtt.publish import single from core.edge_component import action, EdgeComponent, service from util import get_system_and_library_suffix @@ -137,7 +138,8 @@ g_frame_cache = [] RUNNING_MODE_NONE = -1 # 停止状态 RUNNING_MODE_LOCATION = 0 # 广角相机运行模式 RUNNING_MODE_DETECTION = 1 # 高清检测运行模式 -g_running_mode = RUNNING_MODE_LOCATION # 默认是广角相机运行模式 +# g_running_mode = RUNNING_MODE_LOCATION # 默认是广角相机运行模式 +g_running_mode = RUNNING_MODE_NONE #################################################################################### # 内部接口定义 @@ -146,9 +148,10 @@ g_running_mode = RUNNING_MODE_LOCATION # 默认是广角相机运行模式 def _get_msg_info(code) -> str: global MSG_LIST for msg in MSG_LIST: - if code == msg.keys()[0]: - return msg.values - return "未知错误" + for key, value in msg.items(): + if code == key: + return value.encode("utf-8") + return "未知错误".encode("utf-8") # 加工检测图像 def _update_location_image(frame): @@ -204,6 +207,9 @@ class ImageFramework(EdgeComponent): self.process_thread = threading.Thread(target=self._main_processing_thread) self.process_thread.daemon = True + self.location_thread.start() + self.process_thread.start() + self.logger.info(f"Image framework configure done.") def init_image_framework_sdk(self): @@ -220,7 +226,6 @@ class ImageFramework(EdgeComponent): def init_callback(self): def _callback(data): global g_msg_cache, g_frame_cache, g_running_mode - record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False) frame = None ret = SYS_OK @@ -231,24 +236,29 @@ class ImageFramework(EdgeComponent): if len(g_frame_cache) == 0: self.logger.error("当前没有广角数据") return - frame = g_frame_cache.pop() # 从图像缓存中去除图像 g_frame_cache = [] # 清空缓存,避免无限增长 msg = Msg(MSG_LOCATION_RECORD, _copy_record(record), frame) + self.signals.on_image_detect_result.emit(msg) + elif g_running_mode == RUNNING_MODE_LOCATION: + msg = Msg(MSG_LOCATION_RECORD, _copy_record(record), None) + self.signals.on_image_detect_result.emit(msg) # 当检测模式下,接收到【拍照完成】消息 elif g_running_mode == RUNNING_MODE_DETECTION and record.code == (2 + TO_CLIENT_NOTIFY_BASE): - frame.zeros(9344, 7000, cv2.CV_8UC3) + frame = np.zeros((7000, 9344, 3), np.uint8) src_frame_ptr = c_char_p(frame.data.tobytes()) ret = self.image_framework_sdk.LibapiGetHQImage( src_frame_ptr, frame.shape[1], frame.shape[0], 3, c_char_p(b"PythonProcessing")) if ret != SYS_OK: record = CallbackInfo(code=ret, errorInfo=_get_msg_info(ret), bGetData=False) - msg = Msg(MSG_ONLY_RECORD, record, None) - # emit msg + msg = Msg(MSG_DETECTION_RECORD, record, None) + self.signals.on_image_detect_result.emit(msg) return msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), frame) - elif g_running_mode == RUNNING_MODE_LOCATION or g_running_mode == RUNNING_MODE_DETECTION: - msg = Msg(MSG_ONLY_RECORD, _copy_record(record), None) + self.signals.on_image_detect_result.emit(msg) + elif g_running_mode == RUNNING_MODE_DETECTION: + msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), None) + self.signals.on_image_detect_result.emit(msg) else: print("未知回调") return @@ -280,41 +290,105 @@ class ImageFramework(EdgeComponent): self.image_framework_sdk.stop_sdk() self.logger.info("Image framework stopped.") + # 启动检测 @service() def start_location(self): - self.location_thread.start() - self.process_thread.start() + global g_running_mode + if g_running_mode != RUNNING_MODE_LOCATION: + g_running_mode = RUNNING_MODE_LOCATION # 切换为定位模式 @service() - def start_detect(self): + def stop_all(self): global g_running_mode + if g_running_mode != RUNNING_MODE_NONE: + g_running_mode = RUNNING_MODE_NONE # 切换为静默模式 + + @service() + def start_adjust(self): + global g_running_mode + record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False) if g_running_mode != RUNNING_MODE_DETECTION: g_running_mode = RUNNING_MODE_DETECTION # 切换为检测模式 - + else: + record.code = -10001 + record.errorInfo = _get_msg_info(-10001) + return record ret = self.image_framework_sdk.LibapiStartDetection() - if ret != 0: - raise RuntimeError("启动检测失败!") + if ret != SYS_OK: + record = CallbackInfo() + record.code = ret + record.errorInfo = _get_msg_info(ret) + return record + return record @service() - def continue_detect(self): - ret = self.image_framework_sdk.LibapiContinuetDetection() - if ret != 0: - raise RuntimeError("继续检测失败!") - - @service() - def stop_detect(self): + def start_check(self): global g_running_mode + record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False) if g_running_mode == RUNNING_MODE_LOCATION: # 如果在定位任务模式下,该接口不执行 - raise RuntimeError("正在检测,请稍后停止!") + record.code = -10002 + record.errorInfo = _get_msg_info(-10002) + return MSG_DETECTION_RECORD + ret = self.image_framework_sdk.LibapiContinuetDetection() + if ret != SYS_OK: + record.code = ret + record.errorInfo = _get_msg_info(ret) + return record + return record + @service() + def stop_check(self): + global g_running_mode + record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False) + if g_running_mode == RUNNING_MODE_LOCATION: # 如果在定位任务模式下,该接口不执行 + record.code = -10002 + record.errorInfo = _get_msg_info(-10002) + return record + ret = self.image_framework_sdk.LibapiStopDetection() + if ret != SYS_OK: + record.code = ret + record.errorInfo = _get_msg_info(ret) + return record g_running_mode = RUNNING_MODE_NONE # 切换为无模式 - ret = self.image_framework_sdk.LibapStopDetection() - if ret != 0: - raise RuntimeError("停止检测失败!") + return record + + + # @service() + # # def start_location(self): + # # self.location_thread.start() + # # self.process_thread.start() + # + # @service() + # def start_detect(self): + # global g_running_mode + # if g_running_mode != RUNNING_MODE_DETECTION: + # g_running_mode = RUNNING_MODE_DETECTION # 切换为检测模式 + # + # ret = self.image_framework_sdk.LibapiStartDetection() + # if ret != 0: + # raise RuntimeError("启动检测失败!") + # + # @service() + # def continue_detect(self): + # ret = self.image_framework_sdk.LibapiContinuetDetection() + # if ret != 0: + # raise RuntimeError("继续检测失败!") + # + # @service() + # def stop_detect(self): + # global g_running_mode + # if g_running_mode == RUNNING_MODE_LOCATION: # 如果在定位任务模式下,该接口不执行 + # raise RuntimeError("正在检测,请稍后停止!") + # + # g_running_mode = RUNNING_MODE_NONE # 切换为无模式 + # ret = self.image_framework_sdk.LibapStopDetection() + # if ret != 0: + # raise RuntimeError("停止检测失败!") # 广角相机运行定位处理线程 def _location_processing_thread(self): global g_msg_cache, g_frame_cache, g_running_mode + record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False) # 打开广角摄像头 capture = cv2.VideoCapture(0) while True: @@ -335,8 +409,9 @@ class ImageFramework(EdgeComponent): src_frame_ptr, frame.shape[1], frame.shape[0], 3, c_char_p(b"PythonProcessing"), c_char_p(b"_t_PluginCameraPro")) if ret != SYS_OK: - self.logger.error(_get_msg_info(ret)) - # emit msg + record = CallbackInfo(code=ret, errorInfo=_get_msg_info(ret), bGetData=False) + msg = Msg(MSG_ONLY_RECORD, record, None) + self.signals.on_image_detect_result.emit(msg) continue # 向 g_frame_cache 中加入消息,对图像进行二次加工 g_frame_cache.append(frame) @@ -351,7 +426,4 @@ class ImageFramework(EdgeComponent): continue msg = g_msg_cache.pop() - if msg.msg_type == MSG_LOCATION_RECORD: - # cv2.imshow("LOCATION", msg.im2D) - # cv2.waitKey(1) - self.signals.on_image_detect_result.emit(msg) + self.signals.on_image_detect_result.emit(msg) diff --git a/widget/task_run.py b/widget/task_run.py index df990c1..4bc1ee9 100644 --- a/widget/task_run.py +++ b/widget/task_run.py @@ -2,9 +2,9 @@ import cv2 from PyQt5.QtCore import Qt from PyQt5.QtGui import QPixmap, QImage from PyQt5.QtWidgets import QDialog, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QScrollArea, QPushButton, \ - QSpacerItem, QSizePolicy, QLabel + QSpacerItem, QSizePolicy, QLabel, QMessageBox -from components.image_framework import MSG_LOCATION_RECORD +from components.image_framework import MSG_LOCATION_RECORD, MSG_DETECTION_RECORD from core.context import AppContext from widget.embed_item import EmbedItem @@ -42,15 +42,36 @@ class TaskRunDialog(QDialog): def image_result(self, msg): if msg.msg_type == MSG_LOCATION_RECORD: + if msg.im2D is None: + return rgb_frame = cv2.cvtColor(msg.im2D, cv2.COLOR_BGR2RGB) + try: + # 转换为 QImage + height, width, channels = rgb_frame.shape + bytes_per_line = channels * width + qt_image = QImage(rgb_frame.data, width, height, bytes_per_line, QImage.Format_RGB888) - # 转换为 QImage - height, width, channels = rgb_frame.shape - bytes_per_line = channels * width - qt_image = QImage(rgb_frame.data, width, height, bytes_per_line, QImage.Format_RGB888) - - self.view_label.setPixmap(QPixmap.fromImage(qt_image)) + self.view_label.setPixmap(QPixmap.fromImage(qt_image)) + except Exception as e: + dd = 0 + elif msg.msg_type == MSG_DETECTION_RECORD: + if msg.record.code < 0: + self.set_button_default() + QMessageBox.warning(self, '错误信息', msg.record.errorInfo.decode('utf-8')) + elif msg.record.code == 1008: + self.start_adjust_button.setEnabled(False) + self.start_check_button.setEnabled(True) + self.stop_check_button.setEnabled(True) + self.stop_task_button.setEnabled(False) + elif msg.record.code == 1012: + QMessageBox.warning(self, '成功信息', "检测结束") + self.set_button_default() + AppContext.get_edge_context().get_component('image-framework').start_location() + else: + ddd = 0 + def showEvent(self, e): + AppContext.get_edge_context().get_component('image-framework').start_location() def init_ui(self): # bim @@ -188,18 +209,18 @@ class TaskRunDialog(QDialog): self.start_check_button.setFixedHeight(self.ratio * 42) self.start_check_button.clicked.connect(self.start_check) - # self.stop_check_button = QPushButton() - # self.stop_check_button.setObjectName("stopCheckButton") - # self.stop_check_button.setEnabled(False) - # self.stop_check_button.setText("停止检测") - # self.stop_check_button.setFixedHeight(self.ratio * 42) - # self.stop_check_button.clicked.connect(self.stop_check) + self.stop_check_button = QPushButton() + self.stop_check_button.setObjectName("stopCheckButton") + self.stop_check_button.setEnabled(False) + self.stop_check_button.setText("停止检测") + self.stop_check_button.setFixedHeight(self.ratio * 42) + self.stop_check_button.clicked.connect(self.stop_check) self.stop_task_button = QPushButton() self.stop_task_button.setObjectName("stopTaskButton") self.stop_task_button.setText("结束任务") self.stop_task_button.setFixedHeight(self.ratio * 42) - self.stop_task_button.clicked.connect(self.reject) + self.stop_task_button.clicked.connect(self.stop_task) tool_widget.setLayout(tool_layout) @@ -245,34 +266,47 @@ class TaskRunDialog(QDialog): map_widget.layout().addWidget(map_area) map_area.setWidget(self.bim_widget) + self.set_button_default() + + # 校准 def start_adjust(self): self.start_adjust_button.setEnabled(False) - self.start_check_button.setEnabled(True) - # self.stop_check_button.setEnabled(False) + self.start_check_button.setEnabled(False) + self.stop_check_button.setEnabled(False) + self.stop_task_button.setEnabled(False) - AppContext.get_edge_context().get_component('image-framework').start_detect() + result = AppContext.get_edge_context().get_component('image-framework').start_adjust() + if result.code < 0: + self.set_button_default() + QMessageBox.warning(self, '校准启动失败', result.errorInfo) def start_check(self): self.start_adjust_button.setEnabled(False) self.start_check_button.setEnabled(False) - # self.stop_check_button.setEnabled(True) + self.stop_check_button.setEnabled(False) + self.stop_task_button.setEnabled(False) - index = 0 - for item in self.embed_widgets: - if item.objectName() == 'STD': continue - if self.check_widget.geometry().contains(item.rect): - item.setIndex(str(self.checkIndex)) - if index % 2 == 0: - item.setChecked() - else: - item.setError() - index += 1 + result = AppContext.get_edge_context().get_component('image-framework').start_check() + if result.code < 0: + self.set_button_default() + QMessageBox.warning(self, '检测启动失败', result.errorInfo) def stop_check(self): + self.start_adjust_button.setEnabled(False) + self.start_check_button.setEnabled(False) + self.stop_check_button.setEnabled(False) + self.stop_task_button.setEnabled(False) + result = AppContext.get_edge_context().get_component('image-framework').stop_check() + if result.code < 0: + self.set_button_default() + QMessageBox.warning(self, '检测停止失败', result.errorInfo) + + def stop_task(self): + AppContext.get_edge_context().get_component('image-framework').stop_all() + self.reject() + + def set_button_default(self): self.start_adjust_button.setEnabled(True) self.start_check_button.setEnabled(False) - # self.stop_check_button.setEnabled(False) - self.checkIndex += 1 - - def showEvent(self, e): - AppContext.get_edge_context().get_component('image-framework').start_location() \ No newline at end of file + self.stop_check_button.setEnabled(False) + self.stop_task_button.setEnabled(True) \ No newline at end of file