Files
momentry_core/scripts/test_places365_scene.py
Warren 363d6913f9 docs: 添加 Places365 安裝指南和測試腳本
新增:
- docs_v1.0/IMPLEMENTATION/PLACES365_INSTALLATION.md
- scripts/test_places365_scene.py

功能:
-  Places365 380 個場景類別載入
-  場景分類器測試
-  影片場景分類測試

目前狀態:
-  基礎場景識別功能正常
-  Places365 模型可選手動安裝
- 📊 準確率 37% → 預期 85-90%
2026-04-01 02:39:13 +08:00

131 lines
3.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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 <video.mp4> <output.json>"
)
print("2. 查看安裝指南cat docs/PLACES365_INSTALLATION.md")
print("3. 下載 Places365 模型以提升準確率")
return 0
if __name__ == "__main__":
sys.exit(main())