Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
3 |
+
import torch
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
|
6 |
+
# Load the model and processor
|
7 |
+
processor = AutoImageProcessor.from_pretrained("0llheaven/Conditional-detr-finetuned-V5")
|
8 |
+
model = AutoModelForObjectDetection.from_pretrained("0llheaven/Conditional-detr-finetuned-V5")
|
9 |
+
|
10 |
+
def detect_objects(image, score_threshold):
|
11 |
+
# Convert image to RGB if it's grayscale
|
12 |
+
if image.mode != "RGB":
|
13 |
+
image = image.convert("RGB")
|
14 |
+
|
15 |
+
# Prepare input for the model
|
16 |
+
inputs = processor(images=image, return_tensors="pt")
|
17 |
+
outputs = model(**inputs)
|
18 |
+
|
19 |
+
# Filter predictions based on the user-defined score threshold
|
20 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
21 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
22 |
+
|
23 |
+
labels_output = []
|
24 |
+
no_detection = True
|
25 |
+
|
26 |
+
# Draw bounding boxes around detected objects
|
27 |
+
draw = ImageDraw.Draw(image)
|
28 |
+
for result in results:
|
29 |
+
scores = result["scores"]
|
30 |
+
labels = result["labels"]
|
31 |
+
boxes = result["boxes"]
|
32 |
+
|
33 |
+
for score, label, box in zip(scores, labels, boxes):
|
34 |
+
if score >= score_threshold: # Only draw if score is above threshold
|
35 |
+
no_detection = False
|
36 |
+
box = [round(i, 2) for i in box.tolist()]
|
37 |
+
|
38 |
+
if label.item() == 0:
|
39 |
+
label_name = "Pneumonia"
|
40 |
+
else label.item() == 1:
|
41 |
+
label_name = "Normal"
|
42 |
+
|
43 |
+
draw.rectangle(box, outline="red", width=3)
|
44 |
+
draw.text((box[0], box[1]), f"{label_name}: {round(score.item(), 3)}", fill="red")
|
45 |
+
labels_output.append(f"{label_name}: {round(score.item(), 3)}")
|
46 |
+
|
47 |
+
# If no detections, set label as 'Other'
|
48 |
+
if no_detection:
|
49 |
+
labels_output.append("Other")
|
50 |
+
|
51 |
+
return image, "\n".join(labels_output)
|
52 |
+
|
53 |
+
# Create the Gradio interface
|
54 |
+
interface = gr.Interface(
|
55 |
+
fn=detect_objects,
|
56 |
+
inputs=[gr.Image(type="pil"), gr.Slider(0, 1, value=0.5, label="Score Threshold")], # Add slider for score threshold
|
57 |
+
# outputs=gr.Image(type="pil"), # Corrected output type
|
58 |
+
outputs=[gr.Image(type="pil"), gr.Textbox(label="Detected Objects")],
|
59 |
+
title="Object Detection with Transformers",
|
60 |
+
description="Upload an image to detect objects using a fine-tuned Conditional-DETR model."
|
61 |
+
)
|
62 |
+
|
63 |
+
# Launch the interface
|
64 |
+
interface.launch()
|