import os import datetime from pathlib import Path # 配置IP文件路径(统一使用绝对路径) IP_FILE_PATH = Path(r"D:\ccc\IP.txt") def create_directory_structure(): """创建项目所需的目录结构""" try: # 1. 创建根目录下的resource文件夹 resource_dir = Path("resource") resource_dir.mkdir(exist_ok=True) print(f"确保resource目录存在: {resource_dir.absolute()}") # 2. 在resource下创建dect文件夹 dect_dir = resource_dir / "dect" dect_dir.mkdir(exist_ok=True) print(f"确保dect目录存在: {dect_dir.absolute()}") # 3. 在dect下创建三个模型文件夹 model_dirs = ["ocr", "face", "yolo"] for model in model_dirs: model_dir = dect_dir / model model_dir.mkdir(exist_ok=True) print(f"确保{model}模型目录存在: {model_dir.absolute()}") # 4. 读取ip.txt文件获取IP地址 try: with open(IP_FILE_PATH, "r") as f: ip_addresses = [line.strip() for line in f if line.strip()] if not ip_addresses: print("警告: ip.txt文件中未找到有效的IP地址") return print(f"从ip.txt中读取到的IP地址: {ip_addresses}") # 5. 获取当前日期 now = datetime.datetime.now() current_year = str(now.year) current_month = str(now.month) # 6. 为每个IP在每个模型文件夹下创建年->月的目录结构 for ip in ip_addresses: # 处理IP地址中的特殊字符(如果有) safe_ip = ip.replace(".", "_") for model in model_dirs: # 构建路径: resource/dect/{model}/{ip}/{year}/{month} ip_dir = dect_dir / model / safe_ip year_dir = ip_dir / current_year month_dir = year_dir / current_month # 创建目录(如果不存在) month_dir.mkdir(parents=True, exist_ok=True) print(f"创建/确保目录存在: {month_dir.absolute()}") except FileNotFoundError: print(f"错误: 未找到ip.txt文件,请确保该文件存在于 {IP_FILE_PATH}") except Exception as e: print(f"处理IP和日期目录时发生错误: {str(e)}") except Exception as e: print(f"创建目录结构时发生错误: {str(e)}") def get_image_save_path(model_type: str) -> str: """ 获取图片保存的完整路径(不依赖原始图片名称) 参数: model_type: 模型类型,应为"ocr"、"face"或"yolo" 返回: 完整的图片保存路径 """ try: # 读取IP地址(假设只有一个IP或使用第一个IP) with open(IP_FILE_PATH, "r") as f: ip_addresses = [line.strip() for line in f if line.strip()] if not ip_addresses: raise ValueError("ip.txt文件中未找到有效的IP地址") ip = ip_addresses[0] safe_ip = ip.replace(".", "_") # 获取当前日期和时间(精确到毫秒,确保文件名唯一) now = datetime.datetime.now() current_year = str(now.year) current_month = str(now.month) current_day = str(now.day) # 生成时间戳字符串(格式:年月日时分秒毫秒) 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 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 return str(image_path) except Exception as e: print(f"获取图片保存路径时发生错误: {str(e)}") return ""