seawolf2357 commited on
Commit
b40ffb2
·
verified ·
1 Parent(s): 079f6f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -24
app.py CHANGED
@@ -4,24 +4,16 @@ 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}.jpg")
13
- with open(file_path, 'wb') as file:
14
- file.write(file_info)
15
-
16
- # Get all the image files from temp directory
17
- images = [img for img in os.listdir(temp_dir) if img.lower().endswith(('.png', '.jpg', '.jpeg'))]
18
- if not images:
19
- raise ValueError("No images found to create a video.")
20
 
21
- images.sort() # Sort the images by name
22
-
23
- # Read the first image to set the video size
24
- frame = cv2.imread(os.path.join(temp_dir, images[0]))
25
  height, width, layers = frame.shape
26
  size = (width, height)
27
 
@@ -31,10 +23,8 @@ def images_to_video(input_files, fps=30):
31
  # Initialize the video writer
32
  out = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
33
 
34
- # Loop through all images and add to the video
35
- for image in images:
36
- frame = cv2.imread(os.path.join(temp_dir, image))
37
- out.write(frame)
38
 
39
  # Release the video writer
40
  out.release()
@@ -43,11 +33,11 @@ def images_to_video(input_files, fps=30):
43
  return video_filename
44
 
45
  iface = gr.Interface(
46
- fn=images_to_video,
47
- inputs=[gr.File(type="binary", label="Upload Images", multiple=True)],
48
  outputs=gr.Video(label="Output Video"),
49
- title="Images to Video Converter",
50
- description="Upload multiple images to create a video."
51
  )
52
 
53
  iface.launch()
 
4
  import tempfile
5
  from pathlib import Path
6
 
7
+ def image_to_video(input_file, fps=30):
8
+ # Create a temporary directory to store the image
9
  with tempfile.TemporaryDirectory() as temp_dir:
10
+ # Save the uploaded file to the temp directory
11
+ file_path = os.path.join(temp_dir, f"image{Path(input_file.name).suffix}")
12
+ with open(file_path, 'wb') as file:
13
+ file.write(input_file.read())
 
 
 
 
 
 
14
 
15
+ # Read the image to set the video size
16
+ frame = cv2.imread(file_path)
 
 
17
  height, width, layers = frame.shape
18
  size = (width, height)
19
 
 
23
  # Initialize the video writer
24
  out = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
25
 
26
+ # Add the image to the video
27
+ out.write(frame)
 
 
28
 
29
  # Release the video writer
30
  out.release()
 
33
  return video_filename
34
 
35
  iface = gr.Interface(
36
+ fn=image_to_video,
37
+ inputs=gr.File(type="binary", label="Upload Image"),
38
  outputs=gr.Video(label="Output Video"),
39
+ title="Image to Video Converter",
40
+ description="Upload an image to create a video."
41
  )
42
 
43
  iface.launch()