Spaces:
Sleeping
Sleeping
Commit
·
a64bccf
1
Parent(s):
6fa71e4
Add application file
Browse files- app.py +65 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import Owlv2Processor, Owlv2ForObjectDetection
|
4 |
+
import spaces
|
5 |
+
|
6 |
+
# Use GPU if available
|
7 |
+
if torch.cuda.is_available():
|
8 |
+
device = torch.device("cuda")
|
9 |
+
else:
|
10 |
+
device = torch.device("cpu")
|
11 |
+
|
12 |
+
model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16-ensemble").to(device)
|
13 |
+
processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16-ensemble")
|
14 |
+
|
15 |
+
@spaces.GPU
|
16 |
+
def query_image(img, text_queries, score_threshold):
|
17 |
+
text_queries = text_queries
|
18 |
+
text_queries = text_queries.split(",")
|
19 |
+
|
20 |
+
size = max(img.shape[:2])
|
21 |
+
target_sizes = torch.Tensor([[size, size]])
|
22 |
+
inputs = processor(text=text_queries, images=img, return_tensors="pt").to(device)
|
23 |
+
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = model(**inputs)
|
26 |
+
|
27 |
+
outputs.logits = outputs.logits.cpu()
|
28 |
+
outputs.pred_boxes = outputs.pred_boxes.cpu()
|
29 |
+
results = processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes)
|
30 |
+
boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
|
31 |
+
|
32 |
+
result_labels = []
|
33 |
+
for box, score, label in zip(boxes, scores, labels):
|
34 |
+
box = [int(i) for i in box.tolist()]
|
35 |
+
if score < score_threshold:
|
36 |
+
continue
|
37 |
+
result_labels.append((box, text_queries[label.item()]))
|
38 |
+
return img, result_labels
|
39 |
+
|
40 |
+
|
41 |
+
description = """
|
42 |
+
Try this demo for <a href="https://huggingface.co/docs/transformers/main/en/model_doc/owlv2">OWLv2</a>,
|
43 |
+
introduced in <a href="https://arxiv.org/abs/2306.09683">Scaling Open-Vocabulary Object Detection</a>.
|
44 |
+
\n\n Compared to OWLVIT, OWLv2 performs better both in yield and performance (average precision).
|
45 |
+
You can use OWLv2 to query images with text descriptions of any object.
|
46 |
+
To use it, simply upload an image and enter comma separated text descriptions of objects you want to query the image for. You
|
47 |
+
can also use the score threshold slider to set a threshold to filter out low probability predictions.
|
48 |
+
\n\nOWL-ViT is trained on text templates,
|
49 |
+
hence you can get better predictions by querying the image with text templates used in training the original model: e.g. *"photo of a star-spangled banner"*,
|
50 |
+
*"image of a shoe"*. Refer to the <a href="https://arxiv.org/abs/2103.00020">CLIP</a> paper to see the full list of text templates used to augment the training data.
|
51 |
+
\n\n<a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb">Colab demo</a>
|
52 |
+
"""
|
53 |
+
demo = gr.Interface(
|
54 |
+
query_image,
|
55 |
+
inputs=[gr.Image(), "text", gr.Slider(0, 1, value=0.1)],
|
56 |
+
outputs="annotatedimage",
|
57 |
+
title="Zero-Shot Object Detection with OWLv2",
|
58 |
+
description=description,
|
59 |
+
examples=[
|
60 |
+
["assets/astronaut.png", "human face, rocket, star-spangled banner, nasa badge", 0.11],
|
61 |
+
["assets/coffee.png", "coffee mug, spoon, plate", 0.1],
|
62 |
+
["assets/butterflies.jpeg", "orange butterfly", 0.3],
|
63 |
+
],
|
64 |
+
)
|
65 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy>=1.18.5
|
2 |
+
torch>=1.7.0
|
3 |
+
torchvision>=0.8.1
|
4 |
+
git+https://github.com/huggingface/transformers.git
|
5 |
+
scipy
|
6 |
+
spaces
|