This commit is contained in:
njdaoyehu 2024-11-22 18:09:21 +08:00
parent 934855af80
commit 3f4dc076a5
2 changed files with 174 additions and 68 deletions

View File

@ -8,6 +8,7 @@ from numpy.ctypeslib import as_array
import platform import platform
import time import time
import cv2 import cv2
from paho.mqtt.publish import single
from core.edge_component import action, EdgeComponent, service from core.edge_component import action, EdgeComponent, service
from util import get_system_and_library_suffix from util import get_system_and_library_suffix
@ -137,7 +138,8 @@ g_frame_cache = []
RUNNING_MODE_NONE = -1 # 停止状态 RUNNING_MODE_NONE = -1 # 停止状态
RUNNING_MODE_LOCATION = 0 # 广角相机运行模式 RUNNING_MODE_LOCATION = 0 # 广角相机运行模式
RUNNING_MODE_DETECTION = 1 # 高清检测运行模式 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: def _get_msg_info(code) -> str:
global MSG_LIST global MSG_LIST
for msg in MSG_LIST: for msg in MSG_LIST:
if code == msg.keys()[0]: for key, value in msg.items():
return msg.values if code == key:
return "未知错误" return value.encode("utf-8")
return "未知错误".encode("utf-8")
# 加工检测图像 # 加工检测图像
def _update_location_image(frame): 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 = threading.Thread(target=self._main_processing_thread)
self.process_thread.daemon = True self.process_thread.daemon = True
self.location_thread.start()
self.process_thread.start()
self.logger.info(f"Image framework configure done.") self.logger.info(f"Image framework configure done.")
def init_image_framework_sdk(self): def init_image_framework_sdk(self):
@ -220,7 +226,6 @@ class ImageFramework(EdgeComponent):
def init_callback(self): def init_callback(self):
def _callback(data): def _callback(data):
global g_msg_cache, g_frame_cache, g_running_mode global g_msg_cache, g_frame_cache, g_running_mode
record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False) record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False)
frame = None frame = None
ret = SYS_OK ret = SYS_OK
@ -231,24 +236,29 @@ class ImageFramework(EdgeComponent):
if len(g_frame_cache) == 0: if len(g_frame_cache) == 0:
self.logger.error("当前没有广角数据") self.logger.error("当前没有广角数据")
return return
frame = g_frame_cache.pop() # 从图像缓存中去除图像 frame = g_frame_cache.pop() # 从图像缓存中去除图像
g_frame_cache = [] # 清空缓存,避免无限增长 g_frame_cache = [] # 清空缓存,避免无限增长
msg = Msg(MSG_LOCATION_RECORD, _copy_record(record), frame) 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): 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()) src_frame_ptr = c_char_p(frame.data.tobytes())
ret = self.image_framework_sdk.LibapiGetHQImage( ret = self.image_framework_sdk.LibapiGetHQImage(
src_frame_ptr, frame.shape[1], frame.shape[0], 3, c_char_p(b"PythonProcessing")) src_frame_ptr, frame.shape[1], frame.shape[0], 3, c_char_p(b"PythonProcessing"))
if ret != SYS_OK: if ret != SYS_OK:
record = CallbackInfo(code=ret, errorInfo=_get_msg_info(ret), bGetData=False) record = CallbackInfo(code=ret, errorInfo=_get_msg_info(ret), bGetData=False)
msg = Msg(MSG_ONLY_RECORD, record, None) msg = Msg(MSG_DETECTION_RECORD, record, None)
# emit msg self.signals.on_image_detect_result.emit(msg)
return return
msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), frame) msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), frame)
elif g_running_mode == RUNNING_MODE_LOCATION or g_running_mode == RUNNING_MODE_DETECTION: self.signals.on_image_detect_result.emit(msg)
msg = Msg(MSG_ONLY_RECORD, _copy_record(record), None) 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: else:
print("未知回调") print("未知回调")
return return
@ -280,41 +290,105 @@ class ImageFramework(EdgeComponent):
self.image_framework_sdk.stop_sdk() self.image_framework_sdk.stop_sdk()
self.logger.info("Image framework stopped.") self.logger.info("Image framework stopped.")
# 启动检测
@service() @service()
def start_location(self): def start_location(self):
self.location_thread.start() global g_running_mode
self.process_thread.start() if g_running_mode != RUNNING_MODE_LOCATION:
g_running_mode = RUNNING_MODE_LOCATION # 切换为定位模式
@service() @service()
def start_detect(self): def stop_all(self):
global g_running_mode 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: if g_running_mode != RUNNING_MODE_DETECTION:
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() ret = self.image_framework_sdk.LibapiStartDetection()
if ret != 0: if ret != SYS_OK:
raise RuntimeError("启动检测失败!") record = CallbackInfo()
record.code = ret
record.errorInfo = _get_msg_info(ret)
return record
return record
@service() @service()
def continue_detect(self): def start_check(self):
ret = self.image_framework_sdk.LibapiContinuetDetection()
if ret != 0:
raise RuntimeError("继续检测失败!")
@service()
def stop_detect(self):
global g_running_mode global g_running_mode
record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False)
if g_running_mode == RUNNING_MODE_LOCATION: # 如果在定位任务模式下,该接口不执行 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 # 切换为无模式 g_running_mode = RUNNING_MODE_NONE # 切换为无模式
ret = self.image_framework_sdk.LibapStopDetection() return record
if ret != 0:
raise RuntimeError("停止检测失败!")
# @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): def _location_processing_thread(self):
global g_msg_cache, g_frame_cache, g_running_mode global g_msg_cache, g_frame_cache, g_running_mode
record = CallbackInfo(code=SYS_OK, errorInfo=b"", bGetData=False)
# 打开广角摄像头 # 打开广角摄像头
capture = cv2.VideoCapture(0) capture = cv2.VideoCapture(0)
while True: while True:
@ -335,8 +409,9 @@ class ImageFramework(EdgeComponent):
src_frame_ptr, frame.shape[1], frame.shape[0], 3, src_frame_ptr, frame.shape[1], frame.shape[0], 3,
c_char_p(b"PythonProcessing"), c_char_p(b"_t_PluginCameraPro")) c_char_p(b"PythonProcessing"), c_char_p(b"_t_PluginCameraPro"))
if ret != SYS_OK: if ret != SYS_OK:
self.logger.error(_get_msg_info(ret)) record = CallbackInfo(code=ret, errorInfo=_get_msg_info(ret), bGetData=False)
# emit msg msg = Msg(MSG_ONLY_RECORD, record, None)
self.signals.on_image_detect_result.emit(msg)
continue continue
# 向 g_frame_cache 中加入消息,对图像进行二次加工 # 向 g_frame_cache 中加入消息,对图像进行二次加工
g_frame_cache.append(frame) g_frame_cache.append(frame)
@ -351,7 +426,4 @@ class ImageFramework(EdgeComponent):
continue continue
msg = g_msg_cache.pop() 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)

View File

@ -2,9 +2,9 @@ import cv2
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QDialog, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QScrollArea, QPushButton, \ 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 core.context import AppContext
from widget.embed_item import EmbedItem from widget.embed_item import EmbedItem
@ -42,15 +42,36 @@ class TaskRunDialog(QDialog):
def image_result(self, msg): def image_result(self, msg):
if msg.msg_type == MSG_LOCATION_RECORD: if msg.msg_type == MSG_LOCATION_RECORD:
if msg.im2D is None:
return
rgb_frame = cv2.cvtColor(msg.im2D, cv2.COLOR_BGR2RGB) rgb_frame = cv2.cvtColor(msg.im2D, cv2.COLOR_BGR2RGB)
try:
# 转换为 QImage # 转换为 QImage
height, width, channels = rgb_frame.shape height, width, channels = rgb_frame.shape
bytes_per_line = channels * width bytes_per_line = channels * width
qt_image = QImage(rgb_frame.data, width, height, bytes_per_line, QImage.Format_RGB888) 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): def init_ui(self):
# bim # bim
@ -188,18 +209,18 @@ class TaskRunDialog(QDialog):
self.start_check_button.setFixedHeight(self.ratio * 42) self.start_check_button.setFixedHeight(self.ratio * 42)
self.start_check_button.clicked.connect(self.start_check) self.start_check_button.clicked.connect(self.start_check)
# self.stop_check_button = QPushButton() self.stop_check_button = QPushButton()
# self.stop_check_button.setObjectName("stopCheckButton") self.stop_check_button.setObjectName("stopCheckButton")
# self.stop_check_button.setEnabled(False) self.stop_check_button.setEnabled(False)
# self.stop_check_button.setText("停止检测") self.stop_check_button.setText("停止检测")
# self.stop_check_button.setFixedHeight(self.ratio * 42) self.stop_check_button.setFixedHeight(self.ratio * 42)
# self.stop_check_button.clicked.connect(self.stop_check) self.stop_check_button.clicked.connect(self.stop_check)
self.stop_task_button = QPushButton() self.stop_task_button = QPushButton()
self.stop_task_button.setObjectName("stopTaskButton") self.stop_task_button.setObjectName("stopTaskButton")
self.stop_task_button.setText("结束任务") self.stop_task_button.setText("结束任务")
self.stop_task_button.setFixedHeight(self.ratio * 42) 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) tool_widget.setLayout(tool_layout)
@ -245,34 +266,47 @@ class TaskRunDialog(QDialog):
map_widget.layout().addWidget(map_area) map_widget.layout().addWidget(map_area)
map_area.setWidget(self.bim_widget) map_area.setWidget(self.bim_widget)
self.set_button_default()
# 校准
def start_adjust(self): def start_adjust(self):
self.start_adjust_button.setEnabled(False) self.start_adjust_button.setEnabled(False)
self.start_check_button.setEnabled(True) self.start_check_button.setEnabled(False)
# self.stop_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): def start_check(self):
self.start_adjust_button.setEnabled(False) self.start_adjust_button.setEnabled(False)
self.start_check_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 result = AppContext.get_edge_context().get_component('image-framework').start_check()
for item in self.embed_widgets: if result.code < 0:
if item.objectName() == 'STD': continue self.set_button_default()
if self.check_widget.geometry().contains(item.rect): QMessageBox.warning(self, '检测启动失败', result.errorInfo)
item.setIndex(str(self.checkIndex))
if index % 2 == 0:
item.setChecked()
else:
item.setError()
index += 1
def stop_check(self): 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_adjust_button.setEnabled(True)
self.start_check_button.setEnabled(False) self.start_check_button.setEnabled(False)
# self.stop_check_button.setEnabled(False) self.stop_check_button.setEnabled(False)
self.checkIndex += 1 self.stop_task_button.setEnabled(True)
def showEvent(self, e):
AppContext.get_edge_context().get_component('image-framework').start_location()