Spaces:
Sleeping
Sleeping
File size: 3,668 Bytes
8eeb7b8 b5322cd 8eeb7b8 74369d2 2a0448b b5322cd 8eeb7b8 2a0448b 8eeb7b8 2a0448b 74369d2 b5322cd 8eeb7b8 b5322cd 74369d2 b5322cd ffe8226 b5322cd 35057b4 b5322cd 2a0448b b5322cd 0c0e9f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
from interactive_pipe import interactive_pipeline, interactive, Image, TextPrompt
import cv2
from typing import Tuple
import numpy as np
# Processing blocks
# -----------------
def processing_block(
inp: np.ndarray,
amplify: float = 1.,
context: dict = {}
) -> np.ndarray:
context["amplify"] = amplify
return amplify*inp
def draw_text(img: np.ndarray, free_text: str = "Hello", preset_text: str = "choice") -> np.ndarray:
for txt, pos in [(free_text, (10, 50)), (preset_text, (10, 100))]:
out = cv2.putText(
img,
txt,
pos,
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 255, 255), 2
)
return out
def add_extra_caption(img: np.ndarray, caption: str = "Extra caption") -> np.ndarray:
out = cv2.putText(
img,
caption,
(10, 200),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 0, 255), 2
)
return out
def move_circle(img: np.ndarray, x: int = 100, y: int = 100) -> np.ndarray:
out = img.copy()
cv2.circle(out, (x, y), 10, (255, 0, 0), -1)
return out
def remove_caps(txt_prefix: str, txt_in: str = "TUTU") -> str:
return txt_prefix + "\n" + txt_in.lower()
# Pipeline definition
# -------------------
def pipe(inp: np.ndarray, txt_prefix) -> Tuple[np.ndarray, np.ndarray]:
out = add_extra_caption(inp)
out = processing_block(out)
out = draw_text(out)
out = move_circle(out)
txt_out = remove_caps(txt_prefix)
return inp, out, txt_out
# Add interactivity to the processing blocks
# ------------------------------------------
def add_controls() -> None:
# 2 ways to add prompts:
# 1 = free_text=TextPrompt("DEFAULT TEXT", name="Caption custom")
# 2 = free_text=("DEFAULT TEXT", None, "Write Free text")
interactive(
caption=TextPrompt("Created with interactive pipe",
name="Caption custom")
)(add_extra_caption)
interactive(
free_text=("Hello world!", None, "Write Free text"),
preset_text=("Hello", ["Hello", "World", "Goodbye"])
)(draw_text)
# Keyboards shortcuts: up, down, left, right -> only supported when using the Qt/MPL backend
# Replaced by sliders when using the Gradio backend
interactive(
x=(100, [0, 200], "X", ["left", "right"]),
y=(100, [0, 200], "Y", ["up", "down"])
)(move_circle)
interactive(
amplify=(0.5, [0., 1.], "Amplify")
)(processing_block)
interactive(
txt_in=TextPrompt("OUT TEXT",
name="Out text no caps")
)(remove_caps)
# Add interactivity to the processing blocks
# ------------------------------------------
def launch(img: np.ndarray, backend: str = "gradio", markdown_description: str = "") -> None:
add_controls()
pipe_interactive = interactive_pipeline(
gui=backend,
cache=True,
markdown_description=markdown_description
)(pipe)
pipe_interactive(img, "REMOVE CAPS! ")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--backend", default="gradio",
choices=["gradio", "qt", "mpl"], type=str)
parser.add_argument(
"-d", "--debug", action="store_true",
help="Debug mode (to tune difficulty and tolerance)"
)
args = parser.parse_args()
markdown_description = "# Code to build this app on gradio \n\n"
markdown_description += "```python\n"+open(__file__, 'r').read()+"```"
img = Image.load_image("sample.jpg")
launch(img, backend=args.backend, markdown_description=markdown_description)
|