Spaces:
Running
Running
Fix not implemented
Browse files
app.py
CHANGED
@@ -9,9 +9,10 @@ import base64
|
|
9 |
import io
|
10 |
from PIL import Image
|
11 |
from functools import wraps
|
|
|
12 |
|
13 |
|
14 |
-
DEFAULT_TRANSFORM = "
|
15 |
|
16 |
DEFAULT_IMAGE = "images/doctor.webp"
|
17 |
DEFAULT_IMAGE_HEIGHT = 400
|
@@ -58,22 +59,30 @@ for mask in BASE64_DEFAULT_MASKS:
|
|
58 |
def run_with_retry(compose):
|
59 |
@wraps(compose)
|
60 |
def wrapper(*args, **kwargs):
|
61 |
-
|
|
|
62 |
try:
|
63 |
-
|
|
|
64 |
except NotImplementedError as e:
|
65 |
print(f"Caught NotImplementedError: {e}")
|
66 |
if "bbox" in str(e):
|
67 |
kwargs.pop("bboxes", None)
|
68 |
kwargs.pop("category_id", None)
|
|
|
69 |
if "keypoint" in str(e):
|
70 |
kwargs.pop("keypoints", None)
|
|
|
71 |
if "mask" in str(e):
|
72 |
kwargs.pop("mask", None)
|
|
|
|
|
|
|
|
|
|
|
73 |
return wrapper
|
74 |
|
75 |
|
76 |
-
|
77 |
def draw_boxes(image, boxes, color=(255, 0, 0), thickness=2) -> np.ndarray:
|
78 |
"""Draw boxes with PIL."""
|
79 |
pil_image = Image.fromarray(image)
|
@@ -113,7 +122,15 @@ def draw_not_implemented_image(image):
|
|
113 |
"""Draw the image with a text. In the middle."""
|
114 |
pil_image = Image.fromarray(image)
|
115 |
draw = ImageDraw.Draw(pil_image)
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
return np.array(pil_image)
|
118 |
|
119 |
|
@@ -124,7 +141,9 @@ def get_formatted_signature(function_or_class, indentation=4):
|
|
124 |
|
125 |
args = []
|
126 |
for param in signature.parameters.values():
|
127 |
-
if param.
|
|
|
|
|
128 |
str_param = f"{param.name}=,"
|
129 |
else:
|
130 |
if isinstance(param.default, str):
|
@@ -208,28 +227,30 @@ def get_formatted_transform_docs(transform_number):
|
|
208 |
with gr.Blocks() as demo:
|
209 |
|
210 |
with gr.Row():
|
211 |
-
with gr.
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
)
|
227 |
-
|
228 |
-
language="python",
|
229 |
-
value=get_formatted_transform(transforms_keys.index(DEFAULT_TRANSFORM)),
|
230 |
-
interactive=True,
|
231 |
-
lines=5,
|
232 |
-
)
|
233 |
#info = gr.Text(interactive=False, label="Image info", value="")
|
234 |
image = gr.Image(
|
235 |
value=DEFAULT_IMAGE,
|
@@ -244,7 +265,6 @@ with gr.Blocks() as demo:
|
|
244 |
|
245 |
#image.upload(fn=update_image_info, inputs=[image], outputs=[info])
|
246 |
select.change(fn=get_formatted_transform, inputs=[select], outputs=[code])
|
247 |
-
button = gr.Button("Run")
|
248 |
button.click(fn=update, inputs=[image, code], outputs=[augmented_image])
|
249 |
|
250 |
|
|
|
9 |
import io
|
10 |
from PIL import Image
|
11 |
from functools import wraps
|
12 |
+
from copy import deepcopy
|
13 |
|
14 |
|
15 |
+
DEFAULT_TRANSFORM = "CoarseDropout"
|
16 |
|
17 |
DEFAULT_IMAGE = "images/doctor.webp"
|
18 |
DEFAULT_IMAGE_HEIGHT = 400
|
|
|
59 |
def run_with_retry(compose):
|
60 |
@wraps(compose)
|
61 |
def wrapper(*args, **kwargs):
|
62 |
+
processors = deepcopy(compose.processors)
|
63 |
+
for _ in range(4):
|
64 |
try:
|
65 |
+
result = compose(*args, **kwargs)
|
66 |
+
break
|
67 |
except NotImplementedError as e:
|
68 |
print(f"Caught NotImplementedError: {e}")
|
69 |
if "bbox" in str(e):
|
70 |
kwargs.pop("bboxes", None)
|
71 |
kwargs.pop("category_id", None)
|
72 |
+
compose.processors.pop("bboxes")
|
73 |
if "keypoint" in str(e):
|
74 |
kwargs.pop("keypoints", None)
|
75 |
+
compose.processors.pop("keypoints")
|
76 |
if "mask" in str(e):
|
77 |
kwargs.pop("mask", None)
|
78 |
+
except Exception as e:
|
79 |
+
compose.processors = processors
|
80 |
+
raise e
|
81 |
+
compose.processors = processors
|
82 |
+
return result
|
83 |
return wrapper
|
84 |
|
85 |
|
|
|
86 |
def draw_boxes(image, boxes, color=(255, 0, 0), thickness=2) -> np.ndarray:
|
87 |
"""Draw boxes with PIL."""
|
88 |
pil_image = Image.fromarray(image)
|
|
|
122 |
"""Draw the image with a text. In the middle."""
|
123 |
pil_image = Image.fromarray(image)
|
124 |
draw = ImageDraw.Draw(pil_image)
|
125 |
+
# align in the centerm, and make bigger font
|
126 |
+
text = "NOT IMPLEMETED FOR THIS TYPE OF ANNOTATIONS"
|
127 |
+
length = draw.textlength(text)
|
128 |
+
draw.text(
|
129 |
+
(DEFAULT_IMAGE_WIDTH // 2 - length // 2, DEFAULT_IMAGE_HEIGHT // 2),
|
130 |
+
text,
|
131 |
+
fill=(255, 0, 0),
|
132 |
+
align="center",
|
133 |
+
)
|
134 |
return np.array(pil_image)
|
135 |
|
136 |
|
|
|
141 |
|
142 |
args = []
|
143 |
for param in signature.parameters.values():
|
144 |
+
if param.name == "p":
|
145 |
+
str_param = "p=1.0,"
|
146 |
+
elif param.default == inspect.Parameter.empty:
|
147 |
str_param = f"{param.name}=,"
|
148 |
else:
|
149 |
if isinstance(param.default, str):
|
|
|
227 |
with gr.Blocks() as demo:
|
228 |
|
229 |
with gr.Row():
|
230 |
+
with gr.Column():
|
231 |
+
with gr.Group():
|
232 |
+
select = gr.Dropdown(
|
233 |
+
label="Select a transformation",
|
234 |
+
choices=transforms_keys,
|
235 |
+
value=DEFAULT_TRANSFORM,
|
236 |
+
type="index",
|
237 |
+
interactive=True,
|
238 |
+
)
|
239 |
+
with gr.Accordion("Documentation", open=False):
|
240 |
+
docs = gr.TextArea(
|
241 |
+
get_formatted_transform_docs(
|
242 |
+
transforms_keys.index(DEFAULT_TRANSFORM)
|
243 |
+
),
|
244 |
+
show_label=False,
|
245 |
+
interactive=False,
|
246 |
+
)
|
247 |
+
code = gr.Code(
|
248 |
+
language="python",
|
249 |
+
value=get_formatted_transform(transforms_keys.index(DEFAULT_TRANSFORM)),
|
250 |
+
interactive=True,
|
251 |
+
lines=5,
|
252 |
)
|
253 |
+
button = gr.Button("Run")
|
|
|
|
|
|
|
|
|
|
|
254 |
#info = gr.Text(interactive=False, label="Image info", value="")
|
255 |
image = gr.Image(
|
256 |
value=DEFAULT_IMAGE,
|
|
|
265 |
|
266 |
#image.upload(fn=update_image_info, inputs=[image], outputs=[info])
|
267 |
select.change(fn=get_formatted_transform, inputs=[select], outputs=[code])
|
|
|
268 |
button.click(fn=update, inputs=[image, code], outputs=[augmented_image])
|
269 |
|
270 |
|