fffiloni commited on
Commit
5159a77
·
1 Parent(s): 6332f55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -7
app.py CHANGED
@@ -26,7 +26,7 @@ mediapy.set_ffmpeg(ffmpeg_path)
26
 
27
 
28
 
29
- def interpolate(frame1, frame2):
30
 
31
  input_frames = [frame1, frame2]
32
  times_to_interpolate = 2
@@ -35,13 +35,134 @@ def interpolate(frame1, frame2):
35
  input_frames, times_to_interpolate, interpolator))
36
  print(frames)
37
 
38
- return "done"
39
 
 
 
 
 
40
 
 
 
 
 
 
 
 
 
 
41
 
42
- title="sketch-frame-interpolation"
43
- description="<p style='text-align: center'>This is a fork of the Gradio demo for FILM: Frame Interpolation for Large Scene Motion from @akhaliq, but using sketches instead of images. This could be very useful for the animation industry :) <br /> To use it, simply draw your sketches and add the times to interpolate number. Read more at the links below. <br /> <img id='visitor-badge' alt='visitor badge' src='https://visitor-badge.glitch.me/badge?page_id=gradio-blocks.sketch_frame_interpolation' style='display: inline-block'/></p>"
44
- article = "<p style='text-align: center'><a href='https://film-net.github.io/' target='_blank'>FILM: Frame Interpolation for Large Motion</a> | <a href='https://github.com/google-research/frame-interpolation' target='_blank'>Github Repo</a></p>"
45
- custom_css = "style.css"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- gr.Interface(interpolate,[gr.Image(source="upload", type="filepath"),gr.Image(source="upload", type="filepath")],outputs=[gr.Textbox()],title=title,description=description,article=article,css=custom_css).launch(enable_queue=True)
 
26
 
27
 
28
 
29
+ def do_interpolation(frame1, frame2):
30
 
31
  input_frames = [frame1, frame2]
32
  times_to_interpolate = 2
 
35
  input_frames, times_to_interpolate, interpolator))
36
  print(frames)
37
 
38
+ return frames
39
 
40
+ def get_frames(video_in):
41
+ frames = []
42
+ #resize the video
43
+ clip = VideoFileClip(video_in)
44
 
45
+ #check fps
46
+ if clip.fps > 30:
47
+ print("vide rate is over 30, resetting to 30")
48
+ clip_resized = clip.resize(height=512)
49
+ clip_resized.write_videofile("video_resized.mp4", fps=30)
50
+ else:
51
+ print("video rate is OK")
52
+ clip_resized = clip.resize(height=512)
53
+ clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
54
 
55
+ print("video resized to 512 height")
56
+
57
+ # Opens the Video file with CV2
58
+ cap= cv2.VideoCapture("video_resized.mp4")
59
+
60
+ fps = cap.get(cv2.CAP_PROP_FPS)
61
+ print("video fps: " + str(fps))
62
+ i=0
63
+ while(cap.isOpened()):
64
+ ret, frame = cap.read()
65
+ if ret == False:
66
+ break
67
+ cv2.imwrite('kang'+str(i)+'.jpg',frame)
68
+ frames.append('kang'+str(i)+'.jpg')
69
+ i+=1
70
+
71
+ cap.release()
72
+ cv2.destroyAllWindows()
73
+ print("broke the video into frames")
74
+
75
+ return frames, fps
76
+
77
+
78
+ def create_video(frames, fps, type):
79
+ print("building video result")
80
+ clip = ImageSequenceClip(frames, fps=fps)
81
+ clip.write_videofile(type + "_result.mp4", fps=fps)
82
+
83
+ return type + "_result.mp4"
84
+
85
+ def convertG2V(imported_gif):
86
+ clip = VideoFileClip(imported_gif.name)
87
+ clip.write_videofile("my_gif_video.mp4")
88
+ return "my_gif_video.mp4"
89
+
90
+ def infer(video_in):
91
+
92
+
93
+ # 1. break video into frames and get FPS
94
+ break_vid = get_frames(video_in)
95
+ frames_list= break_vid[0]
96
+ fps = break_vid[1]
97
+ #n_frame = int(trim_value*fps)
98
+ n_frame = len(frames_list)
99
+
100
+ if n_frame >= len(frames_list):
101
+ print("video is shorter than the cut value")
102
+ n_frame = len(frames_list)
103
+
104
+ # 2. prepare frames result arrays
105
+ result_frames = []
106
+ print("set stop frames to: " + str(n_frame))
107
+
108
+
109
+
110
+
111
+ for idx, frame in enumerate(frames_list):
112
+ if idx < len(frames_list) - 1:
113
+ next_frame = frames_list[idx+1]
114
+ interpolated_frames = do_interpolation(frame, next_frame) # should return a list of 3 interpolated frames
115
+ result_frames.extend(interpolated_frames)
116
+ print("frames " + idx + " & " + idx + 1 + "/" + str(n_frame) + ": done;")
117
+
118
+ final_vid = create_video(result_frames, fps, "interpolated")
119
+
120
+ files = [final_vid]
121
+
122
+ return final_vid, files
123
+
124
+ title="""
125
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;">
126
+ <div
127
+ style="
128
+ display: inline-flex;
129
+ align-items: center;
130
+ gap: 0.8rem;
131
+ font-size: 1.75rem;
132
+ margin-bottom: 10px;
133
+ "
134
+ >
135
+ <h1 style="font-weight: 600; margin-bottom: 7px;">
136
+ Video interpolation with FILM
137
+ </h1>
138
+
139
+ </div>
140
+ <p> - </p>
141
+ </div>
142
+ """
143
+
144
+ with gr.Blocks() as demo:
145
+ with gr.Column():
146
+ gr.HTML(title)
147
+ with gr.Row():
148
+ with gr.Column():
149
+ video_input = gr.Video(source="upload", type="filepath")
150
+ gif_input = gr.File(label="import a GIF instead", file_types=['.gif'])
151
+ gif_input.change(fn=convertG2V, inputs=gif_input, outputs=video_input)
152
+ submit_btn = gr.Button("Submit")
153
+
154
+ with gr.Column():
155
+ video_output = gr.Video()
156
+ file_output = gr.Files()
157
+
158
+ gr.Examples(
159
+ #examples=["./examples/childishgambino.mp4", "./examples/jimmyfallon.mp4"],
160
+ fn=infer,
161
+ inputs=[video_input],
162
+ outputs=[video_output,file_output],
163
+ #cache_examples=False
164
+ )
165
+
166
+ submit_btn.click(fn=infer, inputs=[video_input], outputs=[video_output, file_output])
167
 
168
+ demo.launch()