Spaces:
Running
on
L4
Running
on
L4
Commit
·
a09a58c
1
Parent(s):
ca815e1
Update
Browse files- app.py +30 -78
- images/image_0.png +2 -2
- images/image_1.png +2 -2
- images/image_2.png +2 -2
- images/image_3.png +2 -2
- images/image_4.png +0 -3
- images/image_5.png +2 -2
- images/image_6.png +2 -2
- images/original_image_0.png +0 -3
- images/original_image_1.png +0 -3
- images/original_image_2.png +0 -3
- images/original_image_3.png +0 -3
- images/original_image_4.png +0 -3
- images/original_image_5.png +0 -3
- images/original_image_6.png +0 -3
- requirements.txt +3 -1
app.py
CHANGED
@@ -9,8 +9,9 @@ import os
|
|
9 |
import pandas as pd
|
10 |
from utils import *
|
11 |
from PIL import Image
|
|
|
|
|
12 |
|
13 |
-
# Carga de modelos
|
14 |
sam_hq_model = SamHQModel.from_pretrained("syscv-community/sam-hq-vit-huge")
|
15 |
sam_hq_processor = SamHQProcessor.from_pretrained("syscv-community/sam-hq-vit-huge")
|
16 |
|
@@ -31,23 +32,26 @@ def predict_masks_and_scores(model, processor, raw_image, input_points=None, inp
|
|
31 |
scores = outputs.iou_scores
|
32 |
return masks, scores
|
33 |
|
34 |
-
def
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
for
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
|
52 |
sam_masks, sam_scores = predict_masks_and_scores(sam_model, sam_processor, user_image, input_boxes=input_boxes, input_points=input_points)
|
53 |
sam_hq_masks, sam_hq_scores = predict_masks_and_scores(sam_hq_model, sam_hq_processor, user_image, input_boxes=input_boxes, input_points=input_points)
|
@@ -89,22 +93,7 @@ def compare_images_points_and_masks(user_image, input_boxes, input_points):
|
|
89 |
"""
|
90 |
return html_code
|
91 |
|
92 |
-
|
93 |
-
with open(json_file, "r") as f:
|
94 |
-
examples = json.load(f)
|
95 |
-
return examples
|
96 |
-
|
97 |
-
examples = load_examples()
|
98 |
-
example_paths = [example["image_path"] for example in examples]
|
99 |
-
example_data_map = {
|
100 |
-
example["image_path"]: {
|
101 |
-
"original_image_path": example["original_image_path"],
|
102 |
-
"points": example["points"],
|
103 |
-
"boxes": example["boxes"],
|
104 |
-
"size": example["size"]
|
105 |
-
}
|
106 |
-
for example in examples
|
107 |
-
}
|
108 |
|
109 |
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="emerald")
|
110 |
with gr.Blocks(theme=theme, title="🔍 Compare SAM vs SAM-HQ") as demo:
|
@@ -113,53 +102,15 @@ with gr.Blocks(theme=theme, title="🔍 Compare SAM vs SAM-HQ") as demo:
|
|
113 |
gr.Markdown("Compare the performance of SAM and SAM-HQ on various images. Click on an example to load it")
|
114 |
gr.Markdown("[SAM-HQ](https://huggingface.co/syscv-community/sam-hq-vit-huge) - [SAM](https://huggingface.co/facebook/sam-vit-huge)")
|
115 |
|
116 |
-
|
117 |
-
image_input = gr.Image(
|
118 |
-
type="pil",
|
119 |
-
label="Example image (click below to load)",
|
120 |
-
interactive=False,
|
121 |
-
height=500,
|
122 |
-
show_label=True
|
123 |
-
)
|
124 |
-
|
125 |
-
gr.Examples(
|
126 |
-
examples=example_paths,
|
127 |
-
inputs=[image_input],
|
128 |
-
label="Click an example to try 👇",
|
129 |
-
)
|
130 |
-
|
131 |
result_html = gr.HTML(elem_id="result-html")
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
datatype=["number", "number"],
|
138 |
-
col_count=(2, "fixed")
|
139 |
-
)
|
140 |
-
boxes_input = gr.Dataframe(
|
141 |
-
headers=["x0", "y0", "x1", "y1"],
|
142 |
-
label="Boxes",
|
143 |
-
datatype=["number", "number", "number", "number"],
|
144 |
-
col_count=(4, "fixed")
|
145 |
-
)
|
146 |
-
|
147 |
-
def on_image_change(image):
|
148 |
-
for example_path, example_data in example_data_map.items():
|
149 |
-
print(image.size)
|
150 |
-
if example_data["size"] == list(image.size):
|
151 |
-
return example_data["points"], example_data["boxes"]
|
152 |
-
return [], []
|
153 |
-
|
154 |
-
image_input.change(
|
155 |
-
fn=on_image_change,
|
156 |
-
inputs=[image_input],
|
157 |
-
outputs=[points_input, boxes_input]
|
158 |
)
|
159 |
|
160 |
-
compare_button = gr.Button("Compare points and masks")
|
161 |
-
compare_button.click(fn=compare_images_points_and_masks, inputs=[image_input, boxes_input, points_input], outputs=result_html)
|
162 |
-
|
163 |
gr.HTML("""
|
164 |
<style>
|
165 |
#result-html {
|
@@ -174,4 +125,5 @@ with gr.Blocks(theme=theme, title="🔍 Compare SAM vs SAM-HQ") as demo:
|
|
174 |
</style>
|
175 |
""")
|
176 |
|
|
|
177 |
demo.launch()
|
|
|
9 |
import pandas as pd
|
10 |
from utils import *
|
11 |
from PIL import Image
|
12 |
+
from gradio_image_prompter import ImagePrompter
|
13 |
+
|
14 |
|
|
|
15 |
sam_hq_model = SamHQModel.from_pretrained("syscv-community/sam-hq-vit-huge")
|
16 |
sam_hq_processor = SamHQProcessor.from_pretrained("syscv-community/sam-hq-vit-huge")
|
17 |
|
|
|
32 |
scores = outputs.iou_scores
|
33 |
return masks, scores
|
34 |
|
35 |
+
def process_inputs(prompts):
|
36 |
+
raw_entries = prompts["points"]
|
37 |
+
|
38 |
+
input_points = []
|
39 |
+
input_boxes = []
|
40 |
+
|
41 |
+
for entry in raw_entries:
|
42 |
+
x1, y1, type_, x2, y2, cls = entry
|
43 |
+
if type_ == 1:
|
44 |
+
input_points.append([int(x1), int(y1)])
|
45 |
+
elif type_ == 2:
|
46 |
+
x_min = int(min(x1, x2))
|
47 |
+
y_min = int(min(y1, y2))
|
48 |
+
x_max = int(max(x1, x2))
|
49 |
+
y_max = int(max(y1, y2))
|
50 |
+
input_boxes.append([x_min, y_min, x_max, y_max])
|
51 |
+
|
52 |
+
input_boxes = [input_boxes] if input_boxes else None
|
53 |
+
input_points = [input_points] if input_points else None
|
54 |
+
user_image = prompts['image']
|
55 |
|
56 |
sam_masks, sam_scores = predict_masks_and_scores(sam_model, sam_processor, user_image, input_boxes=input_boxes, input_points=input_points)
|
57 |
sam_hq_masks, sam_hq_scores = predict_masks_and_scores(sam_hq_model, sam_hq_processor, user_image, input_boxes=input_boxes, input_points=input_points)
|
|
|
93 |
"""
|
94 |
return html_code
|
95 |
|
96 |
+
example_paths = [{"image": 'images/' + path} for path in os.listdir('images')]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="emerald")
|
99 |
with gr.Blocks(theme=theme, title="🔍 Compare SAM vs SAM-HQ") as demo:
|
|
|
102 |
gr.Markdown("Compare the performance of SAM and SAM-HQ on various images. Click on an example to load it")
|
103 |
gr.Markdown("[SAM-HQ](https://huggingface.co/syscv-community/sam-hq-vit-huge) - [SAM](https://huggingface.co/facebook/sam-vit-huge)")
|
104 |
|
105 |
+
print('example_paths', example_paths)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
result_html = gr.HTML(elem_id="result-html")
|
107 |
+
gr.Interface(
|
108 |
+
fn=process_inputs,
|
109 |
+
#examples=example_paths,
|
110 |
+
inputs=ImagePrompter(show_label=False),
|
111 |
+
outputs=result_html,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
)
|
113 |
|
|
|
|
|
|
|
114 |
gr.HTML("""
|
115 |
<style>
|
116 |
#result-html {
|
|
|
125 |
</style>
|
126 |
""")
|
127 |
|
128 |
+
|
129 |
demo.launch()
|
images/image_0.png
CHANGED
![]() |
Git LFS Details
|
![]() |
Git LFS Details
|
images/image_1.png
CHANGED
![]() |
Git LFS Details
|
![]() |
Git LFS Details
|
images/image_2.png
CHANGED
![]() |
Git LFS Details
|
![]() |
Git LFS Details
|
images/image_3.png
CHANGED
![]() |
Git LFS Details
|
![]() |
Git LFS Details
|
images/image_4.png
DELETED
Git LFS Details
|
images/image_5.png
CHANGED
![]() |
Git LFS Details
|
![]() |
Git LFS Details
|
images/image_6.png
CHANGED
![]() |
Git LFS Details
|
![]() |
Git LFS Details
|
images/original_image_0.png
DELETED
Git LFS Details
|
images/original_image_1.png
DELETED
Git LFS Details
|
images/original_image_2.png
DELETED
Git LFS Details
|
images/original_image_3.png
DELETED
Git LFS Details
|
images/original_image_4.png
DELETED
Git LFS Details
|
images/original_image_5.png
DELETED
Git LFS Details
|
images/original_image_6.png
DELETED
Git LFS Details
|
requirements.txt
CHANGED
@@ -5,4 +5,6 @@ pillow
|
|
5 |
torch
|
6 |
git+https://github.com/huggingface/transformers.git
|
7 |
matplotlib
|
8 |
-
numpy
|
|
|
|
|
|
5 |
torch
|
6 |
git+https://github.com/huggingface/transformers.git
|
7 |
matplotlib
|
8 |
+
numpy
|
9 |
+
gradio-image-prompter
|
10 |
+
pydantic==2.10.6
|