From 82ecdf13fa56a7757f26a1eed79499c3f7bc26e5 Mon Sep 17 00:00:00 2001
From: shidong <shidong@jhsoft.cc>
Date: 星期二, 08 七月 2025 19:00:10 +0800
Subject: [PATCH] #2025/7/8 #新增qwen_detect_batch.py和qwen_thread_batch.py测试批量处理图片, #在qwen_thread.py和qwen_thread_batch.py中 新增了预警之后查询隐患描述与处理建议, #新增了隐患描述的接口名称:image_rule_chat_with_detail #新增了处理建议的接口名称:image_rule_chat_suggestion #数据库增加了risk_description与suggestion字段

---
 qwen_detect.py |   92 +++++++++++++++++++++++++++-------------------
 1 files changed, 54 insertions(+), 38 deletions(-)

diff --git a/qwen_detect.py b/qwen_detect.py
index db6ccea..35f87fe 100644
--- a/qwen_detect.py
+++ b/qwen_detect.py
@@ -1,18 +1,16 @@
-# 绂佺敤tokenizer骞惰锛堝繀椤绘斁鍦ㄦ墍鏈塱mport涔嬪墠锛�
+from operator import itemgetter
 import torch
 import threading
 import time as time_sel
 from typing import Dict
-import qwen_task
+from qwen_thread import qwen_thread
 import requests
 import os
-
 import logging
 from transformers import AutoProcessor, AutoModelForVision2Seq
 from pymilvus import connections, Collection
 from logging.handlers import RotatingFileHandler
 import get_mem
-
 
 class ThreadPool:
     def __init__(self):
@@ -40,24 +38,12 @@
         self.threads: Dict[str, threading.Thread] = {}
         self.lock = threading.Lock()
 
-        self.device = "cuda" if torch.cuda.is_available() else "cpu"
-
-        # 鍒濆鍖杚wenvl妫�娴嬫ā鍨�
-        self.qwen_model = AutoModelForVision2Seq.from_pretrained(
-            "./Qwen2-VL-2B-Instruct", device_map="auto",
-            trust_remote_code=True,
-            torch_dtype=torch.float16
-        )
-
-        # default processer
-        self.qwen_tokenizer = AutoProcessor.from_pretrained("./Qwen2-VL-2B-Instruct")
-
         # 鍒濆鍖朚ilvus闆嗗悎
         connections.connect("default", host=self.config.get("milvusurl"), port=self.config.get("milvusport"))
         # 鍔犺浇闆嗗悎
         self.collection = Collection(name="smartobject")
         self.collection.load()
-
+        self.pool = qwen_thread(int(self.config.get("threadnum")), self.config,self.config.get("qwenaddr"))
         #鏄惁鏇存柊
         self._isupdate = False
 
@@ -103,31 +89,61 @@
 
     # 鍚姩绾跨▼浠诲姟
     def worker(self, camera_id):
-        # 鍒濆鍖�
-        detect = qwen_task.detect_tasks()
-        detect._thread_name = f"{camera_id}"  # 璁剧疆鍚嶇О
-        detect.init_logging(f"{camera_id}")
-        # 鍒濆鍖栨娴嬫ā鍨�
-        detect.device = self.device
-        # 鍒濆鍖杚wen妯″瀷
-        detect.qwen_tokenizer = self.qwen_tokenizer
-        detect.qwen_model = self.qwen_model
-
-        # 鍒濆鍖朚ilvus闆嗗悎
-        detect.collection = self.collection
-
-        # 璁剧疆绾跨▼鍚姩鐘舵��
-        detect._running = True
         while True:
             try:
+                res_a = self.collection.query(
+                    expr=f"is_desc == 0 and video_point_id=={camera_id}",
+                    output_fields=["id", "zh_desc_class", "text_vector", "bounding_box", "video_point_name", "task_id",
+                                   "task_name", "event_level_id", "event_level_name",
+                                   "video_point_id", "detect_num", "is_waning", "is_desc", "waning_value", "rule_id",
+                                   "detect_id",
+                                   "detect_time", "image_path", "image_desc_path", "video_path"],
+                    consistency_level="Strong",
+                    order_by_field="id",  # 鎸塱d瀛楁鎺掑簭
+                    order_by_type="desc"  # 闄嶅簭鎺掑垪
+                )
+
                 # 璇诲彇鍏变韩鍐呭瓨涓殑鍥剧墖
-                image_id = get_mem.smem_read_frame_qianwen(camera_id)
-                if image_id > 0:
-                    logging.info(f"璇诲彇鍥惧儚鎴愬姛: {image_id}")
-                    image_id = detect.tark_do(image_id,self.config.get("filesavepath"),self.config.get("ragurl"),self.config.get("max_tokens"))
-                    logging.info(f"澶勭悊鍥惧儚鎴愬姛: {image_id}")
+                # image_id = get_mem.smem_read_frame_qianwen(camera_id)
+                if len(res_a) > 0:
+                    sorted_results = sorted(res_a, key=itemgetter("id"), reverse=True)
+                    # 鏌ヨ鍓峃涓渶澶х殑ID
+                    num = int(self.config.get("threadnum")) - 1
+                    res_a = sorted_results[:num]
+                    for res in res_a:
+                        data = {
+                            "id": res['id'],
+                            "event_level_id": res['event_level_id'],  # event_level_id
+                            "event_level_name": res['event_level_name'],  # event_level_id
+                            "rule_id": res["rule_id"],
+                            "video_point_id": res['video_point_id'],  # video_point_id
+                            "video_point_name": res['video_point_name'],
+                            "is_waning": 0,
+                            "is_desc": 1,
+                            "zh_desc_class": res['zh_desc_class'],  # text_vector
+                            "bounding_box": res['bounding_box'],  # bounding_box
+                            "task_id": res['task_id'],  # task_id
+                            "task_name": res['task_name'],  # task_id
+                            "detect_id": res['detect_id'],  # detect_id
+                            "detect_time": res['detect_time'],  # detect_time
+                            "detect_num": res['detect_num'],
+                            "waning_value": res['waning_value'],
+                            "image_path": res['image_path'],  # image_path
+                            "image_desc_path": res['image_desc_path'],  # image_desc_path
+                            "video_path": res['video_path'],
+                            "text_vector": res['text_vector']
+                        }
+                        # logging.info(f"璇诲彇鍥惧儚鎴愬姛: {res['id']}")
+                        # 淇濆瓨鍒癿ilvus
+                        image_id = self.collection.upsert(data).primary_keys
+                        res['id'] = image_id[0]
+                        # logging.info(f"璇诲彇鍥惧儚鎴愬姛: {image_id}")
+                        image_id = self.pool.submit(res)
+                        # image_id = pool.tark_do(image_id,self.config.get("ragurl"),self.config.get("ragmode"),self.config.get("max_tokens"))
+                        # logging.info(f"澶勭悊鍥惧儚鎴愬姛: {image_id}")
+                    sorted_results = None
             except Exception as e:
-                logging.info(f"{detect._thread_name}绾跨▼閿欒:{e}")
+                logging.info(f"{camera_id}绾跨▼閿欒:{e}")
 
     #璋冪敤鏄惁闇�瑕佹洿鏂�
     def isUpdate(self):

--
Gitblit v1.8.0