Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -173,6 +173,47 @@ def process_video(video_path, pose_detector, skeleton_generator):
|
|
| 173 |
|
| 174 |
def process_gif(gif_path, pose_detector, skeleton_generator):
|
| 175 |
"""
|
| 176 |
-
Process GIF for pose detection and skeleton generation
|
|
|
|
|
|
|
| 177 |
"""
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
def process_gif(gif_path, pose_detector, skeleton_generator):
|
| 175 |
"""
|
| 176 |
+
Process GIF for pose detection and skeleton generation.
|
| 177 |
+
This implementation uses Pillow to extract frames from the GIF, processes each frame,
|
| 178 |
+
and creates a temporary MP4 video with the processed frames.
|
| 179 |
"""
|
| 180 |
+
from PIL import Image, ImageSequence
|
| 181 |
+
import cv2
|
| 182 |
+
import numpy as np
|
| 183 |
+
import tempfile
|
| 184 |
+
|
| 185 |
+
# Открываем GIF с помощью Pillow
|
| 186 |
+
gif = Image.open(gif_path)
|
| 187 |
+
frames = []
|
| 188 |
+
for frame in ImageSequence.Iterator(gif):
|
| 189 |
+
frame = frame.convert("RGB")
|
| 190 |
+
frame_np = np.array(frame)
|
| 191 |
+
# Переводим RGB в BGR (OpenCV использует BGR)
|
| 192 |
+
frame_cv = cv2.cvtColor(frame_np, cv2.COLOR_RGB2BGR)
|
| 193 |
+
frames.append(frame_cv)
|
| 194 |
+
|
| 195 |
+
processed_frames = []
|
| 196 |
+
animation_frames = []
|
| 197 |
+
# Обрабатываем каждый кадр
|
| 198 |
+
for frame in frames:
|
| 199 |
+
landmarks, annotated_frame = pose_detector.detect_video_frame(frame)
|
| 200 |
+
if annotated_frame is None:
|
| 201 |
+
annotated_frame = frame
|
| 202 |
+
processed_frames.append(annotated_frame)
|
| 203 |
+
if landmarks is not None:
|
| 204 |
+
skeleton_data = skeleton_generator.generate_skeleton(landmarks)
|
| 205 |
+
else:
|
| 206 |
+
skeleton_data = None
|
| 207 |
+
animation_frames.append(skeleton_data)
|
| 208 |
+
|
| 209 |
+
# Сохраняем обработанные кадры в временное видео (MP4)
|
| 210 |
+
height, width = processed_frames[0].shape[:2]
|
| 211 |
+
fps = 10 # Можно подобрать подходящее значение для GIF
|
| 212 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 213 |
+
temp_video = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
|
| 214 |
+
out = cv2.VideoWriter(temp_video.name, fourcc, fps, (width, height))
|
| 215 |
+
for frame in processed_frames:
|
| 216 |
+
out.write(frame)
|
| 217 |
+
out.release()
|
| 218 |
+
|
| 219 |
+
return temp_video.name, animation_frames
|