Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,34 @@ from scipy.interpolate import splprep, splev
|
|
| 19 |
from scipy.ndimage import gaussian_filter1d
|
| 20 |
import json
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 24 |
"zhengpeng7/BiRefNet", trust_remote_code=True
|
|
@@ -345,37 +373,90 @@ def predict(image, offset, coin_size_mm):
|
|
| 345 |
scaling_factor,
|
| 346 |
)
|
| 347 |
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
os.makedirs("./outputs", exist_ok=True)
|
| 350 |
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
|
|
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
["./examples/Test21.jpg", 0.15],
|
| 373 |
-
["./examples/Test22.jpg", 0.15],
|
| 374 |
-
["./examples/Test23.jpg", 0.15],
|
| 375 |
-
],
|
| 376 |
-
)
|
| 377 |
-
ifer.launch(share=True)
|
| 378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
|
|
|
|
|
| 19 |
from scipy.ndimage import gaussian_filter1d
|
| 20 |
import json
|
| 21 |
|
| 22 |
+
# Language translations
|
| 23 |
+
TRANSLATIONS = {
|
| 24 |
+
"english": {
|
| 25 |
+
"input_image": "Input Image",
|
| 26 |
+
"offset_value": "Offset value for Mask(mm)",
|
| 27 |
+
"coin_diameter": "Diameter of reference coin(mm). Adjust according to coin.",
|
| 28 |
+
"output_image": "Output Image",
|
| 29 |
+
"outlines": "Outlines of Objects",
|
| 30 |
+
"dxf_file": "DXF file",
|
| 31 |
+
"mask": "Mask",
|
| 32 |
+
"scaling_factor": "Scaling Factor(mm)",
|
| 33 |
+
"scaling_placeholder": "Every pixel is equal to mentioned number in millimeters",
|
| 34 |
+
"language_selector": "Select Language",
|
| 35 |
+
},
|
| 36 |
+
"dutch": {
|
| 37 |
+
"input_image": "Invoer Afbeelding",
|
| 38 |
+
"offset_value": "Offset waarde voor Masker(mm)",
|
| 39 |
+
"coin_diameter": "Diameter van referentiemunt(mm). Pas aan volgens munt.",
|
| 40 |
+
"output_image": "Uitvoer Afbeelding",
|
| 41 |
+
"outlines": "Contouren van Objecten",
|
| 42 |
+
"dxf_file": "DXF bestand",
|
| 43 |
+
"mask": "Masker",
|
| 44 |
+
"scaling_factor": "Schalingsfactor(mm)",
|
| 45 |
+
"scaling_placeholder": "Elke pixel is gelijk aan genoemd aantal in millimeters",
|
| 46 |
+
"language_selector": "Selecteer Taal",
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
|
| 51 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 52 |
"zhengpeng7/BiRefNet", trust_remote_code=True
|
|
|
|
| 373 |
scaling_factor,
|
| 374 |
)
|
| 375 |
|
| 376 |
+
def update_interface(language):
|
| 377 |
+
"""Updates the interface labels based on selected language"""
|
| 378 |
+
return [
|
| 379 |
+
gr.Image(label=TRANSLATIONS[language]["input_image"], type="numpy"),
|
| 380 |
+
gr.Number(label=TRANSLATIONS[language]["offset_value"], value=0.15),
|
| 381 |
+
gr.Number(label=TRANSLATIONS[language]["coin_diameter"], value=20),
|
| 382 |
+
gr.Image(label=TRANSLATIONS[language]["output_image"]),
|
| 383 |
+
gr.Image(label=TRANSLATIONS[language]["outlines"]),
|
| 384 |
+
gr.File(label=TRANSLATIONS[language]["dxf_file"]),
|
| 385 |
+
gr.Image(label=TRANSLATIONS[language]["mask"]),
|
| 386 |
+
gr.Textbox(
|
| 387 |
+
label=TRANSLATIONS[language]["scaling_factor"],
|
| 388 |
+
placeholder=TRANSLATIONS[language]["scaling_placeholder"],
|
| 389 |
+
),
|
| 390 |
+
]
|
| 391 |
+
|
| 392 |
+
if __name__ == "__main__":
|
| 393 |
os.makedirs("./outputs", exist_ok=True)
|
| 394 |
|
| 395 |
+
with gr.Blocks() as demo:
|
| 396 |
+
# Language selector
|
| 397 |
+
language = gr.Dropdown(
|
| 398 |
+
choices=["english", "dutch"],
|
| 399 |
+
value="english",
|
| 400 |
+
label="Select Language",
|
| 401 |
+
interactive=True
|
| 402 |
+
)
|
| 403 |
|
| 404 |
+
# Initialize interface components
|
| 405 |
+
input_image = gr.Image(label=TRANSLATIONS["english"]["input_image"], type="numpy")
|
| 406 |
+
offset = gr.Number(label=TRANSLATIONS["english"]["offset_value"], value=0.15)
|
| 407 |
+
coin_size = gr.Number(label=TRANSLATIONS["english"]["coin_diameter"], value=20)
|
| 408 |
+
|
| 409 |
+
output_image = gr.Image(label=TRANSLATIONS["english"]["output_image"])
|
| 410 |
+
outlines = gr.Image(label=TRANSLATIONS["english"]["outlines"])
|
| 411 |
+
dxf_file = gr.File(label=TRANSLATIONS["english"]["dxf_file"])
|
| 412 |
+
mask = gr.Image(label=TRANSLATIONS["english"]["mask"])
|
| 413 |
+
scaling = gr.Textbox(
|
| 414 |
+
label=TRANSLATIONS["english"]["scaling_factor"],
|
| 415 |
+
placeholder=TRANSLATIONS["english"]["scaling_placeholder"]
|
| 416 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
+
# Create submit button
|
| 419 |
+
submit_btn = gr.Button("Submit")
|
| 420 |
+
|
| 421 |
+
# Handle language change
|
| 422 |
+
language.change(
|
| 423 |
+
fn=lambda x: [
|
| 424 |
+
gr.update(label=TRANSLATIONS[x]["input_image"]),
|
| 425 |
+
gr.update(label=TRANSLATIONS[x]["offset_value"]),
|
| 426 |
+
gr.update(label=TRANSLATIONS[x]["coin_diameter"]),
|
| 427 |
+
gr.update(label=TRANSLATIONS[x]["output_image"]),
|
| 428 |
+
gr.update(label=TRANSLATIONS[x]["outlines"]),
|
| 429 |
+
gr.update(label=TRANSLATIONS[x]["dxf_file"]),
|
| 430 |
+
gr.update(label=TRANSLATIONS[x]["mask"]),
|
| 431 |
+
gr.update(
|
| 432 |
+
label=TRANSLATIONS[x]["scaling_factor"],
|
| 433 |
+
placeholder=TRANSLATIONS[x]["scaling_placeholder"]
|
| 434 |
+
),
|
| 435 |
+
],
|
| 436 |
+
inputs=[language],
|
| 437 |
+
outputs=[
|
| 438 |
+
input_image, offset, coin_size,
|
| 439 |
+
output_image, outlines, dxf_file,
|
| 440 |
+
mask, scaling
|
| 441 |
+
]
|
| 442 |
+
)
|
| 443 |
|
| 444 |
+
# Handle prediction
|
| 445 |
+
submit_btn.click(
|
| 446 |
+
fn=predict,
|
| 447 |
+
inputs=[input_image, offset, coin_size],
|
| 448 |
+
outputs=[output_image, outlines, dxf_file, mask, scaling]
|
| 449 |
+
)
|
| 450 |
|
| 451 |
+
# Add examples
|
| 452 |
+
gr.Examples(
|
| 453 |
+
examples=[
|
| 454 |
+
["./examples/Test20.jpg", 0.15],
|
| 455 |
+
["./examples/Test21.jpg", 0.15],
|
| 456 |
+
["./examples/Test22.jpg", 0.15],
|
| 457 |
+
["./examples/Test23.jpg", 0.15],
|
| 458 |
+
],
|
| 459 |
+
inputs=[input_image, offset]
|
| 460 |
+
)
|
| 461 |
|
| 462 |
+
demo.launch(share=True)
|