spideyhead commited on
Commit
35fcc34
·
verified ·
1 Parent(s): 1ab031c

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -20
main.py CHANGED
@@ -61,13 +61,13 @@ def extract_clip(video_path, start_frame):
61
  """
62
  Open a fresh cap, seek ONCE to start_frame, then read frames
63
  SEQUENTIALLY (no cap.set inside loop). This is reliable for all codecs.
64
- Collects SEQUENCE_LENGTH face crops using bounding box caching for speed.
65
  """
66
  cap = cv2.VideoCapture(video_path)
67
  cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame) # Seek exactly ONCE
68
 
69
  faces = []
70
- base_box = None
71
  attempts = 0
72
 
73
  while len(faces) < SEQUENCE_LENGTH and attempts < 60:
@@ -79,25 +79,33 @@ def extract_clip(video_path, start_frame):
79
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
80
  pil_img = Image.fromarray(frame_rgb)
81
 
82
- if base_box is None:
83
- # Downscale for faster MTCNN detection
84
- detect_img = pil_img.copy()
85
- if detect_img.width > 640:
86
- ratio = 640.0 / detect_img.width
87
- detect_img = detect_img.resize((640, int(detect_img.height * ratio)))
88
- boxes, _ = mtcnn.detect(detect_img)
89
- if boxes is not None and len(boxes) > 0:
90
- base_box = [b / ratio for b in boxes[0].tolist()]
91
- else:
92
- boxes, _ = mtcnn.detect(pil_img)
93
- if boxes is not None and len(boxes) > 0:
94
- base_box = boxes[0].tolist()
95
 
96
- if base_box is not None:
97
- # We use a larger padding (0.5) because we are caching the box and the face might move slightly
98
- crop = crop_face(pil_img, base_box, padding=0.5)
99
- if crop is not None:
100
- faces.append(crop)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  cap.release()
103
 
 
61
  """
62
  Open a fresh cap, seek ONCE to start_frame, then read frames
63
  SEQUENTIALLY (no cap.set inside loop). This is reliable for all codecs.
64
+ Collects SEQUENCE_LENGTH face crops using downscaled frame-by-frame tracking.
65
  """
66
  cap = cv2.VideoCapture(video_path)
67
  cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame) # Seek exactly ONCE
68
 
69
  faces = []
70
+ last_box = None
71
  attempts = 0
72
 
73
  while len(faces) < SEQUENCE_LENGTH and attempts < 60:
 
79
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
80
  pil_img = Image.fromarray(frame_rgb)
81
 
82
+ # Downscale for faster MTCNN detection on every frame
83
+ detect_img = pil_img.copy()
84
+ current_box = None
 
 
 
 
 
 
 
 
 
 
85
 
86
+ if detect_img.width > 640:
87
+ ratio = 640.0 / detect_img.width
88
+ detect_img = detect_img.resize((640, int(detect_img.height * ratio)))
89
+ boxes, _ = mtcnn.detect(detect_img)
90
+ if boxes is not None and len(boxes) > 0:
91
+ current_box = [b / ratio for b in boxes[0].tolist()]
92
+ else:
93
+ boxes, _ = mtcnn.detect(pil_img)
94
+ if boxes is not None and len(boxes) > 0:
95
+ current_box = boxes[0].tolist()
96
+
97
+ if current_box is not None:
98
+ smoothed = smooth_box(current_box, last_box)
99
+ last_box = smoothed
100
+ elif last_box is not None:
101
+ smoothed = last_box # Hold last known position
102
+ else:
103
+ continue # No face yet — keep reading
104
+
105
+ # Use standard padding for centered faces
106
+ crop = crop_face(pil_img, smoothed, padding=0.35)
107
+ if crop is not None:
108
+ faces.append(crop)
109
 
110
  cap.release()
111