balthou commited on
Commit
8eeb7b8
·
1 Parent(s): b5322cd

add text prompting

Browse files
Files changed (2) hide show
  1. app.py +55 -2
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,4 +1,5 @@
1
- from interactive_pipe import interactive_pipeline, interactive, Image
 
2
  from typing import Tuple
3
  import numpy as np
4
 
@@ -14,11 +15,45 @@ def processing_block(
14
  return amplify*inp
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Pipeline definition
18
  # -------------------
19
 
 
20
  def pipe(inp: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
21
- out = processing_block(inp)
 
 
 
22
  return inp, out
23
 
24
  # Add interactivity to the processing blocks
@@ -26,6 +61,24 @@ def pipe(inp: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
26
 
27
 
28
  def add_controls() -> None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  interactive(
30
  amplify=(0.5, [0., 1.], "Amplify")
31
  )(processing_block)
 
1
+ from interactive_pipe import interactive_pipeline, interactive, Image, TextPrompt
2
+ import cv2
3
  from typing import Tuple
4
  import numpy as np
5
 
 
15
  return amplify*inp
16
 
17
 
18
+ def draw_text(img: np.ndarray, free_text: str = "Hello", preset_text: str = "choice") -> np.ndarray:
19
+ for txt, pos in [(free_text, (10, 50)), (preset_text, (10, 100))]:
20
+ out = cv2.putText(
21
+ img,
22
+ txt,
23
+ pos,
24
+ cv2.FONT_HERSHEY_SIMPLEX,
25
+ 1,
26
+ (255, 255, 255), 2
27
+ )
28
+ return out
29
+
30
+
31
+ def add_extra_caption(img: np.ndarray, caption: str = "Extra caption") -> np.ndarray:
32
+ out = cv2.putText(
33
+ img,
34
+ caption,
35
+ (10, 200),
36
+ cv2.FONT_HERSHEY_SIMPLEX,
37
+ 1,
38
+ (255, 0, 255), 2
39
+ )
40
+ return out
41
+
42
+
43
+ def move_circle(img: np.ndarray, x: int = 100, y: int = 100) -> np.ndarray:
44
+ out = img.copy()
45
+ cv2.circle(out, (x, y), 10, (255, 0, 0), -1)
46
+ return out
47
+
48
  # Pipeline definition
49
  # -------------------
50
 
51
+
52
  def pipe(inp: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
53
+ out = add_extra_caption(inp)
54
+ out = processing_block(out)
55
+ out = draw_text(out)
56
+ out = move_circle(out)
57
  return inp, out
58
 
59
  # Add interactivity to the processing blocks
 
61
 
62
 
63
  def add_controls() -> None:
64
+
65
+ # 2 ways to add prompts:
66
+ # 1 = free_text=TextPrompt("DEFAULT TEXT", name="Caption custom")
67
+ # 2 = free_text=("DEFAULT TEXT", None, "Write Free text")
68
+ interactive(
69
+ caption=TextPrompt("Created with interactive pipe",
70
+ name="Caption custom")
71
+ )(add_extra_caption)
72
+ interactive(
73
+ free_text=("Hello world!", None, "Write Free text"),
74
+ preset_text=("Hello", ["Hello", "World", "Goodbye"])
75
+ )(draw_text)
76
+ # Keyboards shortcuts: up, down, left, right -> only supported when using the Qt/MPL backend
77
+ # Replaced by sliders when using the Gradio backend
78
+ interactive(
79
+ x=(100, [0, 200], "X", ["left", "right"]),
80
+ y=(100, [0, 200], "Y", ["up", "down"])
81
+ )(move_circle)
82
  interactive(
83
  amplify=(0.5, [0., 1.], "Amplify")
84
  )(processing_block)
requirements.txt CHANGED
@@ -1 +1 @@
1
- interactive-pipe>=0.8.3
 
1
+ interactive-pipe>=0.8.4