Spaces:
Runtime error
Runtime error
Commit
·
425083f
1
Parent(s):
71a7417
Dict return
Browse files
app.py
CHANGED
|
@@ -11,12 +11,15 @@ from models.preprocess import preprocess
|
|
| 11 |
|
| 12 |
FAST_SAM = loadModel()
|
| 13 |
|
|
|
|
| 14 |
def base64_to_image(base64_str):
|
| 15 |
image_data = base64.b64decode(base64_str)
|
| 16 |
image = Image.open(BytesIO(image_data))
|
| 17 |
return image
|
| 18 |
|
| 19 |
# Main processing function
|
|
|
|
|
|
|
| 20 |
def segment_marker(img_rgb: Image.Image, marker_coordinates: str):
|
| 21 |
# Parse marker coordinates from JSON string
|
| 22 |
try:
|
|
@@ -39,11 +42,10 @@ def segment_marker(img_rgb: Image.Image, marker_coordinates: str):
|
|
| 39 |
bg_only_removed_img = removeOnlyBg(img_rgb, masks[0])
|
| 40 |
img_base64_only_bg = convertToBuffer(bg_only_removed_img)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return img_bg_segmented, img_bg_only_removed # Return as two separate images
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
print(f"An error occurred: {str(e)}")
|
|
@@ -57,13 +59,9 @@ iface = gr.Interface(
|
|
| 57 |
gr.Image(type="pil", label="Upload Image"),
|
| 58 |
gr.Textbox(label="Markers Coordinates (JSON format)")
|
| 59 |
],
|
| 60 |
-
outputs=
|
| 61 |
-
gr.Image(type="pil", label="Background Removed with Segmentation"),
|
| 62 |
-
gr.Image(type="pil", label="Only Background Removed")
|
| 63 |
-
],
|
| 64 |
title="Image Segmentation with Background Removal",
|
| 65 |
description="Upload an image and JSON-formatted marker coordinates to perform image segmentation and background removal."
|
| 66 |
)
|
| 67 |
-
|
| 68 |
# Run the Gradio app
|
| 69 |
iface.launch(share=True)
|
|
|
|
| 11 |
|
| 12 |
FAST_SAM = loadModel()
|
| 13 |
|
| 14 |
+
|
| 15 |
def base64_to_image(base64_str):
|
| 16 |
image_data = base64.b64decode(base64_str)
|
| 17 |
image = Image.open(BytesIO(image_data))
|
| 18 |
return image
|
| 19 |
|
| 20 |
# Main processing function
|
| 21 |
+
|
| 22 |
+
|
| 23 |
def segment_marker(img_rgb: Image.Image, marker_coordinates: str):
|
| 24 |
# Parse marker coordinates from JSON string
|
| 25 |
try:
|
|
|
|
| 42 |
bg_only_removed_img = removeOnlyBg(img_rgb, masks[0])
|
| 43 |
img_base64_only_bg = convertToBuffer(bg_only_removed_img)
|
| 44 |
|
| 45 |
+
return {
|
| 46 |
+
'bg_removed_segmented_img': f'data:image/png;base64,{img_base64_bg_segmented}',
|
| 47 |
+
'bg_only_removed_segmented_img': f'data:image/png;base64,{img_base64_only_bg}'
|
| 48 |
+
}
|
|
|
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
print(f"An error occurred: {str(e)}")
|
|
|
|
| 59 |
gr.Image(type="pil", label="Upload Image"),
|
| 60 |
gr.Textbox(label="Markers Coordinates (JSON format)")
|
| 61 |
],
|
| 62 |
+
outputs="json", # Set output to JSON format to return the dictionary
|
|
|
|
|
|
|
|
|
|
| 63 |
title="Image Segmentation with Background Removal",
|
| 64 |
description="Upload an image and JSON-formatted marker coordinates to perform image segmentation and background removal."
|
| 65 |
)
|
|
|
|
| 66 |
# Run the Gradio app
|
| 67 |
iface.launch(share=True)
|