后端更新
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
import config.minIO as minIO
|
||||
import db.postgres as pg
|
||||
from agent.licenseImageAgent import get_license_response
|
||||
from agent.vehicleImageAgent import get_vehicle_response
|
||||
from ai.plate.my_plate import recognizer
|
||||
from config.minIO import minio_client, get_temp_url
|
||||
from config.yolo import YOLOSingleton
|
||||
from db.postgres import (
|
||||
get_dept_id_by_iot_user_name,
|
||||
get_dept_ids_by_dept_id,
|
||||
)
|
||||
from db.postgres.sentinel import update_sentinel_record, get_sentinel_record_by_id
|
||||
from db.postgres.sentinel import (
|
||||
get_sentinel_record_by_id,
|
||||
saveSentinelRecord,
|
||||
)
|
||||
from llm.ticketLLM import *
|
||||
from llm.ticketLLMv2 import get_ticket_response_v2
|
||||
from models.SentinelRecordRequest import SentinelRecordRequest
|
||||
from routers.WS import ws_manager
|
||||
from utils import validate_plate
|
||||
|
||||
|
||||
def process_ticket_image(
|
||||
@@ -189,35 +195,97 @@ def process_silkworm_cocoon_image(
|
||||
}
|
||||
|
||||
|
||||
async def process_vehicle_animal_image(
|
||||
# 处理车牌照片
|
||||
async def process_all_vehicle_animal_image(
|
||||
data: SentinelRecordRequest,
|
||||
):
|
||||
# 通过设备id获得组织id
|
||||
dept_id = get_dept_id_by_iot_user_name(data.DeviceId)
|
||||
oss_url = minIO.get_temp_url(
|
||||
"sentinel", "vehicle_image_side/" + data.vehicleImageSide
|
||||
)
|
||||
|
||||
# 得到动物类型
|
||||
oss_url = minIO.get_temp_url("sentinel", "vehicle_image/" + data.VehicleImage)
|
||||
# LLM得到车身信息
|
||||
analysis_result = await get_vehicle_response(oss_url)
|
||||
livestock_type = analysis_result.get("livestock_type", "")
|
||||
remark = analysis_result.get("remark", "")
|
||||
# 保存到数据库
|
||||
update_sentinel_record(data.Id, livestock_type, remark, dept_id)
|
||||
# 可以通知的部门ids
|
||||
available_departments = get_dept_ids_by_dept_id(dept_id)
|
||||
have_animal = analysis_result.get("have_animal", False)
|
||||
|
||||
# 通知控制界面
|
||||
await ws_manager.noticeSentinel(
|
||||
{
|
||||
"content": f"载有{livestock_type}的车辆即将进入关卡,请准备检查",
|
||||
"type": "vehicle_alert",
|
||||
},
|
||||
available_departments,
|
||||
)
|
||||
# 通知大屏界面
|
||||
await ws_manager.noticeSentinelMonitorStatus(
|
||||
{
|
||||
"content": get_sentinel_record_by_id(data.Id),
|
||||
"type": "vehicle_alert",
|
||||
},
|
||||
available_departments,
|
||||
)
|
||||
if not have_animal:
|
||||
minIO.delete_file("sentinel", "vehicle_image_side/" + data.vehicleImageSide)
|
||||
minIO.delete_file("sentinel", "vehicle_image_front/" + data.vehicleImageFront)
|
||||
else:
|
||||
# 通过设备id获得组织id
|
||||
dept_id = get_dept_id_by_iot_user_name(data.DeviceId)
|
||||
|
||||
# 处理汽车正面照--------------------------
|
||||
|
||||
# 从OSS下载
|
||||
oss_url = minIO.get_temp_url(
|
||||
"sentinel", "vehicle_image_front/" + data.vehicleImageFront
|
||||
)
|
||||
|
||||
# 获取系统临时目录(自动兼容 Windows / Linux)
|
||||
tmp_dir = Path(tempfile.gettempdir())
|
||||
tmp_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
tmp_path = tmp_dir / data.vehicleImageFront
|
||||
|
||||
# 下载图片到 tmp_path
|
||||
response = requests.get(oss_url, stream=True)
|
||||
if response.status_code == 200:
|
||||
with open(tmp_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
else:
|
||||
raise Exception(f"下载失败: {oss_url}, status_code={response.status_code}")
|
||||
|
||||
# 调用识别
|
||||
results = recognizer.analyze_image(str(tmp_path))
|
||||
# 车牌号一次识别
|
||||
license_plate = results[0].plate_no if results else ""
|
||||
# 车牌号二次校准
|
||||
license_plate = validate_plate(license_plate)
|
||||
license_plate_color_str = results[0].plate_color if results else ""
|
||||
color_map = {
|
||||
"蓝色": 0,
|
||||
"黄色": 1,
|
||||
"绿色": 2,
|
||||
"黑色": 3,
|
||||
"白色": 4,
|
||||
}
|
||||
|
||||
license_plate_color = color_map.get(license_plate_color_str, 0)
|
||||
license_plate_image = data.vehicleImageFront
|
||||
# 保存到数据库
|
||||
saveSentinelRecord(
|
||||
data.Id,
|
||||
data.VehicleType,
|
||||
data.vehicleImageSide,
|
||||
livestock_type,
|
||||
remark,
|
||||
dept_id,
|
||||
license_plate,
|
||||
license_plate_image,
|
||||
license_plate_color,
|
||||
)
|
||||
# 识别完成后删除临时文件
|
||||
os.remove(tmp_path)
|
||||
|
||||
# 可以通知的部门ids
|
||||
available_departments = get_dept_ids_by_dept_id(dept_id)
|
||||
# 通知控制界面
|
||||
await ws_manager.noticeSentinel(
|
||||
{
|
||||
"content": f"车辆即将进入关卡,请准备检查",
|
||||
"license_plate": license_plate,
|
||||
"type": "vehicle_alert",
|
||||
},
|
||||
available_departments,
|
||||
)
|
||||
# 通知大屏界面
|
||||
await ws_manager.noticeSentinelMonitorStatus(
|
||||
{
|
||||
"content": get_sentinel_record_by_id(data.Id),
|
||||
"type": "vehicle_alert",
|
||||
},
|
||||
available_departments,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user