diff --git a/components/image_framework.py b/components/image_framework.py index 323a996..edeb3bb 100644 --- a/components/image_framework.py +++ b/components/image_framework.py @@ -8,7 +8,6 @@ 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 @@ -138,8 +137,7 @@ 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_NONE +g_running_mode = RUNNING_MODE_LOCATION # 默认是广角相机运行模式 #################################################################################### # 内部接口定义 @@ -148,10 +146,9 @@ g_running_mode = RUNNING_MODE_NONE def _get_msg_info(code) -> str: global MSG_LIST for msg in MSG_LIST: - for key, value in msg.items(): - if code == key: - return value.encode("utf-8") - return "未知错误".encode("utf-8") + if code == msg.keys()[0]: + return msg.values + return "未知错误" # 加工检测图像 def _update_location_image(frame): @@ -207,9 +204,6 @@ 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): @@ -226,6 +220,7 @@ 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 @@ -236,29 +231,24 @@ 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 = np.zeros((7000, 9344, 3), np.uint8) + frame.zeros(9344, 7000, cv2.CV_8UC3) 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_DETECTION_RECORD, record, None) - self.signals.on_image_detect_result.emit(msg) + msg = Msg(MSG_ONLY_RECORD, record, None) + # emit msg return msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), frame) - 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) + elif g_running_mode == RUNNING_MODE_LOCATION or g_running_mode == RUNNING_MODE_DETECTION: + msg = Msg(MSG_ONLY_RECORD, _copy_record(record), None) else: print("未知回调") return @@ -290,105 +280,41 @@ class ImageFramework(EdgeComponent): self.image_framework_sdk.stop_sdk() self.logger.info("Image framework stopped.") - # 启动检测 @service() def start_location(self): - global g_running_mode - if g_running_mode != RUNNING_MODE_LOCATION: - g_running_mode = RUNNING_MODE_LOCATION # 切换为定位模式 + self.location_thread.start() + self.process_thread.start() @service() - def stop_all(self): + def start_detect(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 != SYS_OK: - record = CallbackInfo() - record.code = ret - record.errorInfo = _get_msg_info(ret) - return record - return record + if ret != 0: + raise RuntimeError("启动检测失败!") @service() - def start_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 MSG_DETECTION_RECORD + def continue_detect(self): ret = self.image_framework_sdk.LibapiContinuetDetection() - if ret != SYS_OK: - record.code = ret - record.errorInfo = _get_msg_info(ret) - return record - return record + if ret != 0: + raise RuntimeError("继续检测失败!") @service() - def stop_check(self): + def stop_detect(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 + raise RuntimeError("正在检测,请稍后停止!") + g_running_mode = RUNNING_MODE_NONE # 切换为无模式 - 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("停止检测失败!") + 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: @@ -409,9 +335,8 @@ 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: - 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) + self.logger.error(_get_msg_info(ret)) + # emit msg continue # 向 g_frame_cache 中加入消息,对图像进行二次加工 g_frame_cache.append(frame) @@ -426,4 +351,7 @@ class ImageFramework(EdgeComponent): continue msg = g_msg_cache.pop() - self.signals.on_image_detect_result.emit(msg) + if msg.msg_type == MSG_LOCATION_RECORD: + # cv2.imshow("LOCATION", msg.im2D) + # cv2.waitKey(1) + self.signals.on_image_detect_result.emit(msg) diff --git a/req.txt b/req.txt index 4746cb4..6141723 100644 --- a/req.txt +++ b/req.txt @@ -8,5 +8,4 @@ paho-mqtt==2.1.0 dynaconf==3.2.5 fastapi==0.111.0 uvicorn==0.30.1 -SQLAlchemy==2.0.34 -opencv-python==4.10.0.84 \ No newline at end of file +SQLAlchemy==2.0.34 \ No newline at end of file diff --git a/util/imgProcess.py b/util/imgProcess.py new file mode 100644 index 0000000..549721f --- /dev/null +++ b/util/imgProcess.py @@ -0,0 +1,14 @@ +import os +import threading +from ctypes import * +import numpy as np +from PyQt5.QtCore import QObject, pyqtSignal +from dynaconf.base import Settings +from numpy.ctypeslib import as_array +import platform +import time +import cv2 +import imgProcess + +def process(msg): + return msg \ No newline at end of file