Spaces:
Runtime error
Runtime error
Commit
·
1bcb7e6
1
Parent(s):
f1725e1
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,79 @@
|
|
1 |
-
|
2 |
import cv2
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
from transformers import OwlViTProcessor, OwlViTForObjectDetection
|
|
|
6 |
|
7 |
-
|
8 |
-
# Use GPU if available
|
9 |
if torch.cuda.is_available():
|
10 |
device = torch.device("cuda")
|
11 |
else:
|
12 |
device = torch.device("cpu")
|
13 |
|
|
|
14 |
model = OwlViTForObjectDetection.from_pretrained("google/owlvit-large-patch14").to(device)
|
15 |
model.eval()
|
|
|
|
|
16 |
processor = OwlViTProcessor.from_pretrained("google/owlvit-large-patch14")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
text_queries = text_queries
|
21 |
-
text_queries = text_queries.split(",")
|
22 |
|
23 |
target_sizes = torch.Tensor([img.shape[:2]])
|
24 |
-
inputs = processor(text=text_queries, images=img, return_tensors="pt").to(device)
|
25 |
|
26 |
with torch.no_grad():
|
27 |
-
outputs = model(**inputs)
|
28 |
|
|
|
29 |
outputs.logits = outputs.logits.cpu()
|
30 |
-
outputs.pred_boxes = outputs.pred_boxes.cpu()
|
|
|
|
|
31 |
results = processor.post_process(outputs=outputs, target_sizes=target_sizes)
|
32 |
boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
|
33 |
|
34 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
35 |
|
|
|
36 |
for box, score, label in zip(boxes, scores, labels):
|
37 |
box = [int(i) for i in box.tolist()]
|
38 |
|
39 |
if score >= score_threshold:
|
40 |
img = cv2.rectangle(img, box[:2], box[2:], (255,0,0), 5)
|
41 |
-
if box[3] + 25 > 768
|
42 |
-
y = box[3] - 10
|
43 |
-
else:
|
44 |
-
y = box[3] + 25
|
45 |
|
46 |
img = cv2.putText(
|
47 |
img, text_queries[label], (box[0], y), font, 1, (255,0,0), 2, cv2.LINE_AA
|
48 |
)
|
49 |
return img
|
50 |
|
51 |
-
|
52 |
description = """
|
53 |
-
Gradio demo for
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
To use it, simply upload an image and enter comma separated text descriptions of objects you want to query the image for. You
|
58 |
-
can also use the score threshold slider to set a threshold to filter out low probability predictions.
|
59 |
-
|
60 |
-
\n\nOWL-ViT is trained on text templates,
|
61 |
-
hence you can get better predictions by querying the image with text templates used in training the original model: *"photo of a star-spangled banner"*,
|
62 |
-
*"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.
|
63 |
-
\n\n<a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb">Colab demo</a>
|
64 |
"""
|
|
|
|
|
65 |
demo = gr.Interface(
|
66 |
query_image,
|
67 |
-
inputs=[
|
68 |
outputs="image",
|
69 |
title="Zero-Shot Object Detection with OWL-ViT",
|
70 |
description=description,
|
71 |
examples=[
|
72 |
-
["
|
73 |
-
["
|
74 |
-
["assets/butterflies.jpeg", "orange butterfly", 0.3],
|
75 |
],
|
76 |
)
|
77 |
demo.launch()
|
|
|
1 |
+
iimport torch
|
2 |
import cv2
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
from transformers import OwlViTProcessor, OwlViTForObjectDetection
|
6 |
+
import requests
|
7 |
|
8 |
+
# 如果GPU可用,就使用GPU,否则使用CPU
|
|
|
9 |
if torch.cuda.is_available():
|
10 |
device = torch.device("cuda")
|
11 |
else:
|
12 |
device = torch.device("cpu")
|
13 |
|
14 |
+
# 从预训练模型"google/owlvit-large-patch14"加载OWL-ViT模型,并将其放置到适当的设备上
|
15 |
model = OwlViTForObjectDetection.from_pretrained("google/owlvit-large-patch14").to(device)
|
16 |
model.eval()
|
17 |
+
|
18 |
+
# 从同一预训练模型中加载处理器
|
19 |
processor = OwlViTProcessor.from_pretrained("google/owlvit-large-patch14")
|
20 |
|
21 |
+
# 定义一个函数来处理图像URL,文本查询和分数阈值
|
22 |
+
def query_image(img_url, text_queries, score_threshold):
|
23 |
+
# 使用requests库从URL中获取图像
|
24 |
+
response = requests.get(img_url)
|
25 |
+
response.raise_for_status()
|
26 |
+
arr = np.asarray(bytearray(response.content), dtype=np.uint8)
|
27 |
+
img = cv2.imdecode(arr, -1) # 使用-1来加载原始图像
|
28 |
|
29 |
+
text_queries = text_queries.split(",") # 将文本查询分割成独立的查询
|
|
|
|
|
30 |
|
31 |
target_sizes = torch.Tensor([img.shape[:2]])
|
32 |
+
inputs = processor(text=text_queries, images=img, return_tensors="pt").to(device) # 使用处理器创建模型的输入
|
33 |
|
34 |
with torch.no_grad():
|
35 |
+
outputs = model(**inputs) # 获取模型的输出
|
36 |
|
37 |
+
# 将输出转移到CPU上
|
38 |
outputs.logits = outputs.logits.cpu()
|
39 |
+
outputs.pred_boxes = outputs.pred_boxes.cpu()
|
40 |
+
|
41 |
+
# 使用处理器进行后处理
|
42 |
results = processor.post_process(outputs=outputs, target_sizes=target_sizes)
|
43 |
boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
|
44 |
|
45 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
46 |
|
47 |
+
# 在图像上绘制边界框并添加标签
|
48 |
for box, score, label in zip(boxes, scores, labels):
|
49 |
box = [int(i) for i in box.tolist()]
|
50 |
|
51 |
if score >= score_threshold:
|
52 |
img = cv2.rectangle(img, box[:2], box[2:], (255,0,0), 5)
|
53 |
+
y = box[3] - 10 if box[3] + 25 > 768 else box[3] + 25
|
|
|
|
|
|
|
54 |
|
55 |
img = cv2.putText(
|
56 |
img, text_queries[label], (box[0], y), font, 1, (255,0,0), 2, cv2.LINE_AA
|
57 |
)
|
58 |
return img
|
59 |
|
|
|
60 |
description = """
|
61 |
+
Gradio demo for OWL-ViT.
|
62 |
+
You can use OWL-ViT to query images with text descriptions of any object.
|
63 |
+
To use it, simply provide an image URL and enter comma separated text descriptions of objects you want to query the image for.
|
64 |
+
You can also use the score threshold slider to set a threshold to filter out low probability predictions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
"""
|
66 |
+
|
67 |
+
# 创建一个Gradio界面
|
68 |
demo = gr.Interface(
|
69 |
query_image,
|
70 |
+
inputs=["text", "text", gr.Slider(0, 1, value=0.1)], # 修改输入,使其接受URL而不是图像
|
71 |
outputs="image",
|
72 |
title="Zero-Shot Object Detection with OWL-ViT",
|
73 |
description=description,
|
74 |
examples=[
|
75 |
+
["https://example.com/path/to/image.png", "human face, rocket, star-spangled banner, nasa badge", 0.11],
|
76 |
+
["https://example.com/path/to/another/image.png", "coffee mug, spoon, plate", 0.1],
|
|
|
77 |
],
|
78 |
)
|
79 |
demo.launch()
|