Update app.py
Browse files
app.py
CHANGED
@@ -101,7 +101,7 @@ def extract_frames(video_path, output_folder, fps):
|
|
101 |
print(f"FFmpeg stderr: {e.stderr}")
|
102 |
raise
|
103 |
|
104 |
-
def extract_and_align_faces_from_video(video_path, aligned_faces_folder, desired_fps):
|
105 |
print(f"Processing video: {video_path}")
|
106 |
|
107 |
frames_folder = os.path.join(os.path.dirname(aligned_faces_folder), 'extracted_frames')
|
@@ -123,27 +123,21 @@ def extract_and_align_faces_from_video(video_path, aligned_faces_folder, desired
|
|
123 |
if len(ffprobe_output) != 2:
|
124 |
raise ValueError(f"Unexpected FFprobe output format: {ffprobe_output}")
|
125 |
|
126 |
-
frame_count = ffprobe_output
|
127 |
-
frame_rate = ffprobe_output[1]
|
128 |
|
129 |
print(f"Frame count (raw): {frame_count}")
|
130 |
print(f"Frame rate (raw): {frame_rate}")
|
131 |
|
132 |
-
try:
|
133 |
-
frame_count = int(frame_count)
|
134 |
-
except ValueError:
|
135 |
-
print(f"Warning: Could not convert frame count '{frame_count}' to int. Using fallback method.")
|
136 |
-
frame_count = len([f for f in os.listdir(frames_folder) if f.endswith('.jpg')])
|
137 |
-
|
138 |
try:
|
139 |
frac = fractions.Fraction(frame_rate)
|
140 |
original_fps = float(frac.numerator) / float(frac.denominator)
|
141 |
except (ValueError, ZeroDivisionError):
|
142 |
print(f"Warning: Could not convert frame rate '{frame_rate}' to float. Using fallback method.")
|
143 |
-
frame_count = len([f for f in os.listdir(frames_folder) if f.endswith('.jpg')])
|
144 |
duration_command = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', video_path]
|
145 |
duration = float(subprocess.check_output(duration_command, universal_newlines=True).strip())
|
146 |
-
original_fps = frame_count / duration
|
|
|
|
|
147 |
|
148 |
except subprocess.CalledProcessError as e:
|
149 |
print(f"Error running FFprobe: {e}")
|
@@ -157,12 +151,14 @@ def extract_and_align_faces_from_video(video_path, aligned_faces_folder, desired
|
|
157 |
embeddings_by_frame = {}
|
158 |
emotions_by_frame = {}
|
159 |
|
160 |
-
for frame_file in sorted(os.listdir(frames_folder)):
|
161 |
if frame_file.endswith('.jpg'):
|
162 |
frame_num = int(frame_file.split('_')[1].split('.')[0])
|
163 |
frame_path = os.path.join(frames_folder, frame_file)
|
164 |
frame = cv2.imread(frame_path)
|
165 |
|
|
|
|
|
166 |
if frame is None:
|
167 |
print(f"Skipping frame {frame_num}: Could not read frame")
|
168 |
continue
|
@@ -368,7 +364,7 @@ def process_video(video_path, num_anomalies, num_components, desired_fps, batch_
|
|
368 |
|
369 |
progress(0.1, "Extracting and aligning faces")
|
370 |
try:
|
371 |
-
embeddings_by_frame, emotions_by_frame, _, original_fps = extract_and_align_faces_from_video(video_path, aligned_faces_folder, desired_fps)
|
372 |
except Exception as e:
|
373 |
return f"Error extracting faces: {str(e)}", None, None, None, None, None, None
|
374 |
|
|
|
101 |
print(f"FFmpeg stderr: {e.stderr}")
|
102 |
raise
|
103 |
|
104 |
+
def extract_and_align_faces_from_video(video_path, aligned_faces_folder, desired_fps, progress=gr.Progress()):
|
105 |
print(f"Processing video: {video_path}")
|
106 |
|
107 |
frames_folder = os.path.join(os.path.dirname(aligned_faces_folder), 'extracted_frames')
|
|
|
123 |
if len(ffprobe_output) != 2:
|
124 |
raise ValueError(f"Unexpected FFprobe output format: {ffprobe_output}")
|
125 |
|
126 |
+
frame_rate, frame_count = ffprobe_output
|
|
|
127 |
|
128 |
print(f"Frame count (raw): {frame_count}")
|
129 |
print(f"Frame rate (raw): {frame_rate}")
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
try:
|
132 |
frac = fractions.Fraction(frame_rate)
|
133 |
original_fps = float(frac.numerator) / float(frac.denominator)
|
134 |
except (ValueError, ZeroDivisionError):
|
135 |
print(f"Warning: Could not convert frame rate '{frame_rate}' to float. Using fallback method.")
|
|
|
136 |
duration_command = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', video_path]
|
137 |
duration = float(subprocess.check_output(duration_command, universal_newlines=True).strip())
|
138 |
+
original_fps = int(frame_count) / duration
|
139 |
+
|
140 |
+
frame_count = int(frame_count)
|
141 |
|
142 |
except subprocess.CalledProcessError as e:
|
143 |
print(f"Error running FFprobe: {e}")
|
|
|
151 |
embeddings_by_frame = {}
|
152 |
emotions_by_frame = {}
|
153 |
|
154 |
+
for i, frame_file in enumerate(sorted(os.listdir(frames_folder))):
|
155 |
if frame_file.endswith('.jpg'):
|
156 |
frame_num = int(frame_file.split('_')[1].split('.')[0])
|
157 |
frame_path = os.path.join(frames_folder, frame_file)
|
158 |
frame = cv2.imread(frame_path)
|
159 |
|
160 |
+
progress((i + 1) / len(os.listdir(frames_folder)), f"Processing frame {i + 1} of {len(os.listdir(frames_folder))}")
|
161 |
+
|
162 |
if frame is None:
|
163 |
print(f"Skipping frame {frame_num}: Could not read frame")
|
164 |
continue
|
|
|
364 |
|
365 |
progress(0.1, "Extracting and aligning faces")
|
366 |
try:
|
367 |
+
embeddings_by_frame, emotions_by_frame, _, original_fps = extract_and_align_faces_from_video(video_path, aligned_faces_folder, desired_fps, progress)
|
368 |
except Exception as e:
|
369 |
return f"Error extracting faces: {str(e)}", None, None, None, None, None, None
|
370 |
|