2 Commits 81acb3f6ba ... 9d59a2f358

Author SHA1 Message Date
  leon 9d59a2f358 增加服务日志 2 days ago
  leon f752e7ad01 创建请求大模型服务代码 6 days ago
3 changed files with 120 additions and 27 deletions
  1. 1 1
      Makefile
  2. 77 0
      reqllm/reqllm.py
  3. 42 26
      server/server.py

+ 1 - 1
Makefile

@@ -1,6 +1,6 @@
 start:
 	cd server && \
-	gunicorn -w 2 -b 0.0.0.0:18000 -k uvicorn.workers.UvicornWorker server:app --daemon
+	gunicorn -w 1 -b 0.0.0.0:18000 -k uvicorn.workers.UvicornWorker server:app --daemon
 	@sleep 1s
 	@ps -ef | grep gunicorn
 

+ 77 - 0
reqllm/reqllm.py

@@ -0,0 +1,77 @@
+import cv2
+import base64
+import threading
+import requests
+
+prompt_words = {
+    "phone" : "1",
+    "fall"  : "2",
+    "fire"  : "3"
+}
+
+def cv2_to_base64(image):
+    image1 = cv2.imencode('.jpg', image)[1]
+    image_code = str(base64.b64encode(image1))[2:-1]
+    return image_code
+
+
+def get_cut_position(boxes, w, h):
+    xmin, ymin, xmax, ymax = w, h, 0, 0
+    if len(boxes) == 1:
+        xmin, ymin, xmax, ymax = boxes[0]
+        box_w = xmax - xmin
+        box_h = ymax - ymin
+        xmin = max(0, xmin - box_w // 5)
+        ymin = max(0, ymin - box_h // 5)
+        xmax = min(w, xmax + box_w // 5)
+        ymax = max(h, ymax + box_h // 5)
+    else:
+        for box in boxes:
+            x1, y1, x2, y2 = box
+            xmin = min(x1, xmin)
+            ymin = min(y1, ymin)
+            xmax = max(xmax, x2)
+            ymax = max(ymax, y2)
+        box_w = xmax - xmin
+        box_h = ymax - ymin
+        xmin = max(0, xmin - box_w // 2)
+        ymin = max(0, ymin - box_h // 2)
+        xmax = min(w, xmax + box_w // 2)
+        ymax = max(h, ymax + box_h // 2)
+    return xmin, ymin, xmax, ymax
+
+def post_illegal_data():
+    pass
+
+def task(boxes, image, task_name, llm_url):
+    """
+    boxes : 违章框的列表 [[x1,y1,x2,y2], ...]
+    image : 图片的opencv格式
+    task_name : 任务名称 phone : 打电话, fall : 摔倒, fire : 烟雾火焰
+    """
+    h, w, _ = image.shape
+    xmin, ymin, xmax, ymax = get_cut_position(boxes, w, h)
+
+    crop_image = image[ymin:ymax, xmin:xmax]
+    crop_image_base64 = cv2_to_base64(crop_image)
+    prompt_word = prompt_words[task_name]
+    
+    data = {
+        "imageData" : crop_image_base64,
+        "text" : prompt_word
+    }
+    try:
+        response = requests.post(llm_url, json=data)
+    except Exception as error:
+        print(error)
+        return
+    res = response.json()
+
+    if res["data"]["illegal"] == 1:
+        post_illegal_data()
+
+
+def create_llm_task(boxes, image, task_name, llm_url):
+    llm_thread = threading.Thread(target=task, args=(boxes, image, task_name, llm_url), daemon=True)
+    llm_thread.start()
+

+ 42 - 26
server/server.py

@@ -2,10 +2,25 @@ from pydantic import BaseModel, field_validator, model_validator, Field
 from typing import List, Optional, Generic, TypeVar
 from fastapi import FastAPI
 import uvicorn
+import logging
+
 
 from qwenvl import model
 from qwenvl import processor
-from qwen_vl_utils import process_vision_info
+# from qwen_vl_utils import process_vision_info
+
+logger = logging.getLogger()
+logger.setLevel(logging.INFO)
+handler1 = logging.StreamHandler()
+handler2 = logging.FileHandler(filename='../log/llmserver.log')
+formatter = logging.Formatter(
+    "%(asctime)s - %(module)s - %(funcName)s - line:%(lineno)d - %(levelname)s - %(message)s"
+)
+handler1.setFormatter(formatter)
+handler2.setFormatter(formatter)
+logger.addHandler(handler1)  # 将日志输出至屏幕
+logger.addHandler(handler2)  # 将日志输出至文件
+
 
 app = FastAPI()
 
@@ -46,32 +61,33 @@ async def detect(item: APIRequest):
             ],
         }
     ]
-    text = processor.apply_chat_template(
-        messages, tokenize=False, add_generation_prompt=True
-    )
-    image_inputs, video_inputs = process_vision_info(messages)
-    inputs = processor(
-        text=[text],
-        images=image_inputs,
-        videos=video_inputs,
-        padding=True,
-        return_tensors="pt",
-    )
-    inputs = inputs.to("cuda")
+    # text = processor.apply_chat_template(
+    #     messages, tokenize=False, add_generation_prompt=True
+    # )
+    # image_inputs, video_inputs = process_vision_info(messages)
+    # inputs = processor(
+    #     text=[text],
+    #     images=image_inputs,
+    #     videos=video_inputs,
+    #     padding=True,
+    #     return_tensors="pt",
+    # )
+    # inputs = inputs.to("cuda")
 
-    # Inference: Generation of the output
-    generated_ids = model.generate(**inputs, max_new_tokens=128)
-    generated_ids_trimmed = [
-        out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
-    ]
-    output_text = processor.batch_decode(
-        generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
-    )
-    print(output_text)
-    # 大模型检测后如果有违章
-    # response["data"]["illegal"] = 1
-    
+    # # Inference: Generation of the output
+    # generated_ids = model.generate(**inputs, max_new_tokens=128)
+    # generated_ids_trimmed = [
+    #     out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
+    # ]
+    # output_text = processor.batch_decode(
+    #     generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
+    # )
+    # print(output_text)
+    # # 大模型检测后如果有违章
+    # # response["data"]["illegal"] = 1
+    # if "yes" in output_text[0].lower():
+    #     response["data"]["illegal"] = 1
     return response
 
 if __name__ == "__main__":
-    uvicorn.run('server:app', host="0.0.0.0", port=18000)
+    uvicorn.run('server:app', host="0.0.0.0", port=18000)