Spaces:
Running
Running
Commit
·
4bfd44d
1
Parent(s):
f61c40f
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from io import BytesIO
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
import json
|
6 |
+
|
7 |
+
from tools.tools import convertToBuffer
|
8 |
+
from visualize.visualize import removeBgFromSegmentImage, removeOnlyBg
|
9 |
+
from models.model import getMask, loadModel
|
10 |
+
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:
|
23 |
+
marker_coordinates = json.loads(marker_coordinates)
|
24 |
+
except json.JSONDecodeError:
|
25 |
+
return "Invalid marker coordinates format. Ensure it's valid JSON."
|
26 |
+
|
27 |
+
try:
|
28 |
+
# Process marker points and labels
|
29 |
+
input_points, input_labels = preprocess(marker_coordinates)
|
30 |
+
|
31 |
+
print(f"Processing image with {len(input_points)} marker points...")
|
32 |
+
# Get mask for segmentation
|
33 |
+
masks = getMask(img_rgb, FAST_SAM, input_points, input_labels)
|
34 |
+
|
35 |
+
# Generate the segmented images
|
36 |
+
bg_removed_segmented_img = removeBgFromSegmentImage(img_rgb, masks[0])
|
37 |
+
img_base64_bg_segmented = convertToBuffer(bg_removed_segmented_img)
|
38 |
+
|
39 |
+
bg_only_removed_img = removeOnlyBg(img_rgb, masks[0])
|
40 |
+
img_base64_only_bg = convertToBuffer(bg_only_removed_img)
|
41 |
+
|
42 |
+
# Convert base64 strings to PIL images for Gradio
|
43 |
+
img_bg_segmented = base64_to_image(img_base64_bg_segmented)
|
44 |
+
img_bg_only_removed = base64_to_image(img_base64_only_bg)
|
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)}")
|
50 |
+
return "An error occurred while processing the image.", None
|
51 |
+
|
52 |
+
|
53 |
+
# Set up the Gradio interface
|
54 |
+
iface = gr.Interface(
|
55 |
+
fn=segment_marker,
|
56 |
+
inputs=[
|
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)
|