可以成功动态更换yolo模型并重启服务生效
This commit is contained in:
8
app.py
8
app.py
@ -52,10 +52,10 @@ BASE_IMAGE_DIR_UP_IMAGES = str((PROJECT_ROOT / "up_images").resolve())
|
|||||||
BASE_MODEL_DIR = str((PROJECT_ROOT / "resource" / "models").resolve()) # 模型文件目录
|
BASE_MODEL_DIR = str((PROJECT_ROOT / "resource" / "models").resolve()) # 模型文件目录
|
||||||
|
|
||||||
# 打印路径配置(调试用,确认目录正确)
|
# 打印路径配置(调试用,确认目录正确)
|
||||||
logger.info(f"[Flask 配置] 项目根目录:{PROJECT_ROOT}")
|
# logger.info(f"[Flask 配置] 项目根目录:{PROJECT_ROOT}")
|
||||||
logger.info(f"[Flask 配置] 模型目录:{BASE_MODEL_DIR}")
|
# logger.info(f"[Flask 配置] 模型目录:{BASE_MODEL_DIR}")
|
||||||
logger.info(f"[Flask 配置] 人脸图片目录:{BASE_IMAGE_DIR_UP_IMAGES}")
|
# logger.info(f"[Flask 配置] 人脸图片目录:{BASE_IMAGE_DIR_UP_IMAGES}")
|
||||||
logger.info(f"[Flask 配置] 检测图片目录:{BASE_IMAGE_DIR_DECT}")
|
# logger.info(f"[Flask 配置] 检测图片目录:{BASE_IMAGE_DIR_DECT}")
|
||||||
|
|
||||||
# ------------------------------
|
# ------------------------------
|
||||||
# 安全检查装饰器(不变,防路径遍历/非法文件)
|
# 安全检查装饰器(不变,防路径遍历/非法文件)
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import os
|
|
||||||
import datetime
|
import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@ -9,19 +8,19 @@ def create_directory_structure():
|
|||||||
# 1. 创建根目录下的resource文件夹(存在则跳过,不覆盖子内容)
|
# 1. 创建根目录下的resource文件夹(存在则跳过,不覆盖子内容)
|
||||||
resource_dir = Path("resource")
|
resource_dir = Path("resource")
|
||||||
resource_dir.mkdir(exist_ok=True)
|
resource_dir.mkdir(exist_ok=True)
|
||||||
print(f"确保resource目录存在: {resource_dir.absolute()}")
|
# print(f"确保resource目录存在: {resource_dir.absolute()}")
|
||||||
|
|
||||||
# 2. 在resource下创建dect文件夹
|
# 2. 在resource下创建dect文件夹
|
||||||
dect_dir = resource_dir / "dect"
|
dect_dir = resource_dir / "dect"
|
||||||
dect_dir.mkdir(exist_ok=True)
|
dect_dir.mkdir(exist_ok=True)
|
||||||
print(f"确保dect目录存在: {dect_dir.absolute()}")
|
# print(f"确保dect目录存在: {dect_dir.absolute()}")
|
||||||
|
|
||||||
# 3. 在dect下创建三个模型文件夹
|
# 3. 在dect下创建三个模型文件夹
|
||||||
model_dirs = ["ocr", "face", "yolo"]
|
model_dirs = ["ocr", "face", "yolo"]
|
||||||
for model in model_dirs:
|
for model in model_dirs:
|
||||||
model_dir = dect_dir / model
|
model_dir = dect_dir / model
|
||||||
model_dir.mkdir(exist_ok=True)
|
model_dir.mkdir(exist_ok=True)
|
||||||
print(f"确保{model}模型目录存在: {model_dir.absolute()}")
|
# print(f"确保{model}模型目录存在: {model_dir.absolute()}")
|
||||||
|
|
||||||
# 4. 调用外部方法获取所有客户端IP地址
|
# 4. 调用外部方法获取所有客户端IP地址
|
||||||
try:
|
try:
|
||||||
@ -59,7 +58,7 @@ def create_directory_structure():
|
|||||||
|
|
||||||
# 递归创建目录(存在则跳过,不覆盖)
|
# 递归创建目录(存在则跳过,不覆盖)
|
||||||
month_dir.mkdir(parents=True, exist_ok=True)
|
month_dir.mkdir(parents=True, exist_ok=True)
|
||||||
print(f"为客户端IP {ip} 创建/确保目录存在: {month_dir.absolute()}")
|
# print(f"为客户端IP {ip} 创建/确保目录存在: {month_dir.absolute()}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"处理客户端IP和日期目录时发生错误: {str(e)}")
|
print(f"处理客户端IP和日期目录时发生错误: {str(e)}")
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
|
||||||
import gc
|
import gc
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
from PIL import Image
|
|
||||||
from insightface.app import FaceAnalysis
|
from insightface.app import FaceAnalysis
|
||||||
# 假设service.face_service中get_all_face_name_with_eigenvalue可获取人脸数据
|
|
||||||
from service.face_service import get_all_face_name_with_eigenvalue
|
from service.face_service import get_all_face_name_with_eigenvalue
|
||||||
|
|
||||||
# GPU状态检查支持
|
# GPU状态检查支持
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import os
|
|
||||||
import numpy as np
|
|
||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
from service.model_service import get_current_yolo_model # 带版本校验的模型获取
|
from service.model_service import get_current_yolo_model # 带版本校验的模型获取
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime, date
|
from datetime import date
|
||||||
|
|
||||||
from fastapi import APIRouter, Query, HTTPException, Request, Path
|
from fastapi import APIRouter, Query, HTTPException, Request, Path
|
||||||
from mysql.connector import Error as MySQLError
|
from mysql.connector import Error as MySQLError
|
||||||
|
@ -12,7 +12,7 @@ def save_face_to_up_images(
|
|||||||
) -> Dict[str, str]:
|
) -> Dict[str, str]:
|
||||||
"""
|
"""
|
||||||
保存人脸图片到 `/up_images/用户IP/人脸名字/` 路径
|
保存人脸图片到 `/up_images/用户IP/人脸名字/` 路径
|
||||||
修复路径计算错误,确保所有路径在up_images根目录下,且统一使用正斜杠
|
确保db_path以up_images开头,且统一使用正斜杠
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
client_ip: 客户端IP(原始格式,如192.168.1.101)
|
client_ip: 客户端IP(原始格式,如192.168.1.101)
|
||||||
@ -38,7 +38,7 @@ def save_face_to_up_images(
|
|||||||
safe_face_name = "".join([c for c in safe_face_name if c not in r'\/:*?"<>|']) # 过滤非法字符
|
safe_face_name = "".join([c for c in safe_face_name if c not in r'\/:*?"<>|']) # 过滤非法字符
|
||||||
|
|
||||||
# 3. 构建根目录(强制转为绝对路径,避免相对路径混淆)
|
# 3. 构建根目录(强制转为绝对路径,避免相对路径混淆)
|
||||||
root_dir = Path("up_images").resolve() # 转为绝对路径
|
root_dir = Path("up_images").resolve() # 转为绝对路径(如D:/Git/bin/video/up_images)
|
||||||
if not root_dir.exists():
|
if not root_dir.exists():
|
||||||
root_dir.mkdir(parents=True, exist_ok=True)
|
root_dir.mkdir(parents=True, exist_ok=True)
|
||||||
print(f"[FileUtil] 已创建up_images根目录:{root_dir}")
|
print(f"[FileUtil] 已创建up_images根目录:{root_dir}")
|
||||||
@ -60,9 +60,9 @@ def save_face_to_up_images(
|
|||||||
if not local_abs_path.resolve().is_relative_to(root_dir.resolve()):
|
if not local_abs_path.resolve().is_relative_to(root_dir.resolve()):
|
||||||
raise Exception(f"图片路径不在up_images根目录下(安全校验失败):{local_abs_path}")
|
raise Exception(f"图片路径不在up_images根目录下(安全校验失败):{local_abs_path}")
|
||||||
|
|
||||||
# 数据库存储路径:从root_dir开始的相对路径,强制替换为正斜杠
|
# 数据库存储路径:强制包含up_images前缀,统一使用正斜杠
|
||||||
relative_path = local_abs_path.relative_to(root_dir)
|
relative_path = local_abs_path.relative_to(root_dir.parent) # 相对于root_dir的父目录
|
||||||
db_path = str(relative_path).replace("\\", "/") # 关键修复:统一使用正斜杠
|
db_path = str(relative_path).replace("\\", "/") # 此时会包含up_images部分
|
||||||
|
|
||||||
# 7. 写入图片文件
|
# 7. 写入图片文件
|
||||||
with open(local_abs_path, "wb") as f:
|
with open(local_abs_path, "wb") as f:
|
||||||
@ -73,8 +73,8 @@ def save_face_to_up_images(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"db_path": db_path, # 存数据库的相对路径(使用正斜杠)
|
"db_path": db_path, # 格式为 up_images/192_168_110_31/小龙/xxx.jpg
|
||||||
"local_abs_path": str(local_abs_path), # 本地绝对路径
|
"local_abs_path": str(local_abs_path), # 本地绝对路径(完整路径)
|
||||||
"msg": "图片保存成功"
|
"msg": "图片保存成功"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user