diff --git a/docs_v1.0/IMPLEMENTATION/PLACES365_INSTALLATION.md b/docs_v1.0/IMPLEMENTATION/PLACES365_INSTALLATION.md new file mode 100644 index 0000000..164d68f --- /dev/null +++ b/docs_v1.0/IMPLEMENTATION/PLACES365_INSTALLATION.md @@ -0,0 +1,77 @@ +# Places365 模型安裝指南 + +## 概述 + +Places365 是一個專門用於場景識別的深度學習模型,包含 365 種場景類別。 + +## 目前狀態 + +### 已安裝 ✅ +- ResNet18 ImageNet 預訓練模型 (`models/resnet18_imagenet.pth`, 44.7MB) +- Places365 類別映射 (`scripts/places365_categories.json`, 380 類) +- ImageNet 到場景映射 (`models/imagenet_to_scene.json`) + +### 功能正常 ✅ +- 基礎場景識別功能 +- 380 個 Places365 類別支援 +- PyTorch MPS 加速(M4 Mac Mini 優化) + +### 效能指標 +| 指標 | 目前 | 預期 (Places365) | +|------|------|-----------------| +| 準確率 | 37% | 85-90% | +| 場景名稱 | scene_XXX | 實際名稱 | +| 處理速度 | ~60 FPS | ~60 FPS | + +## 使用現有模型 + +即使沒有專門的 Places365 模型,系統仍可運作: + +```bash +# 基本使用 +python3 scripts/scene_classifier.py video.mp4 output.json + +# 測試功能 +python3 scripts/test_places365_scene.py + +# 測試影片 +python3 scripts/test_places365_scene.py /path/to/video.mp4 +``` + +## 手動安裝 Places365 模型(可選) + +如需提升準確率,可手動下載專門的 Places365 模型: + +### 步驟 1: 下載模型 + +```bash +cd /Users/accusys/momentry/models + +# 從 GitHub 下載 +curl -L -o resnet18_places365.pth.tar \ + "https://github.com/CSAILVision/places365/raw/master/resnet18_places365.pth.tar" +``` + +### 步驟 2: 驗證 + +```bash +ls -lh resnet18_places365.pth.tar +# 應該約 45MB +``` + +### 步驟 3: 測試 + +```bash +python3 scripts/test_places365_scene.py /path/to/video.mp4 +``` + +## 參考資料 + +- [Places365 官方網站](http://places2.csail.mit.edu/) +- [GitHub Repository](https://github.com/CSAILVision/Places365) + +## 故障排除 + +查看測試報告: +- `docs_v1.0/TESTING/SCENE_CLASSIFICATION_TEST_REPORT_2026_04_01.md` +- `docs_v1.0/IMPLEMENTATION/SCENE_CLASSIFICATION_MODULE.md` diff --git a/scripts/test_places365_scene.py b/scripts/test_places365_scene.py new file mode 100644 index 0000000..ee42f10 --- /dev/null +++ b/scripts/test_places365_scene.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +"""測試 Places365 場景識別功能""" + +import sys +import json +from pathlib import Path + +# 添加腳本目錄到路徑 +script_dir = Path(__file__).parent +sys.path.insert(0, str(script_dir)) + +from scene_classifier import SceneClassifier, PLACES365_CATEGORIES + + +def test_places365_categories(): + """測試 Places365 類別載入""" + print("=== 測試 Places365 類別 ===\n") + + if not PLACES365_CATEGORIES: + print("✗ Places365 類別未載入") + return False + + print(f"✓ 載入 {len(PLACES365_CATEGORIES)} 個場景類別") + + # 顯示前 10 個類別 + print("\n前 10 個場景類別:") + for i in range(min(10, len(PLACES365_CATEGORIES))): + key = str(i) + if key in PLACES365_CATEGORIES: + print(f" {i}. {PLACES365_CATEGORIES[key]}") + + return True + + +def test_scene_classifier(): + """測試場景分類器基本功能""" + print("\n=== 測試場景分類器 ===\n") + + classifier = SceneClassifier() + + if not classifier.load_model(): + print("✗ 模型載入失敗") + return False + + print("✓ 模型載入成功") + print( + f" 模型類型:{'PyTorch' if classifier.model else 'Core ML' if classifier.coreml_model else 'None'}" + ) + + return True + + +def test_video_classification(video_path: str): + """測試影片場景分類""" + print(f"\n=== 測試影片場景分類 ===\n") + print(f"影片:{video_path}") + + if not Path(video_path).exists(): + print(f"✗ 影片檔案不存在:{video_path}") + return False + + classifier = SceneClassifier() + if not classifier.load_model(): + print("✗ 模型載入失敗") + return False + + # 執行分類 + result = classifier.classify_video( + video_path=video_path, + output_path="/tmp/test_scene_output.json", + sample_interval=2.0, + min_scene_duration=3.0, + ) + + # 顯示結果 + print(f"\n✓ 分類完成") + print(f" 場景數量:{len(result['scenes'])}") + + if result["scenes"]: + scene = result["scenes"][0] + print(f"\n主要場景:") + print(f" 類型:{scene['scene_type']}") + print(f" 中文:{scene.get('scene_type_zh', 'N/A')}") + print(f" 持續時間:{scene['end_time'] - scene['start_time']:.1f} 秒") + print(f" 信心度:{scene['confidence'] * 100:.1f}%") + + if scene.get("top_5"): + print(f"\nTop 5 預測:") + for i, pred in enumerate(scene["top_5"][:3]): + print( + f" {i + 1}. {pred['scene_type']} ({pred['confidence'] * 100:.1f}%)" + ) + + return True + + +def main(): + """主測試函數""" + print("Places365 場景識別測試\n") + print("=" * 50) + + # 測試 1: Places365 類別 + if not test_places365_categories(): + print("\n⚠️ Places365 類別測試失敗,但可繼續使用") + + # 測試 2: 場景分類器 + if not test_scene_classifier(): + print("\n✗ 場景分類器測試失敗") + return 1 + + # 測試 3: 影片分類(如果有提供) + if len(sys.argv) > 1: + video_path = sys.argv[1] + if not test_video_classification(video_path): + print("\n⚠️ 影片分類測試失敗") + + print("\n" + "=" * 50) + print("✓ 所有測試完成!") + print("\n下一步:") + print( + "1. 使用場景識別:python3 scripts/scene_classifier.py " + ) + print("2. 查看安裝指南:cat docs/PLACES365_INSTALLATION.md") + print("3. 下載 Places365 模型以提升準確率") + + return 0 + + +if __name__ == "__main__": + sys.exit(main())