Spaces:
Sleeping
Sleeping
import os | |
import tempfile | |
from fastapi import HTTPException | |
from scenedetect import detect, ContentDetector, SceneManager, open_video, scene_manager | |
class SceneDetector: | |
def __init__(self, min_scene_duration: float = 1.5, threshold: int = 30): | |
self.min_scene_duration = min_scene_duration | |
self.threshold = threshold | |
def detect_scenes(self, video_path: str) -> dict: | |
"""Pipeline complet de détection et de filtrage des scènes.""" | |
print(f"\n[DEBUG] Starting scene detection on video: {video_path}") | |
print(f"[DEBUG] Video file exists: {os.path.exists(video_path)}") | |
try: | |
# Ouvrir la vidéo | |
video = open_video(video_path) | |
# Configurer le détecteur de scènes | |
scene_manager = SceneManager() | |
scene_manager.add_detector( | |
ContentDetector(threshold=self.threshold) | |
) | |
# Détecter les scènes | |
scene_manager.detect_scenes(video) | |
scenes = scene_manager.get_scene_list() | |
# Formater les scènes | |
formatted_scenes = [] | |
for scene in scenes: | |
start_time = scene[0].get_timecode() | |
end_time = scene[1].get_timecode() | |
duration = scene[1].get_seconds() - scene[0].get_seconds() | |
if duration >= self.min_scene_duration: | |
formatted_scenes.append({ | |
"start": str(start_time), | |
"end": str(end_time), | |
"recognized_sport": "", # Sera rempli par le classificateur | |
"confidence": None # Sera rempli par le classificateur | |
}) | |
result = { | |
"total_scenes": len(formatted_scenes), | |
"scenes": formatted_scenes | |
} | |
# Debug logs | |
print("\n[DEBUG] Scene Detection Results:") | |
print(f"Total scenes detected: {result['total_scenes']}") | |
if result['scenes']: | |
print("\nFirst 3 scenes details:") | |
for i, scene in enumerate(result['scenes'][:3]): | |
print(f"\nScene {i+1}:") | |
print(f" Start: {scene['start']}") | |
print(f" End: {scene['end']}") | |
print(f" Sport: {scene['recognized_sport']}") | |
print(f" Confidence: {scene['confidence']}") | |
else: | |
print("No scenes detected!") | |
print("\n") | |
return result | |
except Exception as e: | |
print(f"[ERROR] Scene detection failed: {str(e)}") | |
raise HTTPException(status_code=500, detail=f"Scene detection failed: {str(e)}") |