Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -66,7 +66,7 @@ def process_video(video_path, pose_detector, skeleton_generator):
|
|
| 66 |
if not cap.isOpened():
|
| 67 |
return None, None
|
| 68 |
|
| 69 |
-
# Get video properties
|
| 70 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 71 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 72 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
|
@@ -148,7 +148,6 @@ def process_video(video_path, pose_detector, skeleton_generator):
|
|
| 148 |
if frame_count > 1000: # Safety limit for very long videos
|
| 149 |
break
|
| 150 |
|
| 151 |
-
|
| 152 |
# Release resources
|
| 153 |
if cap is not None:
|
| 154 |
cap.release()
|
|
@@ -177,43 +176,43 @@ def process_gif(gif_path, pose_detector, skeleton_generator):
|
|
| 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 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
| 66 |
if not cap.isOpened():
|
| 67 |
return None, None
|
| 68 |
|
| 69 |
+
# Get video properties again
|
| 70 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 71 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 72 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
|
|
|
| 148 |
if frame_count > 1000: # Safety limit for very long videos
|
| 149 |
break
|
| 150 |
|
|
|
|
| 151 |
# Release resources
|
| 152 |
if cap is not None:
|
| 153 |
cap.release()
|
|
|
|
| 176 |
This implementation uses Pillow to extract frames from the GIF, processes each frame,
|
| 177 |
and creates a temporary MP4 video with the processed frames.
|
| 178 |
"""
|
| 179 |
+
try:
|
| 180 |
+
from PIL import Image, ImageSequence
|
| 181 |
+
# Открываем GIF с помощью Pillow
|
| 182 |
+
gif = Image.open(gif_path)
|
| 183 |
+
frames = []
|
| 184 |
+
for frame in ImageSequence.Iterator(gif):
|
| 185 |
+
frame = frame.convert("RGB")
|
| 186 |
+
frame_np = np.array(frame)
|
| 187 |
+
# Переводим RGB в BGR (OpenCV использует BGR)
|
| 188 |
+
frame_cv = cv2.cvtColor(frame_np, cv2.COLOR_RGB2BGR)
|
| 189 |
+
frames.append(frame_cv)
|
| 190 |
+
|
| 191 |
+
processed_frames = []
|
| 192 |
+
animation_frames = []
|
| 193 |
+
# Обрабатываем каждый кадр
|
| 194 |
+
for frame in frames:
|
| 195 |
+
landmarks, annotated_frame = pose_detector.detect_video_frame(frame)
|
| 196 |
+
if annotated_frame is None:
|
| 197 |
+
annotated_frame = frame
|
| 198 |
+
processed_frames.append(annotated_frame)
|
| 199 |
+
if landmarks is not None:
|
| 200 |
+
skeleton_data = skeleton_generator.generate_skeleton(landmarks)
|
| 201 |
+
else:
|
| 202 |
+
skeleton_data = None
|
| 203 |
+
animation_frames.append(skeleton_data)
|
| 204 |
+
|
| 205 |
+
# Сохраняем обработанные кадры в временное видео (MP4)
|
| 206 |
+
height, width = processed_frames[0].shape[:2]
|
| 207 |
+
fps = 10 # Можно подобрать подходящее значение для GIF
|
| 208 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 209 |
+
temp_video = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
|
| 210 |
+
out = cv2.VideoWriter(temp_video.name, fourcc, fps, (width, height))
|
| 211 |
+
for frame in processed_frames:
|
| 212 |
+
out.write(frame)
|
| 213 |
+
out.release()
|
| 214 |
+
|
| 215 |
+
return temp_video.name, animation_frames
|
| 216 |
+
except Exception as e:
|
| 217 |
+
print(f"Error processing GIF: {str(e)}")
|
| 218 |
+
return None, None
|