JayKen commited on
Commit
bb00c3e
·
1 Parent(s): b1e7a95

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
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-base-patch32").to(device)
15
+ model.eval()
16
+ processor = OwlViTProcessor.from_pretrained("google/owlvit-base-patch32")
17
+
18
+
19
+ def query_image(img, text_queries, score_threshold):
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
+
53
+ demo = gr.Interface(
54
+ query_image,
55
+ inputs=[gr.Image(), "text", gr.Slider(0, 1, value=0.1)],
56
+ outputs="image",
57
+ title="Object detection",
58
+ )
59
+ demo.launch()