""" 项目设置测试脚本 检查环境配置和代码是否正常工作 """ import sys from pathlib import Path def test_imports(): """测试必要的库是否可以导入""" print("=" * 60) print("测试Python库导入...") print("=" * 60) required_libs = [ ('torch', 'PyTorch'), ('torchvision', 'TorchVision'), ('PIL', 'Pillow'), ('numpy', 'NumPy'), ('cv2', 'OpenCV'), ('gradio', 'Gradio'), ('sklearn', 'Scikit-learn'), ] failed = [] for lib_name, lib_display in required_libs: try: __import__(lib_name) print(f"✅ {lib_display}") except ImportError: print(f"❌ {lib_display} - 未安装") failed.append(lib_display) if failed: print(f"\n⚠️ 以下库未安装: {', '.join(failed)}") print("请运行: pip install -r requirements.txt") return False else: print("\n✅ 所有必要的库都已安装") return True def test_project_structure(): """测试项目结构""" print("\n" + "=" * 60) print("测试项目结构...") print("=" * 60) required_files = [ 'README.md', 'requirements.txt', 'app.py', 'models/model.py', 'models/train.py', 'utils/detector.py', 'utils/traffic_light.py', 'scripts/preprocess.py', ] required_dirs = [ 'data/train', 'data/test', 'models/checkpoints', ] base_path = Path(__file__).parent all_ok = True print("\n检查文件...") for file_path in required_files: full_path = base_path / file_path if full_path.exists(): print(f"✅ {file_path}") else: print(f"❌ {file_path} - 文件不存在") all_ok = False print("\n检查目录...") for dir_path in required_dirs: full_path = base_path / dir_path if full_path.exists(): print(f"✅ {dir_path}") else: print(f"⚠️ {dir_path} - 目录不存在(将自动创建)") full_path.mkdir(parents=True, exist_ok=True) return all_ok def test_model_code(): """测试模型代码是否可以正常加载""" print("\n" + "=" * 60) print("测试模型代码...") print("=" * 60) try: sys.path.append(str(Path(__file__).parent / 'models')) from model import PedestrianClassifier, get_transforms print("✅ 模型定义代码正常") # 测试创建模型 model = PedestrianClassifier(num_classes=4, model_name='resnet18', pretrained=False) print("✅ 模型创建成功") # 测试数据变换 transforms = get_transforms(is_train=True) print("✅ 数据变换定义正常") return True except Exception as e: print(f"❌ 模型代码测试失败: {e}") return False def test_utils_code(): """测试工具代码""" print("\n" + "=" * 60) print("测试工具代码...") print("=" * 60) try: sys.path.append(str(Path(__file__).parent / 'utils')) from detector import PedestrianDetector from traffic_light import TrafficLightController print("✅ 检测器代码正常") print("✅ 交通灯控制器代码正常") # 测试交通灯控制器 controller = TrafficLightController() controller.start_green_light(30) status = controller.update() print(f"✅ 交通灯控制器功能正常 (状态: {status['state']})") return True except Exception as e: print(f"❌ 工具代码测试失败: {e}") return False def test_data_structure(): """检查数据集结构""" print("\n" + "=" * 60) print("检查数据集结构...") print("=" * 60) base_path = Path(__file__).parent categories = ['normal', 'elderly', 'wheelchair', 'stroller'] has_data = False for split in ['train', 'test']: print(f"\n{split} 数据集:") for category in categories: category_dir = base_path / 'data' / split / category if category_dir.exists(): image_files = list(category_dir.glob("*.jpg")) + \ list(category_dir.glob("*.png")) + \ list(category_dir.glob("*.jpeg")) count = len(image_files) if count > 0: print(f" ✅ {category}: {count} 张图片") has_data = True else: print(f" ⚠️ {category}: 0 张图片") else: print(f" ⚠️ {category}: 目录不存在") if not has_data: print("\n⚠️ 未找到训练数据!") print("请运行以下命令查看数据集收集指南:") print(" python scripts/search_datasets.py") return has_data def main(): """主测试函数""" print("\n" + "=" * 60) print("智能人行横道系统 - 项目设置测试") print("=" * 60) results = [] # 测试导入 results.append(("库导入", test_imports())) # 测试项目结构 results.append(("项目结构", test_project_structure())) # 测试模型代码 results.append(("模型代码", test_model_code())) # 测试工具代码 results.append(("工具代码", test_utils_code())) # 检查数据集 has_data = test_data_structure() results.append(("数据集", has_data)) # 总结 print("\n" + "=" * 60) print("测试总结") print("=" * 60) for name, result in results: status = "✅ 通过" if result else "❌ 失败" print(f"{name}: {status}") all_passed = all(result for _, result in results[:-1]) # 数据集不是必须的 if all_passed: print("\n✅ 项目设置正常!可以开始使用。") if not has_data: print("\n📋 下一步:收集数据集并开始训练模型") print(" 运行: python scripts/search_datasets.py") else: print("\n⚠️ 部分测试未通过,请检查上述错误信息") print("=" * 60) if __name__ == "__main__": main()