Spaces:
Running
Running
Upload with huggingface_hub
Browse files
DESCRIPTION.md
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
Simple image segmentation using gradio's AnnotatedImage component.
|
README.md
CHANGED
@@ -5,7 +5,7 @@ emoji: 🔥
|
|
5 |
colorFrom: indigo
|
6 |
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
-
sdk_version: 3.
|
9 |
app_file: run.py
|
10 |
pinned: false
|
11 |
---
|
|
|
5 |
colorFrom: indigo
|
6 |
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
+
sdk_version: 3.27.0
|
9 |
app_file: run.py
|
10 |
pinned: false
|
11 |
---
|
run.ipynb
CHANGED
@@ -1 +1 @@
|
|
1 |
-
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: image_segmentation\n", "###
|
|
|
1 |
+
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: image_segmentation\n", "### Simple image segmentation using gradio's AnnotatedImage component.\n", " "]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import numpy as np\n", "import random\n", "\n", "with gr.Blocks() as demo:\n", " section_labels = [\n", " \"apple\",\n", " \"banana\",\n", " \"carrot\",\n", " \"donut\",\n", " \"eggplant\",\n", " \"fish\",\n", " \"grapes\",\n", " \"hamburger\",\n", " \"ice cream\",\n", " \"juice\",\n", " ]\n", "\n", " with gr.Row():\n", " num_boxes = gr.Slider(0, 5, 2, step=1, label=\"Number of boxes\")\n", " num_segments = gr.Slider(0, 5, 1, step=1, label=\"Number of segments\")\n", "\n", " with gr.Row():\n", " img_input = gr.Image()\n", " img_output = gr.AnnotatedImage().style(\n", " color_map={\"banana\": \"#a89a00\", \"carrot\": \"#ffae00\"}\n", " )\n", "\n", " section_btn = gr.Button(\"Identify Sections\")\n", " selected_section = gr.Textbox(label=\"Selected Section\")\n", "\n", " def section(img, num_boxes, num_segments):\n", " sections = []\n", " for a in range(num_boxes):\n", " x = random.randint(0, img.shape[1])\n", " y = random.randint(0, img.shape[0])\n", " w = random.randint(0, img.shape[1] - x)\n", " h = random.randint(0, img.shape[0] - y)\n", " sections.append(((x, y, x + w, y + h), section_labels[a]))\n", " for b in range(num_segments):\n", " x = random.randint(0, img.shape[1])\n", " y = random.randint(0, img.shape[0])\n", " r = random.randint(0, min(x, y, img.shape[1] - x, img.shape[0] - y))\n", " mask = np.zeros(img.shape[:2])\n", " for i in range(img.shape[0]):\n", " for j in range(img.shape[1]):\n", " dist_square = (i - y) ** 2 + (j - x) ** 2\n", " if dist_square < r**2:\n", " mask[i, j] = round((r**2 - dist_square) / r**2 * 4) / 4\n", " sections.append((mask, section_labels[b + num_boxes]))\n", " return (img, sections)\n", "\n", " section_btn.click(section, [img_input, num_boxes, num_segments], img_output)\n", "\n", " def select_section(evt: gr.SelectData):\n", " return section_labels[evt.index]\n", "\n", " img_output.select(select_section, None, selected_section)\n", "\n", "\n", "demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
CHANGED
@@ -1,42 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
-
import random
|
4 |
import numpy as np
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
with
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import numpy as np
|
3 |
+
import random
|
4 |
+
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
section_labels = [
|
7 |
+
"apple",
|
8 |
+
"banana",
|
9 |
+
"carrot",
|
10 |
+
"donut",
|
11 |
+
"eggplant",
|
12 |
+
"fish",
|
13 |
+
"grapes",
|
14 |
+
"hamburger",
|
15 |
+
"ice cream",
|
16 |
+
"juice",
|
17 |
+
]
|
18 |
+
|
19 |
+
with gr.Row():
|
20 |
+
num_boxes = gr.Slider(0, 5, 2, step=1, label="Number of boxes")
|
21 |
+
num_segments = gr.Slider(0, 5, 1, step=1, label="Number of segments")
|
22 |
+
|
23 |
+
with gr.Row():
|
24 |
+
img_input = gr.Image()
|
25 |
+
img_output = gr.AnnotatedImage().style(
|
26 |
+
color_map={"banana": "#a89a00", "carrot": "#ffae00"}
|
27 |
+
)
|
28 |
+
|
29 |
+
section_btn = gr.Button("Identify Sections")
|
30 |
+
selected_section = gr.Textbox(label="Selected Section")
|
31 |
+
|
32 |
+
def section(img, num_boxes, num_segments):
|
33 |
+
sections = []
|
34 |
+
for a in range(num_boxes):
|
35 |
+
x = random.randint(0, img.shape[1])
|
36 |
+
y = random.randint(0, img.shape[0])
|
37 |
+
w = random.randint(0, img.shape[1] - x)
|
38 |
+
h = random.randint(0, img.shape[0] - y)
|
39 |
+
sections.append(((x, y, x + w, y + h), section_labels[a]))
|
40 |
+
for b in range(num_segments):
|
41 |
+
x = random.randint(0, img.shape[1])
|
42 |
+
y = random.randint(0, img.shape[0])
|
43 |
+
r = random.randint(0, min(x, y, img.shape[1] - x, img.shape[0] - y))
|
44 |
+
mask = np.zeros(img.shape[:2])
|
45 |
+
for i in range(img.shape[0]):
|
46 |
+
for j in range(img.shape[1]):
|
47 |
+
dist_square = (i - y) ** 2 + (j - x) ** 2
|
48 |
+
if dist_square < r**2:
|
49 |
+
mask[i, j] = round((r**2 - dist_square) / r**2 * 4) / 4
|
50 |
+
sections.append((mask, section_labels[b + num_boxes]))
|
51 |
+
return (img, sections)
|
52 |
+
|
53 |
+
section_btn.click(section, [img_input, num_boxes, num_segments], img_output)
|
54 |
+
|
55 |
+
def select_section(evt: gr.SelectData):
|
56 |
+
return section_labels[evt.index]
|
57 |
+
|
58 |
+
img_output.select(select_section, None, selected_section)
|
59 |
+
|
60 |
+
|
61 |
+
demo.launch()
|