Spaces:
Runtime error
Runtime error
add app
Browse files- app.py +53 -0
- astronaut.png +0 -0
- coffee.png +0 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
+
from transformers import OwlViTProcessor, OwlViTForObjectDetection
|
| 6 |
+
|
| 7 |
+
model = OwlViTForObjectDetection.from_pretrained("google/owlvit-base-patch32").eval()
|
| 8 |
+
processor = OwlViTProcessor.from_pretrained("google/owlvit-base-patch32")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def query_image(img, text_queries):
|
| 12 |
+
text_queries = text_queries.split(",")
|
| 13 |
+
inputs = processor(text=text_queries, images=img, return_tensors="pt")
|
| 14 |
+
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
|
| 18 |
+
target_sizes = torch.Tensor([[768, 768]])
|
| 19 |
+
results = processor.post_process(outputs=outputs, target_sizes=target_sizes)
|
| 20 |
+
boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
|
| 21 |
+
|
| 22 |
+
draw = ImageDraw.Draw(img)
|
| 23 |
+
font = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", size=22)
|
| 24 |
+
|
| 25 |
+
score_threshold = 0.1
|
| 26 |
+
for box, score, label in zip(boxes, scores, labels):
|
| 27 |
+
box = [int(i) for i in box.tolist()]
|
| 28 |
+
|
| 29 |
+
if score >= score_threshold:
|
| 30 |
+
draw.rectangle(box, outline="red", width=4)
|
| 31 |
+
text_loc =[box[0]+5, box[3]+10]
|
| 32 |
+
draw.text(text_loc, text_queries[label], fill="red", font=font, stroke_width=1)
|
| 33 |
+
|
| 34 |
+
img = np.array(img)
|
| 35 |
+
return img
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
description = description = """
|
| 39 |
+
Gradio demo for <a href="https://huggingface.co/docs/transformers/main/en/model_doc/owlvit">OWL-ViT</a>,
|
| 40 |
+
introduced in <a href="https://arxiv.org/abs/2205.06230">Simple Open-Vocabulary Object Detection
|
| 41 |
+
with Vision Transformers</a>.
|
| 42 |
+
\n\nYou can use OWL-ViT to query images with text descriptions of any object.
|
| 43 |
+
To use it, simply upload an image and enter comma separated text descriptions of objects you want to query the image for.
|
| 44 |
+
"""
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
query_image,
|
| 47 |
+
inputs=[gr.Image(shape=(768, 768), type="pil"), "text"],
|
| 48 |
+
outputs="image",
|
| 49 |
+
title="Zero-Shot Object Detection with OWL-ViT",
|
| 50 |
+
description="You can use OWL-ViT to query images with text descriptions of any object",
|
| 51 |
+
examples=[["astronaut.png", "human face, rocket, flag, nasa badge"], ["coffee.png", "coffee mug, spoon, plate"]]
|
| 52 |
+
)
|
| 53 |
+
demo.launch(debug=True)
|
astronaut.png
ADDED
|
coffee.png
ADDED
|