Eduardo Pacheco
commited on
Commit
·
2b1bf26
1
Parent(s):
69f8e74
First commit
Browse files- .gitattributes +1 -0
- .gitignore +2 -0
- app.py +74 -0
- input_image.jpeg +0 -0
- requirements.txt +2 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
input_image.jpeg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio_cached_examples
|
| 2 |
+
__pycache__
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import GroundingDinoForObjectDetection, AutoProcessor
|
| 5 |
+
|
| 6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
+
model = GroundingDinoForObjectDetection.from_pretrained('IDEA-Research/grounding-dino-tiny')
|
| 8 |
+
processor = AutoProcessor.from_pretrained("IDEA-Research/grounding-dino-tiny")
|
| 9 |
+
model.to(device);
|
| 10 |
+
|
| 11 |
+
def app_fn(
|
| 12 |
+
image: gr.Image,
|
| 13 |
+
labels: str,
|
| 14 |
+
box_threshold: float,
|
| 15 |
+
text_threhsold: float,
|
| 16 |
+
) -> str:
|
| 17 |
+
labels = labels.split("\n")
|
| 18 |
+
labels = [label if label.endswith(".") else label + "." for label in labels]
|
| 19 |
+
labels = " ".join(labels)
|
| 20 |
+
inputs = processor(images=image, text=labels, return_tensors="pt").to(device)
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
outputs = model(**inputs)
|
| 23 |
+
|
| 24 |
+
result = processor.post_process_grounded_object_detection(
|
| 25 |
+
outputs,
|
| 26 |
+
inputs.input_ids,
|
| 27 |
+
box_threshold=box_threshold,
|
| 28 |
+
text_threshold=text_threhsold,
|
| 29 |
+
target_sizes=[image.size[::-1]]
|
| 30 |
+
)[0]
|
| 31 |
+
|
| 32 |
+
# convert tensor of [x0,y0,x1,y1] to list of [x0,y0,x1,y1] (int)
|
| 33 |
+
boxes = result["boxes"].int().cpu().tolist()
|
| 34 |
+
pred_labels = result["labels"]
|
| 35 |
+
annot = [(tuple(box), pred_label) for box, pred_label in zip(boxes, pred_labels)]
|
| 36 |
+
|
| 37 |
+
return (image, annot)
|
| 38 |
+
|
| 39 |
+
if __name__=="__main__":
|
| 40 |
+
title = "Grounding DINO 🦖 for Object Detection"
|
| 41 |
+
with gr.Blocks(title=title) as demo:
|
| 42 |
+
gr.Markdown(f"# {title}")
|
| 43 |
+
gr.Markdown(
|
| 44 |
+
"""
|
| 45 |
+
This app demonstrates the use of the Grounding DINO model for object detection using the Hugging Face Transformers library.
|
| 46 |
+
Grounding DINO is known for its strong ability of zero-shot object detection, thus it can detect objects in images based on textual descriptions.
|
| 47 |
+
You can try the model by uploading an image and providing a textual description of the objects you want to detect or by splitting
|
| 48 |
+
the description in different lines (this is how you can pass multiple labels). The model will then highlight the detected objects in the image 🤗
|
| 49 |
+
"""
|
| 50 |
+
)
|
| 51 |
+
with gr.Row():
|
| 52 |
+
box_threshold = gr.Slider(minimum=0, maximum=1, value=0.3, step=0.05, label="Box Threshold")
|
| 53 |
+
text_threshold = gr.Slider(minimum=0, maximum=1, value=0.3, step=0.05, label="Text Threshold")
|
| 54 |
+
labels = gr.Textbox(lines=2, max_lines=5, label="Labels")
|
| 55 |
+
btn = gr.Button()
|
| 56 |
+
with gr.Row():
|
| 57 |
+
img = gr.Image(type="pil")
|
| 58 |
+
annotated_image = gr.AnnotatedImage()
|
| 59 |
+
|
| 60 |
+
btn.click(fn=app_fn, inputs=[img, labels, box_threshold, text_threshold], outputs=[annotated_image])
|
| 61 |
+
|
| 62 |
+
gr.Examples(
|
| 63 |
+
[
|
| 64 |
+
["input_image.jpeg", "a person.\na mountain.", 0.25, 0.25],
|
| 65 |
+
["input_image.jpeg", "a group of peolple running to the sea with mountains on the background.", 0.25, 0.25]
|
| 66 |
+
],
|
| 67 |
+
inputs = [img, labels, box_threshold, text_threshold],
|
| 68 |
+
outputs = [annotated_image],
|
| 69 |
+
fn=app_fn,
|
| 70 |
+
cache_examples=True,
|
| 71 |
+
label='Try this example input!'
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
demo.launch()
|
input_image.jpeg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers.git@main#egg=transformers
|
| 2 |
+
torch
|