balthou commited on
Commit
a369310
·
1 Parent(s): 6be4e9d

update example code

Browse files
Files changed (1) hide show
  1. app.py +62 -30
app.py CHANGED
@@ -1,44 +1,76 @@
1
  from interactive_pipe import interactive_pipeline, interactive
 
 
2
  import numpy as np
3
 
 
4
 
5
- @interactive(angle=(45., [0., 360.]))
6
- def gen_color(angle=45.):
7
- lin_coord = np.linspace(-1, 1, 256)
8
- X, Y = np.meshgrid(lin_coord, lin_coord)
9
- ang = np.deg2rad(angle)
10
- im = np.cos(ang) * X + np.sin(ang) * Y
11
- im = np.clip(1+im, 0, 1)
12
- im = np.stack(
13
- [im, im[::-1], im[:, ::-1]],
14
- axis=-1
15
- )
16
- return im
17
 
 
 
 
 
 
 
 
18
 
19
- @interactive(
20
- flip=(True, "Flip Image"),
21
- mirror=(True, "Mirror Image")
22
- )
23
- def flip_image(img, flip=True, mirror=True):
24
- img = img[::-1] if flip else img
25
- img = img[:, ::-1] if mirror else img
26
- return img
27
 
 
 
 
 
 
 
28
 
29
- markdown_description = "# Code to build this app on gradio \n"
30
- markdown_description += "```python\n"+open(__file__, 'r').read()+"```"
31
 
 
 
 
 
 
32
 
33
- @interactive_pipeline(
34
- gui="gradio",
35
- markdown_description=markdown_description
36
- )
37
- def simple_pipe():
38
  inp = gen_color()
39
- out = flip_image(inp)
40
- return [inp, out]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
 
 
43
  if __name__ == "__main__":
44
- simple_pipe()
 
 
 
 
 
 
 
 
1
  from interactive_pipe import interactive_pipeline, interactive
2
+ import argparse
3
+ from typing import Tuple
4
  import numpy as np
5
 
6
+ # --- Define processing blocks ---
7
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ def gen_color(
10
+ green: float = 0.8,
11
+ decay: int = 1
12
+ ) -> np.ndarray: # 1 image output
13
+ coord = np.linspace(0., 1., 256)
14
+ X, Y = np.meshgrid(np.exp(-coord*decay), coord)
15
+ return np.stack([X, green*Y, Y[::-1]], -1)
16
 
 
 
 
 
 
 
 
 
17
 
18
+ def flip_img(
19
+ img: np.ndarray, # 1 image input
20
+ flip: bool = True
21
+ ) -> Tuple[np.ndarray, np.ndarray]: # 2 image outputs
22
+ out = img[::-1] if flip else img.copy()
23
+ return out, img[:, ::-1]
24
 
 
 
25
 
26
+ def amplify(
27
+ img: np.ndarray, # 1 image input
28
+ amp: str = "L"
29
+ ) -> np.ndarray: # 1 image output
30
+ return 1/{"S": 4, "M": 3, "L": 2, "XL": 1}[amp] * img
31
 
32
+ # --- Define pipeline = a sequence of processing blocks ---
33
+
34
+
35
+ def pipe() -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
 
36
  inp = gen_color()
37
+ out_flip, out_mirror = flip_img(inp)
38
+ out = amplify(out_mirror)
39
+ return [inp, out_flip, out] # Display 3 images side by side
40
+
41
+
42
+ # --- Add interactivity ---
43
+
44
+ def add_controls() -> None:
45
+ # Add controls to the processing blocks
46
+ interactive(
47
+ green=(1., [0., 1.], "Green amount"), # float <-> continuous slider
48
+ decay=(2, [0, 10], "Color decay") # int <-> discrete slider
49
+ )(gen_color)
50
+ interactive(
51
+ amp=("L", ["S", "M", "L", "XL"], "Amplify") # str <-> dropdown menu
52
+ )(amplify)
53
+ interactive(
54
+ flip=(True, "Flip") # bool <-> checkbox
55
+ )(flip_img)
56
+
57
+
58
+ def launch(backend="gradio", markdown_description="") -> None:
59
+ # Decorate pipeline which becomes interactive
60
+ playable_pipe = interactive_pipeline(
61
+ gui=backend,
62
+ markdown_description=markdown_description
63
+ )(pipe)
64
+ playable_pipe() # Launch the interactive pipeline
65
 
66
 
67
+ # --------------------------------------------------
68
  if __name__ == "__main__":
69
+ parser = argparse.ArgumentParser()
70
+ parser.add_argument("-b", "--backend", default="gradio",
71
+ choices=["gradio", "qt", "mpl"], type=str)
72
+ args = parser.parse_args()
73
+ markdown_description = "# Code to build this app on gradio \n\n"
74
+ markdown_description += "```python\n"+open(__file__, 'r').read()+"```"
75
+ add_controls()
76
+ launch(args.backend, markdown_description)