Explorar el Código

编写服务框架

leon hace 1 semana
commit
66fd29a335
Se han modificado 2 ficheros con 56 adiciones y 0 borrados
  1. 38 0
      server.py
  2. 18 0
      utils.py

+ 38 - 0
server.py

@@ -0,0 +1,38 @@
+from pydantic import BaseModel, field_validator, model_validator, Field
+from typing import List, Optional, Generic, TypeVar
+from utils import base64_to_cv2
+from fastapi import FastAPI
+
+app = FastAPI()
+
+T = TypeVar("T")
+
+class APIRequest(BaseModel):
+    imageData : str
+    text  : str
+
+class APIResponse(BaseModel, Generic[T]):
+    success: bool
+    data: Optional[List[T]]
+    msg: Optional[List[str]]
+
+@app.post("/llm/detect")
+@app.post("/llm/detect/")
+async def detect(item: APIRequest):
+    # illegal 为 0 代表没有违章
+    # illegal 为 1 代表有违章
+    response = {
+        "sucess": "OK", 
+        "data": {"illegal" : 0}, 
+        "msg": ""
+    }
+    image = base64_to_cv2(item.imageData)
+    if not image.size:
+        response["sucess"] = "FAILED"
+        response["msg"] = "Decode Image Error"
+        return response
+    
+    # 大模型检测后如果有违章
+    # response["data"]["illegal"] = 1
+    
+    return response

+ 18 - 0
utils.py

@@ -0,0 +1,18 @@
+import base64
+import cv2
+import numpy as np
+
+# opencv读取出来的图片相当于numpy数组
+def cv2_to_base64(image):
+    image1 = cv2.imencode('.jpg', image)[1]
+    image_code = str(base64.b64encode(image1))[2:-1]
+    return image_code
+
+def base64_to_cv2(image_code):
+    #解码
+    img_data=base64.b64decode(image_code)
+    #转为numpy
+    img_array=np.fromstring(img_data,np.uint8)
+    #转成opencv可用格式
+    img=cv2.imdecode(img_array,cv2.COLOR_RGB2BGR)
+    return img