识别结果保存到对应目录下后不显示完整路径

This commit is contained in:
2025-09-09 17:09:34 +08:00
parent 532a9e75e9
commit d3c4820b73
2 changed files with 34 additions and 26 deletions

View File

@ -38,31 +38,34 @@ def detect(frame):
yolo_flag, yolo_result = yoloDetect(frame) yolo_flag, yolo_result = yoloDetect(frame)
print(f"YOLO检测结果{yolo_result}") print(f"YOLO检测结果{yolo_result}")
if yolo_flag: if yolo_flag:
# 直接调用路径生成函数,无需传入原始图片名 # 元组解构:获取「完整保存路径」和「显示用短路径」
save_path = get_image_save_path(model_type="yolo") full_save_path, display_path = get_image_save_path(model_type="yolo")
if save_path: if full_save_path: # 只判断完整路径是否有效(用于保存)
cv2.imwrite(save_path, frame) cv2.imwrite(full_save_path, frame)
print(f"✅ YOLO违规图片已保存{save_path}") # 打印时使用「显示用短路径」,符合需求格式
print(f"✅ YOLO违规图片已保存{display_path}")
return (True, yolo_result, "yolo") return (True, yolo_result, "yolo")
# 2. 人脸检测优先级2 # 2. 人脸检测优先级2
face_flag, face_result = faceDetect(frame) face_flag, face_result = faceDetect(frame)
print(f"人脸检测结果:{face_result}") print(f"人脸检测结果:{face_result}")
if face_flag: if face_flag:
save_path = get_image_save_path(model_type="face") # 同样解构元组,分离保存路径和显示路径
if save_path: full_save_path, display_path = get_image_save_path(model_type="face")
cv2.imwrite(save_path, frame) if full_save_path:
print(f"✅ 人脸违规图片已保存:{save_path}") cv2.imwrite(full_save_path, frame)
print(f"✅ 人脸违规图片已保存:{display_path}")
return (True, face_result, "face") return (True, face_result, "face")
# 3. OCR检测优先级3 # 3. OCR检测优先级3
ocr_flag, ocr_result = ocrDetect(frame) ocr_flag, ocr_result = ocrDetect(frame)
print(f"OCR检测结果{ocr_result}") print(f"OCR检测结果{ocr_result}")
if ocr_flag: if ocr_flag:
save_path = get_image_save_path(model_type="ocr") # 解构元组,保存用完整路径,打印用短路径
if save_path: full_save_path, display_path = get_image_save_path(model_type="ocr")
cv2.imwrite(save_path, frame) if full_save_path:
print(f"✅ OCR违规图片已保存{save_path}") cv2.imwrite(full_save_path, frame)
print(f"✅ OCR违规图片已保存{display_path}")
return (True, ocr_result, "ocr") return (True, ocr_result, "ocr")
# 4. 无违规内容(不保存图片) # 4. 无违规内容(不保存图片)

View File

@ -45,8 +45,8 @@ def create_directory_structure():
# 6. 为每个IP在每个模型文件夹下创建年->月的目录结构 # 6. 为每个IP在每个模型文件夹下创建年->月的目录结构
for ip in ip_addresses: for ip in ip_addresses:
# 处理IP地址中的特殊字符如果有 # 直接使用原始IP格式
safe_ip = ip.replace(".", "_") safe_ip = ip
for model in model_dirs: for model in model_dirs:
# 构建路径: resource/dect/{model}/{ip}/{year}/{month} # 构建路径: resource/dect/{model}/{ip}/{year}/{month}
@ -67,15 +67,15 @@ def create_directory_structure():
print(f"创建目录结构时发生错误: {str(e)}") print(f"创建目录结构时发生错误: {str(e)}")
def get_image_save_path(model_type: str) -> str: def get_image_save_path(model_type: str) -> tuple:
""" """
获取图片保存的完整路径(不依赖原始图片名称) 获取图片保存的完整路径和显示用路径
参数: 参数:
model_type: 模型类型,应为"ocr""face""yolo" model_type: 模型类型,应为"ocr""face""yolo"
返回: 返回:
完整的图片保存路径 元组 (完整保存路径, 显示用路径)
""" """
try: try:
# 读取IP地址假设只有一个IP或使用第一个IP # 读取IP地址假设只有一个IP或使用第一个IP
@ -86,7 +86,7 @@ def get_image_save_path(model_type: str) -> str:
raise ValueError("ip.txt文件中未找到有效的IP地址") raise ValueError("ip.txt文件中未找到有效的IP地址")
ip = ip_addresses[0] ip = ip_addresses[0]
safe_ip = ip.replace(".", "_") safe_ip = ip # 直接使用原始IP格式
# 获取当前日期和时间(精确到毫秒,确保文件名唯一) # 获取当前日期和时间(精确到毫秒,确保文件名唯一)
now = datetime.datetime.now() now = datetime.datetime.now()
@ -96,16 +96,21 @@ def get_image_save_path(model_type: str) -> str:
# 生成时间戳字符串(格式:年月日时分秒毫秒) # 生成时间戳字符串(格式:年月日时分秒毫秒)
timestamp = now.strftime("%Y%m%d%H%M%S%f")[:-3] # 去除最后三位,保留到毫秒 timestamp = now.strftime("%Y%m%d%H%M%S%f")[:-3] # 去除最后三位,保留到毫秒
# 构建路径: resource/dect/{model}/{ip}/{year}/{month}/{day} # 构建基础目录路径
day_dir = Path("resource") / "dect" / model_type / safe_ip / current_year / current_month / current_day base_dir = Path("resource") / "dect"
# 构建完整路径: resource/dect/{model}/{ip}/{year}/{month}/{day}
day_dir = base_dir / model_type / safe_ip / current_year / current_month / current_day
day_dir.mkdir(parents=True, exist_ok=True) day_dir.mkdir(parents=True, exist_ok=True)
# 构建图片文件名(使用时间戳确保唯一性 # 构建图片文件名(简化名称去掉resource_dect_前缀
image_filename = f"resource_dect_{model_type}_{safe_ip}_{current_year}_{current_month}_{current_day}_{timestamp}.jpg" image_filename = f"{model_type}_{safe_ip}_{current_year}_{current_month}_{current_day}_{timestamp}.jpg"
image_path = day_dir / image_filename full_path = day_dir / image_filename
return str(image_path) # 计算显示用路径相对于resource/dect的路径
display_path = full_path.relative_to(base_dir)
return str(full_path), str(display_path)
except Exception as e: except Exception as e:
print(f"获取图片保存路径时发生错误: {str(e)}") print(f"获取图片保存路径时发生错误: {str(e)}")
return "" return "", ""