NimaBoscarino commited on
Commit
09bc438
1 Parent(s): 3f908f4

Allow choosing choosing from multiple model options (#1)

Browse files

- Allow choosing choosing from multiple model options (eed8d18548c10f365984f9245969ef90ca5a18e7)

Files changed (1) hide show
  1. app.py +64 -30
app.py CHANGED
@@ -1,6 +1,8 @@
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
@@ -14,51 +16,83 @@ from huggingface_hub import snapshot_download
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
- def predict(frame1, frame2, times_to_interpolate):
43
-
44
- frame1 = resize(256,frame1)
45
- frame2 = resize(256,frame2)
 
 
 
46
 
47
  frame1.save("test1.png")
48
  frame2.save("test2.png")
49
 
50
- resize_img("test1.png","test2.png")
51
  input_frames = ["test1.png", "resized_img2.png"]
52
 
53
  frames = list(
54
  util.interpolate_recursively_from_files(
55
- input_frames, times_to_interpolate, interpolator))
56
 
57
  mediapy.write_video("out.mp4", frames, fps=30)
58
  return "out.mp4"
59
 
60
- title="frame-interpolation"
61
- description="Gradio demo for FILM: Frame Interpolation for Large Scene Motion. To use it, simply upload your images and add the times to interpolate number or click on one of the examples to load them. Read more at the links below."
 
62
  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>"
63
- examples=[['cat3.jpeg','cat4.jpeg',2]]
64
- gr.Interface(predict,[gr.inputs.Image(type='filepath'),gr.inputs.Image(type='filepath'),gr.inputs.Slider(minimum=2,maximum=4,step=1)],"playable_video",title=title,description=description,article=article,examples=examples).launch(enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+
3
  os.system("git clone https://github.com/google-research/frame-interpolation")
4
  import sys
5
+
6
  sys.path.append("frame-interpolation")
7
  import numpy as np
8
  import tensorflow as tf
 
16
  from image_tools.sizes import resize_and_crop
17
 
18
 
19
+ def load_model(model_name):
20
+ interpolator = interpolator.Interpolator(snapshot_download(repo_id=model_name), None)
21
+
22
+ return model
23
+
24
 
25
+ model_names = [
26
+ "akhaliq/frame-interpolation-film-style",
27
+ "akhaliq/frame-interpolation_film_l1",
28
+ "akhaliq/frame_interpolation_film_vgg",
29
+ "akhaliq/frame-interpolation-film-imagenet-vgg-verydeep-19"
30
+ ]
31
+
32
+ models = {model_name: load_model(model_name) for model_name in model_names}
33
 
34
  ffmpeg_path = util.get_ffmpeg_path()
35
  mediapy.set_ffmpeg(ffmpeg_path)
36
 
37
+
38
+ def resize(width, img):
39
+ basewidth = width
40
+ img = Image.open(img)
41
+ wpercent = (basewidth / float(img.size[0]))
42
+ hsize = int((float(img.size[1]) * float(wpercent)))
43
+ img = img.resize((basewidth, hsize), Image.ANTIALIAS)
44
+ return img
45
+
46
+
47
+ def resize_img(img1, img2):
48
+ img_target_size = Image.open(img1)
49
+ img_to_resize = resize_and_crop(
50
+ img2,
51
+ (img_target_size.size[0], img_target_size.size[1]), # set width and height to match img1
52
+ crop_origin="middle"
53
+ )
54
+ img_to_resize.save('resized_img2.png')
55
+
56
+
57
+ def predict(frame1, frame2, times_to_interpolate, model_name):
58
+ model = models[model_name]
59
+
60
+ frame1 = resize(256, frame1)
61
+ frame2 = resize(256, frame2)
62
 
63
  frame1.save("test1.png")
64
  frame2.save("test2.png")
65
 
66
+ resize_img("test1.png", "test2.png")
67
  input_frames = ["test1.png", "resized_img2.png"]
68
 
69
  frames = list(
70
  util.interpolate_recursively_from_files(
71
+ input_frames, times_to_interpolate, model))
72
 
73
  mediapy.write_video("out.mp4", frames, fps=30)
74
  return "out.mp4"
75
 
76
+
77
+ title = "frame-interpolation"
78
+ description = "Gradio demo for FILM: Frame Interpolation for Large Scene Motion. To use it, simply upload your images and add the times to interpolate number or click on one of the examples to load them. Read more at the links below."
79
  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>"
80
+ examples = [
81
+ ['cat3.jpeg', 'cat4.jpeg', 2, model_names[0]],
82
+ ['cat1.jpeg', 'cat2.jpeg', 2, model_names[1]],
83
+ ]
84
+
85
+ gr.Interface(
86
+ predict,
87
+ [
88
+ gr.inputs.Image(type='filepath'),
89
+ gr.inputs.Image(type='filepath'),
90
+ gr.inputs.Slider(minimum=2, maximum=4, step=1),
91
+ gr.inputs.Dropdown(choices=model_names, default=model_names[0])
92
+ ],
93
+ "playable_video",
94
+ title=title,
95
+ description=description,
96
+ article=article,
97
+ examples=examples
98
+ ).launch(enable_queue=True)