K00B404 commited on
Commit
c1bfbc1
·
verified ·
1 Parent(s): 22e2e72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -2
app.py CHANGED
@@ -1,4 +1,86 @@
1
  import gradio as gr
 
 
2
 
3
- i = gr.Interface.load("models/morph-labs/morph-prover-v0-7b")
4
- i.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import cv2
4
 
5
+ class ImageMorpher:
6
+ def __init__(self, num_frames=30, fps=10):
7
+ self.num_frames = num_frames
8
+ self.fps = fps
9
+
10
+ def morph_images(self, image_A, image_B):
11
+ """Morphs two images."""
12
+ image_A = np.array(image_A)
13
+ image_B = np.array(image_B)
14
+
15
+ if image_A.shape != image_B.shape:
16
+ raise ValueError("Images must have the same dimensions.")
17
+ if image_A.dtype != image_B.dtype:
18
+ raise ValueError("Images must have the same data type.")
19
+
20
+ height, width, _ = image_A.shape
21
+ morphed_images = []
22
+
23
+ for i in range(self.num_frames + 1):
24
+ alpha = i / self.num_frames
25
+ beta = 1 - alpha
26
+ morphed_image = cv2.addWeighted(image_A, alpha, image_B, beta, 0)
27
+ morphed_images.append(morphed_image)
28
+
29
+ return morphed_images
30
+
31
+ def create_mp4(self, images, output_path="output.mp4"):
32
+ """Creates an MP4 video from a list of images."""
33
+ height, width, _ = images[0].shape
34
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
35
+ video_writer = cv2.VideoWriter(output_path, fourcc, self.fps, (width, height))
36
+
37
+ for image in images:
38
+ video_writer.write(image)
39
+
40
+ video_writer.release()
41
+
42
+ def morph_image_chain(self, images):
43
+ """Morphs a chain of images and returns the video path."""
44
+ morphed_images = []
45
+ for i in range(len(images) - 1):
46
+ morphed_images.extend(self.morph_images(images[i], images[i + 1]))
47
+ self.create_mp4(morphed_images)
48
+ return "output.mp4"
49
+
50
+ # Create an instance of the ImageMorpher class
51
+ morpher = ImageMorpher()
52
+
53
+ # Define the Gradio interface
54
+ iface = gr.Interface(
55
+ fn=morpher.morph_image_chain,
56
+ inputs=gr.Image(label="Images", type="numpy", image_mode="RGB", tool="select", multiple=True),
57
+ outputs=gr.Video(label="Output Video"),
58
+ title="Image Chain Morphing App",
59
+ )
60
+
61
+ iface.launch()
62
+ '''
63
+ Explanation:
64
+
65
+ ImageMorpher class:
66
+
67
+ Encapsulates the morphing logic and video creation.
68
+ Has num_frames and fps attributes for customization.
69
+ morph_images method handles morphing two images.
70
+ create_mp4 method creates the MP4 video.
71
+ morph_image_chain method:
72
+ Iterates through the input image list and morphs each pair.
73
+ Creates the final MP4 video using create_mp4.
74
+ Returns the path to the video file.
75
+
76
+ Interface:
77
+ inputs: Changed to gr.Image with multiple=True for multi-image input.
78
+ outputs: Changed to gr.Video to display the video directly within the interface.
79
+
80
+ The fn is set to morpher.morph_image_chain to handle the morphing process.
81
+
82
+ How to use:
83
+ Select two or more images using the "select" tool in the "Images" input field.
84
+ Click "Submit".
85
+ The "Output Video" block will display the morphed video, which you can watch and download.
86
+ '''