Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ mediapy.set_ffmpeg(ffmpeg_path)
|
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
-
def
|
| 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
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
|
|
|
| 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()
|