Spaces:
Runtime error
Runtime error
Commit
·
7e8a433
1
Parent(s):
5f6ce43
Fix broken gifs with default
Browse files
app.py
CHANGED
|
@@ -10,33 +10,54 @@ global stored_frames
|
|
| 10 |
def load_and_store_frames(image_file, grid_x, grid_y):
|
| 11 |
global stored_frames
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def generate_grid(grid_x, grid_y, font_size, font_color, position, border_size, border_color):
|
| 42 |
global stored_frames
|
|
@@ -115,21 +136,33 @@ def extract_frames_from_video(video_file):
|
|
| 115 |
|
| 116 |
|
| 117 |
def convert_gif_to_video(gif_path, output_video_path, frame_rate):
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
out.release()
|
| 135 |
|
|
|
|
| 10 |
def load_and_store_frames(image_file, grid_x, grid_y):
|
| 11 |
global stored_frames
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
# Make sure file exists
|
| 15 |
+
if image_file is None:
|
| 16 |
+
return "File not found", ""
|
| 17 |
+
|
| 18 |
+
print(f"Loading frames for {image_file.name}")
|
| 19 |
+
|
| 20 |
+
if image_file.name.endswith('.mp4'):
|
| 21 |
+
frames = extract_frames_from_video(image_file.name)
|
| 22 |
+
video_path = image_file.name
|
| 23 |
+
else: # it's a gif
|
| 24 |
+
try:
|
| 25 |
+
img = Image.open(image_file.name)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Could not open GIF file: {e}")
|
| 28 |
+
return "Could not open GIF file", ""
|
| 29 |
+
|
| 30 |
+
frames = []
|
| 31 |
+
for i in range(0, img.n_frames):
|
| 32 |
+
try:
|
| 33 |
+
img.seek(i)
|
| 34 |
+
frames.append(img.copy())
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Could not seek to frame {i}: {e}")
|
| 37 |
+
|
| 38 |
+
# Convert GIF to MP4 for preview
|
| 39 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 40 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 41 |
+
video_path = tmp_file.name
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
duration = img.info.get('duration', 100)
|
| 45 |
+
# default to reasonable framerate if duration is 0
|
| 46 |
+
framerate = 1 / (duration / 1000.0) if duration > 0 else 10
|
| 47 |
+
print(f"frame count: {len(frames)} framerate: {duration} {img.info}")
|
| 48 |
+
convert_gif_to_video(image_file.name, tmp_file.name, framerate)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Could not convert GIF to MP4: {e}")
|
| 51 |
+
|
| 52 |
+
stored_frames = frames # Store the frames for later use
|
| 53 |
+
total_frames = len(frames)
|
| 54 |
+
selected_frames_count = grid_x * grid_y
|
| 55 |
+
details = f"**Total Frames:** {len(frames)}\n\n"
|
| 56 |
+
output_info = f"Grid size: {grid_x} x {grid_y}\n\nSelected Frames: {selected_frames_count} / {total_frames} ({selected_frames_count / total_frames * 100:.2f}%)"
|
| 57 |
+
return f"Frames loaded successfully\n\n{details}\n\n{output_info}", video_path
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"An error occurred while loading and storing frames: {e}")
|
| 60 |
+
return f"An error occurred: {e}", ""
|
| 61 |
|
| 62 |
def generate_grid(grid_x, grid_y, font_size, font_color, position, border_size, border_color):
|
| 63 |
global stored_frames
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
def convert_gif_to_video(gif_path, output_video_path, frame_rate):
|
| 139 |
+
try:
|
| 140 |
+
# Load the gif
|
| 141 |
+
gif = Image.open(gif_path)
|
| 142 |
+
except Exception as e:
|
| 143 |
+
print(f"Could not open GIF file: {e}")
|
| 144 |
+
return
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
# Define the codec and create VideoWriter object
|
| 148 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 149 |
+
out = cv2.VideoWriter(output_video_path, fourcc, frame_rate, (gif.width, gif.height))
|
| 150 |
+
except Exception as e:
|
| 151 |
+
print(f"Could not create VideoWriter object: {e}")
|
| 152 |
+
return
|
| 153 |
+
|
| 154 |
+
try:
|
| 155 |
+
# Iterate over the frames of the gif
|
| 156 |
+
for frame_index in range(gif.n_frames):
|
| 157 |
+
gif.seek(frame_index)
|
| 158 |
+
# Convert the PIL Image to an array
|
| 159 |
+
frame_arr = np.array(gif.convert("RGB"))
|
| 160 |
+
# Convert RGB to BGR format
|
| 161 |
+
frame_bgr = cv2.cvtColor(frame_arr, cv2.COLOR_RGB2BGR)
|
| 162 |
+
# Write the frame to the video
|
| 163 |
+
out.write(frame_bgr)
|
| 164 |
+
except Exception as e:
|
| 165 |
+
print(f"Could not write frame to video: {e}")
|
| 166 |
|
| 167 |
out.release()
|
| 168 |
|