Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Load the OWLv2 model and processor
|
13 |
+
model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16-ensemble").to(device)
|
14 |
+
processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16-ensemble")
|
15 |
+
|
16 |
+
# Define default text queries relevant to home interior & renovation defects.
|
17 |
+
default_queries = (
|
18 |
+
"pipe defect, rust on pipe, cracked pipe, plastic pipe defect, metal pipe defect, "
|
19 |
+
"water damage on wall, mold on wall, broken sink, damaged cabinet, faulty door"
|
20 |
+
)
|
21 |
+
|
22 |
+
@spaces.GPU
|
23 |
+
def query_image(img, text_queries, score_threshold):
|
24 |
+
# Use default queries if none provided
|
25 |
+
if not text_queries.strip():
|
26 |
+
text_queries = default_queries
|
27 |
+
# Split and clean text queries into a list
|
28 |
+
queries = [q.strip() for q in text_queries.split(",") if q.strip()]
|
29 |
+
|
30 |
+
# Determine target size based on the image dimensions
|
31 |
+
size = max(img.shape[:2])
|
32 |
+
target_sizes = torch.Tensor([[size, size]])
|
33 |
+
|
34 |
+
# Process inputs
|
35 |
+
inputs = processor(text=queries, images=img, return_tensors="pt").to(device)
|
36 |
+
|
37 |
+
with torch.no_grad():
|
38 |
+
outputs = model(**inputs)
|
39 |
+
|
40 |
+
# Bring outputs to CPU and post-process them
|
41 |
+
outputs.logits = outputs.logits.cpu()
|
42 |
+
outputs.pred_boxes = outputs.pred_boxes.cpu()
|
43 |
+
results = processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes)
|
44 |
+
boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
|
45 |
+
|
46 |
+
result_labels = []
|
47 |
+
for box, score, label in zip(boxes, scores, labels):
|
48 |
+
if score < score_threshold:
|
49 |
+
continue
|
50 |
+
# OWLv2 returns label indices corresponding to the order of the input queries.
|
51 |
+
if label.item() < len(queries):
|
52 |
+
result_label = queries[label.item()]
|
53 |
+
else:
|
54 |
+
result_label = "unknown"
|
55 |
+
box = [int(i) for i in box.tolist()]
|
56 |
+
result_labels.append((box, result_label))
|
57 |
+
|
58 |
+
return img, result_labels
|
59 |
+
|
60 |
+
description = """
|
61 |
+
This demo uses OWLv2 for zero-shot object detection, specifically tailored for home interior and renovation defects.
|
62 |
+
Enter comma-separated text queries describing issues relevant to home renovations—for example:
|
63 |
+
"pipe defect, rust on pipe, cracked pipe, plastic pipe defect, metal pipe defect, water damage on wall, mold on wall, broken sink, damaged cabinet, faulty door".
|
64 |
+
If left blank, a default set of queries will be used.
|
65 |
+
"""
|
66 |
+
|
67 |
+
demo = gr.Interface(
|
68 |
+
fn=query_image,
|
69 |
+
inputs=[
|
70 |
+
gr.Image(type="pil", label="Upload an Image"),
|
71 |
+
gr.Textbox(default=default_queries, label="Text Queries"),
|
72 |
+
gr.Slider(0, 1, value=0.1, label="Score Threshold")
|
73 |
+
],
|
74 |
+
outputs=[gr.Image(label="Annotated Image"), "json"],
|
75 |
+
title="Zero-Shot Home Renovation Defect Detection with OWLv2",
|
76 |
+
description=description,
|
77 |
+
examples=[
|
78 |
+
# Replace these example paths with your sample images if available.
|
79 |
+
["assets/pipe_sample.jpg", default_queries, 0.11],
|
80 |
+
["assets/kitchen_renovation.jpg", default_queries, 0.1],
|
81 |
+
],
|
82 |
+
)
|
83 |
+
|
84 |
+
demo.launch()
|