leonelhs commited on
Commit
76ccfaa
1 Parent(s): 34e673b

add options

Browse files
Files changed (3) hide show
  1. app.py +24 -12
  2. interpolator.py +20 -3
  3. requirements.txt +1 -0
app.py CHANGED
@@ -8,19 +8,25 @@ path = "./smoot.mp4"
8
  interpolator = Interpolator()
9
 
10
 
11
- def predict(image_a, image_b):
12
  image1 = load_image(image_a)
13
  image2 = load_image(image_b)
14
  input_frames = [image1, image2]
15
- frames = list(interpolate_recursively(input_frames, interpolator))
16
- mediapy.write_video(path, frames, fps=30)
 
 
 
 
 
 
17
  return path
18
 
19
 
20
  footer = r"""
21
  <center>
22
  <b>
23
- Demo for <a href='https://www.tensorflow.org/hub/tutorials/tf_hub_delf_module'>DELF</a>
24
  </b>
25
  </center>
26
  """
@@ -33,20 +39,26 @@ coffee&emoji=&slug=leonelhs&button_colour=FFDD00&font_colour=000000&font_family=
33
  </center>
34
  """
35
 
36
- with gr.Blocks(title="DELF") as app:
37
- gr.HTML("<center><h1>Match images using DELF</h1></center>")
38
- gr.HTML("<center><h3>Neural network and logic for processing images to identify keypoints and their "
39
- "descriptors.</h3></center>")
 
40
  with gr.Row(equal_height=False):
41
  with gr.Column():
42
- input_img_a = gr.Image(type="filepath", label="Input image A")
43
- input_img_b = gr.Image(type="filepath", label="Input image B")
 
 
 
 
44
  run_btn = gr.Button(variant="primary")
 
45
  with gr.Column():
46
- output_img = gr.Video(format="mp4", label="Interpolate video")
47
  gr.ClearButton(components=[input_img_a, input_img_b, output_img], variant="stop")
48
 
49
- run_btn.click(predict, [input_img_a, input_img_b], [output_img])
50
 
51
  with gr.Row():
52
  blobs_a = [[f"examples/image_a/{x:02d}.jpg"] for x in range(1, 2)]
 
8
  interpolator = Interpolator()
9
 
10
 
11
+ def predict(image_a, image_b, preview):
12
  image1 = load_image(image_a)
13
  image2 = load_image(image_b)
14
  input_frames = [image1, image2]
15
+ if preview:
16
+ fps = 3
17
+ frames = interpolator.preview_frames(input_frames)
18
+ else:
19
+ fps = 30
20
+ frames = list(interpolate_recursively(input_frames, interpolator))
21
+
22
+ mediapy.write_video(path, frames, fps=fps)
23
  return path
24
 
25
 
26
  footer = r"""
27
  <center>
28
  <b>
29
+ Demo for <a href='https://www.tensorflow.org/hub/tutorials/tf_hub_film_example'>FILM model</a>
30
  </b>
31
  </center>
32
  """
 
39
  </center>
40
  """
41
 
42
+ with gr.Blocks(title="FILM") as app:
43
+ gr.HTML("<center><h1>Frame interpolation using the FILM model</h1></center>")
44
+ gr.HTML("<center><h3>Frame interpolation is the task of synthesizing many in-between images from a given set of "
45
+ "images. The technique is often used for frame rate upsampling or creating slow-motion video "
46
+ "effects.</h3></center>")
47
  with gr.Row(equal_height=False):
48
  with gr.Column():
49
+ with gr.Row(equal_height=True):
50
+ with gr.Column():
51
+ input_img_a = gr.Image(type="filepath", label="Input image A")
52
+ with gr.Column():
53
+ input_img_b = gr.Image(type="filepath", label="Input image B")
54
+ pre = gr.Checkbox(label="Preview", value=True, info="Run in preview mode video")
55
  run_btn = gr.Button(variant="primary")
56
+
57
  with gr.Column():
58
+ output_img = gr.Video(format="mp4", label="Interpolate video", autoplay=True)
59
  gr.ClearButton(components=[input_img_a, input_img_b, output_img], variant="stop")
60
 
61
+ run_btn.click(predict, [input_img_a, input_img_b, pre], [output_img])
62
 
63
  with gr.Row():
64
  blobs_a = [[f"examples/image_a/{x:02d}.jpg"] for x in range(1, 2)]
interpolator.py CHANGED
@@ -1,7 +1,8 @@
 
 
1
  import numpy as np
2
  import tensorflow as tf
3
- import tensorflow_hub as hub
4
- from typing import Generator, List, Iterable
5
 
6
  """A wrapper class for running a frame interpolation based on the FILM model on TFHub
7
 
@@ -12,6 +13,8 @@ Usage:
12
  (B,H,W,C) layout, batch_dt is the sub-frame time in range [0..1], (B,) layout.
13
  """
14
 
 
 
15
 
16
  def _pad_to_align(x, align):
17
  """Pads image batch x so width and height divide by align.
@@ -63,7 +66,9 @@ class Interpolator:
63
  inference.'
64
  """
65
  self.times_to_interpolate = times_to_interpolate
66
- self._model = hub.load("https://tfhub.dev/google/film/1")
 
 
67
  self._align = align
68
 
69
  def __call__(self, x0: np.ndarray, x1: np.ndarray,
@@ -92,6 +97,18 @@ class Interpolator:
92
  image = tf.image.crop_to_bounding_box(image, **bbox_to_crop)
93
  return image.numpy()
94
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  def _recursive_generator(
97
  frame1: np.ndarray, frame2: np.ndarray, num_recursions: int,
 
1
+ from typing import Generator, List, Iterable
2
+
3
  import numpy as np
4
  import tensorflow as tf
5
+ from huggingface_hub import snapshot_download
 
6
 
7
  """A wrapper class for running a frame interpolation based on the FILM model on TFHub
8
 
 
13
  (B,H,W,C) layout, batch_dt is the sub-frame time in range [0..1], (B,) layout.
14
  """
15
 
16
+ FILM_REPO_ID = "leonelhs/film"
17
+
18
 
19
  def _pad_to_align(x, align):
20
  """Pads image batch x so width and height divide by align.
 
66
  inference.'
67
  """
68
  self.times_to_interpolate = times_to_interpolate
69
+ model_path = snapshot_download(FILM_REPO_ID)
70
+ self._model = tf.saved_model.load(model_path)
71
+ # self._model = hub.load("https://tfhub.dev/google/film/1")
72
  self._align = align
73
 
74
  def __call__(self, x0: np.ndarray, x1: np.ndarray,
 
97
  image = tf.image.crop_to_bounding_box(image, **bbox_to_crop)
98
  return image.numpy()
99
 
100
+ def preview_frames(self, frames: List[np.ndarray]):
101
+
102
+ time = np.array([0.5], dtype=np.float32)
103
+
104
+ media_input = {
105
+ 'time': np.expand_dims(time, axis=0), # adding the batch dimension to the time
106
+ 'x0': np.expand_dims(frames[0], axis=0), # adding the batch dimension to the image
107
+ 'x1': np.expand_dims(frames[1], axis=0) # adding the batch dimension to the image
108
+ }
109
+ mid = self._model(media_input)
110
+ return [frames[0], mid['image'][0].numpy(), frames[1]]
111
+
112
 
113
  def _recursive_generator(
114
  frame1: np.ndarray, frame2: np.ndarray, num_recursions: int,
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  tensorflow>=2.15.0
2
  tensorflow-hub>=0.15.0
3
  requests>=2.31.0
 
1
+ gradio
2
  tensorflow>=2.15.0
3
  tensorflow-hub>=0.15.0
4
  requests>=2.31.0