seawolf2357 commited on
Commit
c892817
1 Parent(s): 612cb39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -30
app.py CHANGED
@@ -1,32 +1,54 @@
1
  import cv2
 
2
  import os
3
-
4
- def images_to_video(image_folder, output_video_file, fps=30):
5
- # Get all the image files from the provided folder
6
- images = [img for img in os.listdir(image_folder) if img.endswith(".png") or img.endswith(".jpg") or img.endswith(".jpeg")]
7
-
8
- # Ensure the images are in the correct order if needed
9
- images.sort() # You might want to customize this logic
10
-
11
- # Get the dimensions of the first image
12
- frame = cv2.imread(os.path.join(image_folder, images[0]))
13
- height, width, layers = frame.shape
14
- size = (width, height)
15
-
16
- # Initialize the video writer
17
- out = cv2.VideoWriter(output_video_file, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
18
-
19
- # Loop through all images and add to the video
20
- for image in images:
21
- frame = cv2.imread(os.path.join(image_folder, image))
22
- out.write(frame)
23
-
24
- # Release the video writer
25
- out.release()
26
-
27
- # Define the image folder and the output video file
28
- image_folder = 'path_to_image_folder' # Replace with the path to your image folder
29
- output_video_file = 'output_video.mp4' # Replace with your desired output file name
30
-
31
- # Create the video
32
- images_to_video(image_folder, output_video_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
+ import gradio as gr
3
  import os
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+ def images_to_video(input_files, fps=30):
8
+ # Create a temporary directory to store images
9
+ with tempfile.TemporaryDirectory() as temp_dir:
10
+ # Save uploaded files to temp directory
11
+ for i, file_info in enumerate(input_files):
12
+ file_path = os.path.join(temp_dir, f"image_{i}{Path(file_info.name).suffix}")
13
+ with open(file_path, 'wb') as file:
14
+ file.write(file_info.read())
15
+ # Get all the image files from temp directory
16
+ images = [img for img in os.listdir(temp_dir) if img.lower().endswith(('.png', '.jpg', '.jpeg'))]
17
+ if not images:
18
+ raise ValueError("No images found to create a video.")
19
+
20
+ images.sort() # Sort the images by name
21
+
22
+ # Read the first image to set the video size
23
+ frame = cv2.imread(os.path.join(temp_dir, images[0]))
24
+ height, width, layers = frame.shape
25
+ size = (width, height)
26
+
27
+ # Create a unique filename for the output video
28
+ video_filename = os.path.join('/mnt/data', 'output_video.mp4')
29
+
30
+ # Initialize the video writer
31
+ out = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
32
+
33
+ # Loop through all images and add to the video
34
+ for image in images:
35
+ frame = cv2.imread(os.path.join(temp_dir, image))
36
+ out.write(frame)
37
+
38
+ # Release the video writer
39
+ out.release()
40
+
41
+ # Return the path of the created video file
42
+ return video_filename
43
+
44
+ # Create the Gradio interface
45
+ iface = gr.Interface(
46
+ fn=images_to_video,
47
+ inputs=[gr.inputs.Image(type="file", label="Upload Images", multiple=True)],
48
+ outputs=gr.outputs.Video(label="Output Video"),
49
+ title="Images to Video Converter",
50
+ description="Upload multiple images to create a video."
51
+ )
52
+
53
+ # Launch the interface
54
+ iface.launch()