improve geometry pool/traffic light/circle
Browse files
app.py
CHANGED
|
@@ -13,19 +13,33 @@ from transformers import pipeline
|
|
| 13 |
|
| 14 |
|
| 15 |
@interactive(
|
| 16 |
-
|
| 17 |
-
|
| 18 |
)
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
out
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
balls = [
|
| 28 |
-
((0
|
| 29 |
((0.0, -0.6), [1, 1, 0]),
|
| 30 |
((-0.15, -0.85), [1, 0, 0]),
|
| 31 |
((0.0, -0.85), [0, 1, 0]),
|
|
@@ -33,18 +47,44 @@ def gen_color(
|
|
| 33 |
((-0.075, -0.725), [1, 1, 0]),
|
| 34 |
((0.075, -0.725), [1, 0, 0]),
|
| 35 |
]
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return out
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
@interactive(detect=(True, "Enable classification"))
|
| 49 |
def apply_classifier(img, context={}, detect=False):
|
| 50 |
result = {}
|
|
@@ -80,10 +120,13 @@ def display_result(context={}):
|
|
| 80 |
|
| 81 |
|
| 82 |
def tutorial_pipeline():
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
result_curve = display_result()
|
| 86 |
-
return
|
| 87 |
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
@interactive(
|
| 16 |
+
background_color=("green", ["green", "blue", "red"]),
|
| 17 |
+
border_size=(0.05, [0., 0.3]),
|
| 18 |
)
|
| 19 |
+
def generate_background(background_color="green", border_size: float = 0.) -> np.ndarray:
|
| 20 |
+
out = np.zeros((256, 128, 3)) # Initial background set to black
|
| 21 |
+
border_int = int(border_size * 256)
|
| 22 |
+
out[border_int:out.shape[0]-border_int, border_int:out.shape[1]-border_int,
|
| 23 |
+
["red", "green", "blue"].index(background_color)] = 0.5
|
| 24 |
+
return out
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@interactive(
|
| 28 |
+
radius=(0.005, [0., 0.01]), # continuous slider (float)
|
| 29 |
+
spread=(1., [0., 2.]), # continuous slider (float)
|
| 30 |
+
geometric_shape=("snooker", ["snooker", "circle", "traffic light"]),
|
| 31 |
+
)
|
| 32 |
+
def add_circles(
|
| 33 |
+
background: np.ndarray,
|
| 34 |
+
radius: float = 0.,
|
| 35 |
+
spread: float = 1.,
|
| 36 |
+
geometric_shape: str = "snooker",
|
| 37 |
+
) -> np.ndarray:
|
| 38 |
+
out = background.copy()
|
| 39 |
+
x, y = np.meshgrid(
|
| 40 |
+
np.linspace(-1, 1, out.shape[1]), np.linspace(-1, 1, out.shape[0]))
|
| 41 |
balls = [
|
| 42 |
+
((0., 0.3), [0.8, 0.8, 0.8]), # Cue ball (white)
|
| 43 |
((0.0, -0.6), [1, 1, 0]),
|
| 44 |
((-0.15, -0.85), [1, 0, 0]),
|
| 45 |
((0.0, -0.85), [0, 1, 0]),
|
|
|
|
| 47 |
((-0.075, -0.725), [1, 1, 0]),
|
| 48 |
((0.075, -0.725), [1, 0, 0]),
|
| 49 |
]
|
| 50 |
+
circle_clock = [
|
| 51 |
+
((np.cos(angle), np.sin(angle)), [1, 1, 0]) for angle in np.linspace(0, 2*np.pi, 12)
|
| 52 |
+
]
|
| 53 |
+
traffic_light = [
|
| 54 |
+
((0.0, 0.0), [1, 0.8, 0]),
|
| 55 |
+
((0.0, 0.12), [0, 1, 0]),
|
| 56 |
+
((0.0, -0.12), [1, 0, 0])
|
| 57 |
+
]
|
| 58 |
|
| 59 |
+
chosen_pattern = {"circle": circle_clock, "snooker": balls,
|
| 60 |
+
"traffic light": traffic_light}[geometric_shape]
|
| 61 |
+
for (cx, cy), color in chosen_pattern:
|
| 62 |
+
r = (x - spread*cx) ** 2 + (y - spread*cy) ** 2
|
| 63 |
+
out[r < radius, :] = color
|
| 64 |
return out
|
| 65 |
|
| 66 |
|
| 67 |
+
@interactive(add_stick=(False, "Add black rectangle"))
|
| 68 |
+
def add_details(img: np.ndarray, add_stick: bool = False) -> np.ndarray:
|
| 69 |
+
out = img.copy()
|
| 70 |
+
x, y = np.meshgrid(
|
| 71 |
+
np.linspace(-1, 1, out.shape[1]), np.linspace(-1, 1, out.shape[0]))
|
| 72 |
+
if add_stick:
|
| 73 |
+
# out[(np.abs(x)+0.5*np.abs(y)) < 0.3] = 0. # [0.8, 0.8, 0.]
|
| 74 |
+
mask = (np.abs(x) < 0.1) * (0.75*np.abs(y) < 0.2)
|
| 75 |
+
out[mask, :] = 0.
|
| 76 |
+
return out
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@interactive(
|
| 80 |
+
noise_level=(0.05, [0., 0.2]),
|
| 81 |
+
seed=(42, [-1, 100])
|
| 82 |
+
)
|
| 83 |
+
def add_noise(img: np.ndarray, noise_level: float = 0., seed: int = 42):
|
| 84 |
+
np.random.seed(seed)
|
| 85 |
+
return (img + np.random.normal(0, noise_level, img.shape)).clip(0., 1.)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
@interactive(detect=(True, "Enable classification"))
|
| 89 |
def apply_classifier(img, context={}, detect=False):
|
| 90 |
result = {}
|
|
|
|
| 120 |
|
| 121 |
|
| 122 |
def tutorial_pipeline():
|
| 123 |
+
background = generate_background()
|
| 124 |
+
foreground = add_details(background)
|
| 125 |
+
foreground = add_circles(foreground)
|
| 126 |
+
noisy_input = add_noise(foreground)
|
| 127 |
+
apply_classifier(noisy_input)
|
| 128 |
result_curve = display_result()
|
| 129 |
+
return [[background, foreground], [noisy_input, result_curve]]
|
| 130 |
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|