diff --git a/.idea/FristProject.iml b/.idea/FristProject.iml index 835a31e..ffc2383 100644 --- a/.idea/FristProject.iml +++ b/.idea/FristProject.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/deployment.xml b/.idea/deployment.xml index e886ec8..82fe0af 100644 --- a/.idea/deployment.xml +++ b/.idea/deployment.xml @@ -1,8 +1,15 @@ - + - + + + + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 3c03da2..cabbcc2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ - + diff --git a/core/ChannelManager.py b/core/ChannelManager.py index 06a57e8..6910885 100644 --- a/core/ChannelManager.py +++ b/core/ChannelManager.py @@ -108,8 +108,6 @@ class ChannelData: execution_time = end_time - start_time print(f"停止一个通道线程,花费了: {execution_time} seconds") - def stop_run(self): - self.bool_run = False class ChannelManager: diff --git a/core/ModelManager.py b/core/ModelManager.py index 3399e74..465d57e 100644 --- a/core/ModelManager.py +++ b/core/ModelManager.py @@ -31,16 +31,17 @@ class ModelManager: self.fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 使用 mp4 编码 #基于模型运行环境进行相应初始化工作 self.model_platform = myCongif.get_data("model_platform") - self.device_id = myCongif.get_data("device_id") - # acl初始化 -- 一个线程一个 -- 需要验证 + # acl初始化 -- 一个线程一个 if self.model_platform == "acl": + self.device_id = myCongif.get_data("device_id") ACLModeManger.init_acl(self.device_id) #acl -- 全程序初始化 - self.model_dic = {} # model_id model + #self.model_dic = {} # model_id model # 报警处理线程-全进程独立一个线程处理 self.warnM = None def __del__(self): self.logger.debug("释放资源") + self.stop_work(0) #停止所有工作 del self.verify_list #应该需要深入的删除--待完善 if self.model_platform == "acl": ACLModeManger.del_acl(self.device_id) #acl -- 全程序反初始化 需要确保在执行析构前,其它资源已释放 @@ -168,19 +169,22 @@ class ModelManager: buffer_bgr_webp = frame_bgr_webp.tobytes() return buffer_bgr_webp,img,warntext - def dowork_thread(self,channel_id,model_data,schedule,m,verify_rate,warn_interval): + def dowork_thread(self,channel_id,model_data,schedule,verify_rate,warn_interval): '''一个通道一个线程,关联的模型在一个线程检测,局部变量都是一个通道独有''' channel_data = self.verify_list.get_channel(channel_id) #是对ChannelData 对象的引用 context = None + model = self._import_model("", model_data[5], model_data[8], model_data[9]) # 动态加载模型处理文件py + if not model: + print("实例化自定义模型文件出错,退出线程!") + return # 线程ACL初始化 if self.model_platform == "acl": # ACL线程中初始化内容 context = ACLModeManger.th_inti_acl(self.device_id) - #初始化模型资源 - ret = m.init_acl_resource() + #初始化模型资源 -- 加载模型文件 + ret = model.init_acl_resource() if not ret: print("初始化模型资源出错,退出线程!") return - channel_data.b_model = True result = [0 for _ in range(model_data[3] * verify_rate)] # 初始化时间*验证帧率数量的结果list proportion = model_data[4] # 判断是否报警的占比 @@ -191,6 +195,7 @@ class ModelManager: #开始拉取画面循环检测 cap = channel_data.cap last_frame_time = time.time() #初始化个读帧时间 + channel_data.b_model = True while channel_data.bool_run: #基于tag 作为运行标识。 线程里只是读,住线程更新,最多晚一轮,应该不用线程锁。需验证 # 帧率控制帧率 current_time = time.time() @@ -203,7 +208,7 @@ class ModelManager: if not ret: continue #没读到画面继续 #执行图片推理 - buffer_bgr_webp,img_bgr_ndarray,warn_text = self.verify(frame,m,model_data,channel_id,schedule,result) + buffer_bgr_webp,img_bgr_ndarray,warn_text = self.verify(frame,model,model_data,channel_id,schedule,result) #分析图片放入内存中 channel_data.add_deque(img_bgr_ndarray) # 缓冲区大小由maxlen控制 超上限后,删除最前的数据 #channel_data.increment_counter() #帧序列加一 @@ -250,15 +255,15 @@ class ModelManager: channel_data.b_model = False if self.model_platform == "acl": # ACL线程中初始化内容 try: - m.release() #释放模型资源资源 + model.release() #释放模型资源资源 + # 删除模型对象 + del model #释放context if context: # ACL线程中反初始化内容 -- 若线程异常退出,这些资源就不能正常释放了 # 再释放context ACLModeManger.th_del_acl(context) except Exception as e: print(e) - #删除模型对象 - del m #cv2.destroyAllWindows() print("线程结束!!!!") @@ -293,7 +298,6 @@ class ModelManager: #3.启动工作线程 ************************** self.start_work_th(channel_id,c_data) - # 启动告警线程 if self.warnM is None: self.warnM = WarnManager() @@ -321,16 +325,13 @@ class ModelManager: f"from channel2model t1 left join model t2 on t1.model_id = t2.ID where t1.channel_id ={channel_id};") model_data = mDBM.do_select(strsql, 1) # 2024-7-12调整规则,一个通道只关联一个模型,表结构暂时不动 if model_data and model_data[0]: # 如果该通道关联了模型 - # 基于基类实例化模块类 - m = self._import_model("", model_data[5], model_data[8], model_data[9]) # 动态加载模型处理文件py - if m: - schedule = self.getschedule(model_data[6]) # 获取布防计划 - # 数据准备OK-开始工作线程 - c_data.bool_run = True - c_data.work_th = threading.Thread(target=self.dowork_thread, - args=(channel_id, model_data, schedule, m, verify_rate, - warn_interval)) # 一个视频通道一个线程 - c_data.work_th.start() + schedule = self.getschedule(model_data[6]) # 获取布防计划 + # 数据准备OK-开始工作线程 + c_data.bool_run = True + c_data.work_th = threading.Thread(target=self.dowork_thread, + args=(channel_id, model_data, schedule, verify_rate, + warn_interval)) # 一个视频通道一个线程 + c_data.work_th.start() def restartC2M(self,channel_id): diff --git a/model/plugins/ModelBase.py b/model/plugins/ModelBase.py index 81ea57d..c0535d3 100644 --- a/model/plugins/ModelBase.py +++ b/model/plugins/ModelBase.py @@ -89,7 +89,6 @@ class ModelBase(ABC): # print('ACL finalize Successfully') def init_acl_resource(self): - #self._init_acl() #测试使用 ''' 初始化模型、输出相关资源。相关数据类型: aclmdlDesc aclDataBuffer aclmdlDataset''' print("Init model resource") # 加载模型文件 @@ -144,6 +143,7 @@ class ModelBase(ABC): if ret == FAILED: self._release_dataset(self.input_dataset) # 失败时释放dataset self.input_dataset = None + return ret #print("[Model] create model input dataset success") def _unpack_bytes_array(self, byte_array, shape, datatype): @@ -195,7 +195,10 @@ class ModelBase(ABC): def execute(self, input_list): '''创建输入dataset对象, 推理完成后, 将输出数据转换为numpy格式''' - self._gen_input_dataset(input_list) # 创建模型输入dataset结构 + ret = self._gen_input_dataset(input_list) # 创建模型输入dataset结构 + if ret == FAILED: + self.mylogger.error(f"_gen_input_dataset fail!--{ret}") + return None ret = acl.mdl.execute(self.model_id, self.input_dataset, self.output_dataset) # 调用离线模型的execute推理数据 if ret: self.mylogger.error(f"acl.mdl.execute fail!--{ret}") diff --git a/zfbox.db b/zfbox.db index 91ed369..e6c84cd 100644 Binary files a/zfbox.db and b/zfbox.db differ