File size: 2,540 Bytes
f7598da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
try:
    import cv2
    import numpy as np
    from insightface.app import FaceAnalysis
    INSIGHTFACE_AVAILABLE = True
except ImportError:
    INSIGHTFACE_AVAILABLE = False
    print("Warning: InsightFace not available. Dynamic cuts may fail if coords missing.")

def detect_faces_jit(video_path):
    """

    Runs face detection on the fly if pre-computed coords are missing.

    Returns: list of {'frame': int, 'faces': [[x1,y1,x2,y2]]}

    """
    if not INSIGHTFACE_AVAILABLE: 
        print("ERROR: InsightFace not loaded.")
        return []
    
    # Normalize path for Windows OpenCV
    video_path = os.path.abspath(video_path)
    print(f"Running JIT Face Detection on: {video_path}")
    
    # Initialize InsightFace
    try:
        app = FaceAnalysis(name='buffalo_l', providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
        app.prepare(ctx_id=0, det_size=(640, 640))
    except Exception as e:
        print(f"InsightFace Init Error: {e}. Trying CPU only.")
        app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
        app.prepare(ctx_id=0, det_size=(640, 640))
        
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print(f"CRITICAL ERROR: Could not open video file for JIT detection: {video_path}")
        # Try handling unicode path issues if any, though abspath helps
        return []

    face_data = []
    frame_idx = 0
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print(f"Video opened. Total frames: {total_frames}")
    
    faces_found_count = 0
    
    while True:
        ret, frame = cap.read()
        if not ret: break
        
        faces = app.get(frame)
        current_faces = []
        for face in faces:
            bbox = face.bbox.astype(int).tolist()
            current_faces.append(bbox)
        
        if current_faces:
            face_data.append({
                "frame": frame_idx,
                "faces": current_faces
            })
            faces_found_count += 1
            if faces_found_count <= 5: # Debug first few detections
                print(f"  [DEBUG] Frame {frame_idx}: Found {len(faces)} faces: {current_faces}")
            
        if frame_idx % 200 == 0:
            print(f"  Scanning faces: {frame_idx}/{total_frames}...")
            
        frame_idx += 1
        
    cap.release()
    print(f"JIT Detection Complete. Found faces in {len(face_data)} frames.")
    return face_data