roychao19477 commited on
Commit
6ca32b7
·
1 Parent(s): ef1b87f

First commit

Browse files
Files changed (1) hide show
  1. app.py +15 -4
app.py CHANGED
@@ -65,7 +65,20 @@ def extract_faces(video_file):
65
  # Inference
66
  results = model(frame, verbose=False)[0]
67
  for box in results.boxes:
68
- x1, y1, x2, y2 = map(int, box.xyxy[0])
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  face_crop = frame[y1:y2, x1:x2]
70
  if face_crop.size != 0:
71
  resized = cv2.resize(face_crop, (224, 224))
@@ -77,9 +90,7 @@ def extract_faces(video_file):
77
  # Save as video
78
  tmpdir = tempfile.mkdtemp()
79
  output_path = os.path.join(tmpdir, "face_only_video.mp4")
80
- #clip = ImageSequenceClip([cv2.cvtColor(f, cv2.COLOR_BGR2RGB) for f in frames], fps=fps)
81
- #clip.write_videofile(output_path, codec="libx264", audio=False, verbose=False, logger=None)
82
- clip = ImageSequenceClip(frames, fps=25)
83
  clip.write_videofile(output_path, codec="libx264", audio=False)
84
 
85
  return output_path
 
65
  # Inference
66
  results = model(frame, verbose=False)[0]
67
  for box in results.boxes:
68
+ # version 1
69
+ # x1, y1, x2, y2 = map(int, box.xyxy[0])
70
+ # version 2
71
+ h, w, _ = frame.shape
72
+ x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
73
+ pad_ratio = 0.3 # 30% padding
74
+
75
+ dx = (x2 - x1) * pad_ratio
76
+ dy = (y2 - y1) * pad_ratio
77
+
78
+ x1 = int(max(0, x1 - dx))
79
+ y1 = int(max(0, y1 - dy))
80
+ x2 = int(min(w, x2 + dx))
81
+ y2 = int(min(h, y2 + dy))
82
  face_crop = frame[y1:y2, x1:x2]
83
  if face_crop.size != 0:
84
  resized = cv2.resize(face_crop, (224, 224))
 
90
  # Save as video
91
  tmpdir = tempfile.mkdtemp()
92
  output_path = os.path.join(tmpdir, "face_only_video.mp4")
93
+ clip = ImageSequenceClip([cv2.cvtColor(f, cv2.COLOR_BGR2RGB) for f in frames], fps=25)
 
 
94
  clip.write_videofile(output_path, codec="libx264", audio=False)
95
 
96
  return output_path