Merge remote-tracking branch 'origin/master'

# Conflicts:
#	components/image_framework.py
This commit is contained in:
njdaoyehu 2024-11-22 18:10:00 +08:00
commit 4f2ecd5825
3 changed files with 47 additions and 106 deletions

View File

@ -8,7 +8,6 @@ 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
@ -138,8 +137,7 @@ 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
#################################################################################### ####################################################################################
# 内部接口定义 # 内部接口定义
@ -148,10 +146,9 @@ g_running_mode = RUNNING_MODE_NONE
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:
for key, value in msg.items(): if code == msg.keys()[0]:
if code == key: return msg.values
return value.encode("utf-8") return "未知错误"
return "未知错误".encode("utf-8")
# 加工检测图像 # 加工检测图像
def _update_location_image(frame): 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 = 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):
@ -226,6 +220,7 @@ 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
@ -236,29 +231,24 @@ 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 = np.zeros((7000, 9344, 3), np.uint8) frame.zeros(9344, 7000, cv2.CV_8UC3)
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_DETECTION_RECORD, record, None) msg = Msg(MSG_ONLY_RECORD, record, None)
self.signals.on_image_detect_result.emit(msg) # emit msg
return return
msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), frame) msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), frame)
self.signals.on_image_detect_result.emit(msg) elif g_running_mode == RUNNING_MODE_LOCATION or g_running_mode == RUNNING_MODE_DETECTION:
elif g_running_mode == RUNNING_MODE_DETECTION: msg = Msg(MSG_ONLY_RECORD, _copy_record(record), None)
msg = Msg(MSG_DETECTION_RECORD, _copy_record(record), None)
self.signals.on_image_detect_result.emit(msg)
else: else:
print("未知回调") print("未知回调")
return return
@ -290,105 +280,41 @@ 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):
global g_running_mode self.location_thread.start()
if g_running_mode != RUNNING_MODE_LOCATION: self.process_thread.start()
g_running_mode = RUNNING_MODE_LOCATION # 切换为定位模式
@service() @service()
def stop_all(self): def start_detect(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 != SYS_OK: if ret != 0:
record = CallbackInfo() raise RuntimeError("启动检测失败!")
record.code = ret
record.errorInfo = _get_msg_info(ret)
return record
return record
@service() @service()
def start_check(self): def continue_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 MSG_DETECTION_RECORD
ret = self.image_framework_sdk.LibapiContinuetDetection() ret = self.image_framework_sdk.LibapiContinuetDetection()
if ret != SYS_OK: if ret != 0:
record.code = ret raise RuntimeError("继续检测失败!")
record.errorInfo = _get_msg_info(ret)
return record
return record
@service() @service()
def stop_check(self): 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: # 如果在定位任务模式下,该接口不执行
record.code = -10002 raise RuntimeError("正在检测,请稍后停止!")
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 # 切换为无模式
return record ret = self.image_framework_sdk.LibapStopDetection()
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:
@ -409,9 +335,8 @@ 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:
record = CallbackInfo(code=ret, errorInfo=_get_msg_info(ret), bGetData=False) self.logger.error(_get_msg_info(ret))
msg = Msg(MSG_ONLY_RECORD, record, None) # emit msg
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)
@ -426,4 +351,7 @@ 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

@ -9,4 +9,3 @@ dynaconf==3.2.5
fastapi==0.111.0 fastapi==0.111.0
uvicorn==0.30.1 uvicorn==0.30.1
SQLAlchemy==2.0.34 SQLAlchemy==2.0.34
opencv-python==4.10.0.84

14
util/imgProcess.py Normal file
View File

@ -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