Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system("git clone https://github.com/google-research/frame-interpolation")
|
3 |
+
import sys
|
4 |
+
sys.path.append("frame-interpolation")
|
5 |
+
import numpy as np
|
6 |
+
import tensorflow as tf
|
7 |
+
import mediapy
|
8 |
+
from PIL import Image
|
9 |
+
from eval import interpolator, util
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
from huggingface_hub import snapshot_download
|
13 |
+
|
14 |
+
from image_tools.sizes import resize_and_crop
|
15 |
+
|
16 |
+
|
17 |
+
model = snapshot_download(repo_id="akhaliq/frame-interpolation-film-style")
|
18 |
+
|
19 |
+
interpolator = interpolator.Interpolator(model, None)
|
20 |
+
|
21 |
+
ffmpeg_path = util.get_ffmpeg_path()
|
22 |
+
mediapy.set_ffmpeg(ffmpeg_path)
|
23 |
+
|
24 |
+
def resize(width,img):
|
25 |
+
basewidth = width
|
26 |
+
img = Image.open(img)
|
27 |
+
wpercent = (basewidth/float(img.size[0]))
|
28 |
+
hsize = int((float(img.size[1])*float(wpercent)))
|
29 |
+
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
|
30 |
+
return img
|
31 |
+
|
32 |
+
|
33 |
+
def resize_img(img1,img2):
|
34 |
+
img_target_size = Image.open(img1)
|
35 |
+
img_to_resize = resize_and_crop(
|
36 |
+
img2,
|
37 |
+
(img_target_size.size[0],img_target_size.size[1]), #set width and height to match img1
|
38 |
+
crop_origin="middle"
|
39 |
+
)
|
40 |
+
img_to_resize.save('resized_img2.png')
|
41 |
+
|
42 |
+
|
43 |
+
sketch1 = gr.Image(image_mode="L",
|
44 |
+
source="canvas",
|
45 |
+
type="filepath",
|
46 |
+
shape=(400, 400),
|
47 |
+
invert_colors=False)
|
48 |
+
|
49 |
+
sketch2 = gr.Image(image_mode="L",
|
50 |
+
source="canvas",
|
51 |
+
type="filepath",
|
52 |
+
shape=(400, 400),
|
53 |
+
invert_colors=False)
|
54 |
+
|
55 |
+
|
56 |
+
def predict(frame1, frame2, times_to_interpolate):
|
57 |
+
|
58 |
+
frame1 = resize(256,frame1)
|
59 |
+
frame2 = resize(256,frame2)
|
60 |
+
|
61 |
+
frame1.save("test1.png")
|
62 |
+
frame2.save("test2.png")
|
63 |
+
|
64 |
+
resize_img("test1.png","test2.png")
|
65 |
+
input_frames = ["test1.png", "resized_img2.png"]
|
66 |
+
|
67 |
+
frames = list(
|
68 |
+
util.interpolate_recursively_from_files(
|
69 |
+
input_frames, times_to_interpolate, interpolator))
|
70 |
+
|
71 |
+
mediapy.write_video("out.mp4", frames, fps=30)
|
72 |
+
return "out.mp4"
|
73 |
+
|
74 |
+
|
75 |
+
gr.Interface(predict,[sketch1,sketch2,gr.inputs.Slider(minimum=2,maximum=4,step=1)],"playable_video".launch(enable_queue=True)
|