diff --git a/core/all.py b/core/all.py index b070a2a..50f89fb 100644 --- a/core/all.py +++ b/core/all.py @@ -38,31 +38,34 @@ def detect(frame): yolo_flag, yolo_result = yoloDetect(frame) print(f"YOLO检测结果:{yolo_result}") if yolo_flag: - # 直接调用路径生成函数,无需传入原始图片名 - save_path = get_image_save_path(model_type="yolo") - if save_path: - cv2.imwrite(save_path, frame) - print(f"✅ YOLO违规图片已保存:{save_path}") + # 元组解构:获取「完整保存路径」和「显示用短路径」 + full_save_path, display_path = get_image_save_path(model_type="yolo") + if full_save_path: # 只判断完整路径是否有效(用于保存) + cv2.imwrite(full_save_path, frame) + # 打印时使用「显示用短路径」,符合需求格式 + print(f"✅ YOLO违规图片已保存:{display_path}") return (True, yolo_result, "yolo") # 2. 人脸检测(优先级2) face_flag, face_result = faceDetect(frame) print(f"人脸检测结果:{face_result}") if face_flag: - save_path = get_image_save_path(model_type="face") - if save_path: - cv2.imwrite(save_path, frame) - print(f"✅ 人脸违规图片已保存:{save_path}") + # 同样解构元组,分离保存路径和显示路径 + full_save_path, display_path = get_image_save_path(model_type="face") + if full_save_path: + cv2.imwrite(full_save_path, frame) + print(f"✅ 人脸违规图片已保存:{display_path}") return (True, face_result, "face") # 3. OCR检测(优先级3) ocr_flag, ocr_result = ocrDetect(frame) print(f"OCR检测结果:{ocr_result}") if ocr_flag: - save_path = get_image_save_path(model_type="ocr") - if save_path: - cv2.imwrite(save_path, frame) - print(f"✅ OCR违规图片已保存:{save_path}") + # 解构元组,保存用完整路径,打印用短路径 + full_save_path, display_path = get_image_save_path(model_type="ocr") + if full_save_path: + cv2.imwrite(full_save_path, frame) + print(f"✅ OCR违规图片已保存:{display_path}") return (True, ocr_result, "ocr") # 4. 无违规内容(不保存图片) diff --git a/core/establish.py b/core/establish.py index 4b8027c..f679634 100644 --- a/core/establish.py +++ b/core/establish.py @@ -45,8 +45,8 @@ def create_directory_structure(): # 6. 为每个IP在每个模型文件夹下创建年->月的目录结构 for ip in ip_addresses: - # 处理IP地址中的特殊字符(如果有) - safe_ip = ip.replace(".", "_") + # 直接使用原始IP格式 + safe_ip = ip for model in model_dirs: # 构建路径: resource/dect/{model}/{ip}/{year}/{month} @@ -67,15 +67,15 @@ def create_directory_structure(): 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" 返回: - 完整的图片保存路径 + 元组 (完整保存路径, 显示用路径) """ try: # 读取IP地址(假设只有一个IP或使用第一个IP) @@ -86,7 +86,7 @@ def get_image_save_path(model_type: str) -> str: raise ValueError("ip.txt文件中未找到有效的IP地址") ip = ip_addresses[0] - safe_ip = ip.replace(".", "_") + safe_ip = ip # 直接使用原始IP格式 # 获取当前日期和时间(精确到毫秒,确保文件名唯一) 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] # 去除最后三位,保留到毫秒 - # 构建路径: 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) - # 构建图片文件名(使用时间戳确保唯一性) - image_filename = f"resource_dect_{model_type}_{safe_ip}_{current_year}_{current_month}_{current_day}_{timestamp}.jpg" - image_path = day_dir / image_filename + # 构建图片文件名(简化名称,去掉resource_dect_前缀) + image_filename = f"{model_type}_{safe_ip}_{current_year}_{current_month}_{current_day}_{timestamp}.jpg" + 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: print(f"获取图片保存路径时发生错误: {str(e)}") - return "" + return "", ""