|
from interactive_pipe import interactive_pipeline, interactive |
|
import argparse |
|
from typing import Tuple |
|
import numpy as np |
|
|
|
|
|
|
|
|
|
def gen_color( |
|
green: float = 0.8, |
|
decay: int = 1 |
|
) -> np.ndarray: |
|
coord = np.linspace(0., 1., 256) |
|
X, Y = np.meshgrid(np.exp(-coord*decay), coord) |
|
return np.stack([X, green*Y, Y[::-1]], -1) |
|
|
|
|
|
def flip_img( |
|
img: np.ndarray, |
|
flip: bool = True |
|
) -> Tuple[np.ndarray, np.ndarray]: |
|
out = img[::-1] if flip else img.copy() |
|
return out, img[:, ::-1] |
|
|
|
|
|
def amplify( |
|
img: np.ndarray, |
|
amp: str = "L" |
|
) -> np.ndarray: |
|
return 1/{"S": 4, "M": 3, "L": 2, "XL": 1}[amp] * img |
|
|
|
|
|
|
|
|
|
def pipe() -> Tuple[np.ndarray, np.ndarray, np.ndarray]: |
|
inp = gen_color() |
|
out_flip, out_mirror = flip_img(inp) |
|
out = amplify(out_mirror) |
|
return [inp, out_flip, out] |
|
|
|
|
|
|
|
|
|
def add_controls() -> None: |
|
|
|
interactive( |
|
green=(1., [0., 1.], "Green amount"), |
|
decay=(2, [0, 10], "Color decay") |
|
)(gen_color) |
|
interactive( |
|
amp=("L", ["S", "M", "L", "XL"], "Amplify") |
|
)(amplify) |
|
interactive( |
|
flip=(True, "Flip") |
|
)(flip_img) |
|
|
|
|
|
def launch(backend="gradio", markdown_description="") -> None: |
|
|
|
playable_pipe = interactive_pipeline( |
|
gui=backend, |
|
markdown_description=markdown_description |
|
)(pipe) |
|
playable_pipe() |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument("-b", "--backend", default="gradio", |
|
choices=["gradio", "qt", "mpl"], type=str) |
|
args = parser.parse_args() |
|
markdown_description = "# Code to build this app on gradio \n\n" |
|
markdown_description += "```python\n"+open(__file__, 'r').read()+"```" |
|
add_controls() |
|
launch(args.backend, markdown_description) |
|
|