Spaces:
Running
Running
Commit
·
42decf6
1
Parent(s):
425083f
App.py upated
Browse files- app.py +5 -8
- gradio.ipynb +21 -39
app.py
CHANGED
@@ -11,12 +11,6 @@ from models.preprocess import preprocess
|
|
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 |
|
@@ -42,6 +36,7 @@ def segment_marker(img_rgb: Image.Image, marker_coordinates: str):
|
|
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}'
|
@@ -49,7 +44,7 @@ def segment_marker(img_rgb: Image.Image, marker_coordinates: str):
|
|
49 |
|
50 |
except Exception as e:
|
51 |
print(f"An error occurred: {str(e)}")
|
52 |
-
return "An error occurred while processing the image."
|
53 |
|
54 |
|
55 |
# Set up the Gradio interface
|
@@ -63,5 +58,7 @@ iface = gr.Interface(
|
|
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 |
-
|
|
|
|
11 |
|
12 |
FAST_SAM = loadModel()
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Main processing function
|
15 |
|
16 |
|
|
|
36 |
bg_only_removed_img = removeOnlyBg(img_rgb, masks[0])
|
37 |
img_base64_only_bg = convertToBuffer(bg_only_removed_img)
|
38 |
|
39 |
+
# Return the images in a dictionary format as base64 strings
|
40 |
return {
|
41 |
'bg_removed_segmented_img': f'data:image/png;base64,{img_base64_bg_segmented}',
|
42 |
'bg_only_removed_segmented_img': f'data:image/png;base64,{img_base64_only_bg}'
|
|
|
44 |
|
45 |
except Exception as e:
|
46 |
print(f"An error occurred: {str(e)}")
|
47 |
+
return {'error': "An error occurred while processing the image."}
|
48 |
|
49 |
|
50 |
# Set up the Gradio interface
|
|
|
58 |
title="Image Segmentation with Background Removal",
|
59 |
description="Upload an image and JSON-formatted marker coordinates to perform image segmentation and background removal."
|
60 |
)
|
61 |
+
|
62 |
# Run the Gradio app
|
63 |
+
if __name__ == "__main__":
|
64 |
+
iface.launch(share=True)
|
gradio.ipynb
CHANGED
@@ -122,54 +122,35 @@
|
|
122 |
"execution_count": null,
|
123 |
"metadata": {},
|
124 |
"outputs": [
|
125 |
-
{
|
126 |
-
"name": "stdout",
|
127 |
-
"output_type": "stream",
|
128 |
-
"text": [
|
129 |
-
"Processing image with 1 marker points...\n"
|
130 |
-
]
|
131 |
-
},
|
132 |
{
|
133 |
"name": "stderr",
|
134 |
"output_type": "stream",
|
135 |
"text": [
|
136 |
-
"
|
137 |
-
"
|
138 |
-
"Speed: 1549.8ms preprocess, 5068.7ms inference, 5802.7ms postprocess per image at shape (1, 3, 1024, 1024)\n"
|
139 |
]
|
140 |
},
|
141 |
{
|
142 |
"name": "stdout",
|
143 |
"output_type": "stream",
|
144 |
"text": [
|
145 |
-
"
|
146 |
-
|
147 |
-
},
|
148 |
-
{
|
149 |
-
"name": "stderr",
|
150 |
-
"output_type": "stream",
|
151 |
-
"text": [
|
152 |
"\n",
|
153 |
-
"
|
154 |
-
"Speed: 541.0ms preprocess, 4238.3ms inference, 3713.8ms postprocess per image at shape (1, 3, 1024, 1024)\n"
|
155 |
]
|
156 |
},
|
157 |
{
|
158 |
-
"
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
"
|
167 |
-
"
|
168 |
-
"\n",
|
169 |
-
"0: 736x1024 17 objects, 3183.0ms\n",
|
170 |
-
"Speed: 475.6ms preprocess, 3183.0ms inference, 2650.1ms postprocess per image at shape (1, 3, 1024, 1024)\n",
|
171 |
-
"\n"
|
172 |
-
]
|
173 |
},
|
174 |
{
|
175 |
"name": "stdout",
|
@@ -182,9 +163,9 @@
|
|
182 |
"name": "stderr",
|
183 |
"output_type": "stream",
|
184 |
"text": [
|
185 |
-
"
|
186 |
-
"
|
187 |
-
"\n"
|
188 |
]
|
189 |
},
|
190 |
{
|
@@ -198,8 +179,9 @@
|
|
198 |
"name": "stderr",
|
199 |
"output_type": "stream",
|
200 |
"text": [
|
201 |
-
"
|
202 |
-
"
|
|
|
203 |
]
|
204 |
}
|
205 |
],
|
|
|
122 |
"execution_count": null,
|
123 |
"metadata": {},
|
124 |
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
{
|
126 |
"name": "stderr",
|
127 |
"output_type": "stream",
|
128 |
"text": [
|
129 |
+
"e:\\anaconda\\envs\\tbi-gradio-env\\Lib\\site-packages\\ultralytics\\nn\\tasks.py:377: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
|
130 |
+
" return torch.load(file, map_location='cpu'), file # load\n"
|
|
|
131 |
]
|
132 |
},
|
133 |
{
|
134 |
"name": "stdout",
|
135 |
"output_type": "stream",
|
136 |
"text": [
|
137 |
+
"* Running on local URL: http://127.0.0.1:7861\n",
|
138 |
+
"* Running on public URL: https://ef7a8cd179699f0a20.gradio.live\n",
|
|
|
|
|
|
|
|
|
|
|
139 |
"\n",
|
140 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
|
|
|
141 |
]
|
142 |
},
|
143 |
{
|
144 |
+
"data": {
|
145 |
+
"text/html": [
|
146 |
+
"<div><iframe src=\"https://ef7a8cd179699f0a20.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
147 |
+
],
|
148 |
+
"text/plain": [
|
149 |
+
"<IPython.core.display.HTML object>"
|
150 |
+
]
|
151 |
+
},
|
152 |
+
"metadata": {},
|
153 |
+
"output_type": "display_data"
|
|
|
|
|
|
|
|
|
|
|
154 |
},
|
155 |
{
|
156 |
"name": "stdout",
|
|
|
163 |
"name": "stderr",
|
164 |
"output_type": "stream",
|
165 |
"text": [
|
166 |
+
"\n",
|
167 |
+
"0: 736x1024 17 objects, 3929.7ms\n",
|
168 |
+
"Speed: 8.6ms preprocess, 3929.7ms inference, 4360.5ms postprocess per image at shape (1, 3, 1024, 1024)\n"
|
169 |
]
|
170 |
},
|
171 |
{
|
|
|
179 |
"name": "stderr",
|
180 |
"output_type": "stream",
|
181 |
"text": [
|
182 |
+
"\n",
|
183 |
+
"0: 736x1024 17 objects, 3214.6ms\n",
|
184 |
+
"Speed: 233.8ms preprocess, 3214.6ms inference, 1889.9ms postprocess per image at shape (1, 3, 1024, 1024)\n"
|
185 |
]
|
186 |
}
|
187 |
],
|