Spaces:
Runtime error
Runtime error
poc
Browse files
app.py
CHANGED
|
@@ -13,6 +13,11 @@ This is the demo for a Open Vocabulary Image Segmentation using
|
|
| 13 |
[Segment Anything Model](https://github.com/facebookresearch/segment-anything) and
|
| 14 |
[MetaCLIP](https://github.com/facebookresearch/MetaCLIP) combo.
|
| 15 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
SAM_GENERATOR = pipeline(
|
|
@@ -78,32 +83,44 @@ def filter_detections(
|
|
| 78 |
return detections[filtering_mask]
|
| 79 |
|
| 80 |
|
| 81 |
-
def inference(image_rgb_pil: Image.Image, prompt: str) -> Image.Image:
|
| 82 |
width, height = image_rgb_pil.size
|
| 83 |
area = width * height
|
| 84 |
|
| 85 |
detections = run_sam(image_rgb_pil)
|
| 86 |
-
detections = detections[detections.area / area > 0.
|
| 87 |
detections = filter_detections(
|
| 88 |
image_rgb_pil=image_rgb_pil,
|
| 89 |
detections=detections,
|
| 90 |
prompt=prompt)
|
| 91 |
|
| 92 |
-
return
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
gr.Markdown(MARKDOWN)
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
| 99 |
-
input_image = gr.Image(image_mode='RGB', type='pil')
|
| 100 |
prompt_text = gr.Textbox(label="Prompt", value="dog")
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
submit_button.click(
|
| 105 |
inference,
|
| 106 |
inputs=[input_image, prompt_text],
|
| 107 |
-
outputs=
|
| 108 |
|
| 109 |
-
demo.launch(debug=False)
|
|
|
|
| 13 |
[Segment Anything Model](https://github.com/facebookresearch/segment-anything) and
|
| 14 |
[MetaCLIP](https://github.com/facebookresearch/MetaCLIP) combo.
|
| 15 |
"""
|
| 16 |
+
EXAMPLES = [
|
| 17 |
+
["https://media.roboflow.com/notebooks/examples/dog.jpeg", "dog"],
|
| 18 |
+
["https://media.roboflow.com/notebooks/examples/dog.jpeg", "building"],
|
| 19 |
+
["https://media.roboflow.com/notebooks/examples/dog-3.jpeg", "jacket"],
|
| 20 |
+
]
|
| 21 |
|
| 22 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
SAM_GENERATOR = pipeline(
|
|
|
|
| 83 |
return detections[filtering_mask]
|
| 84 |
|
| 85 |
|
| 86 |
+
def inference(image_rgb_pil: Image.Image, prompt: str) -> List[Image.Image]:
|
| 87 |
width, height = image_rgb_pil.size
|
| 88 |
area = width * height
|
| 89 |
|
| 90 |
detections = run_sam(image_rgb_pil)
|
| 91 |
+
detections = detections[detections.area / area > 0.01]
|
| 92 |
detections = filter_detections(
|
| 93 |
image_rgb_pil=image_rgb_pil,
|
| 94 |
detections=detections,
|
| 95 |
prompt=prompt)
|
| 96 |
|
| 97 |
+
return [
|
| 98 |
+
annotate(image_rgb_pil=image_rgb_pil, detections=detections),
|
| 99 |
+
annotate(image_rgb_pil=Image.new("RGB", (width, height), "black"), detections=detections)
|
| 100 |
+
]
|
| 101 |
|
| 102 |
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
gr.Markdown(MARKDOWN)
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column():
|
| 107 |
+
input_image = gr.Image(image_mode='RGB', type='pil', height=500)
|
| 108 |
prompt_text = gr.Textbox(label="Prompt", value="dog")
|
| 109 |
+
submit_button = gr.Button("Submit")
|
| 110 |
+
gallery = gr.Gallery(label="Result", object_fit="scale-down", preview=True)
|
| 111 |
+
with gr.Row():
|
| 112 |
+
gr.Examples(
|
| 113 |
+
examples=EXAMPLES,
|
| 114 |
+
fn=inference,
|
| 115 |
+
inputs=[input_image, prompt_text],
|
| 116 |
+
outputs=[gallery],
|
| 117 |
+
cache_examples=True,
|
| 118 |
+
run_on_click=True
|
| 119 |
+
)
|
| 120 |
|
| 121 |
submit_button.click(
|
| 122 |
inference,
|
| 123 |
inputs=[input_image, prompt_text],
|
| 124 |
+
outputs=gallery)
|
| 125 |
|
| 126 |
+
demo.launch(debug=False, show_error=True)
|