Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,25 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 3 |
import torch
|
| 4 |
from PIL import Image, ImageDraw
|
| 5 |
-
import io
|
| 6 |
|
| 7 |
-
#
|
| 8 |
processor = AutoImageProcessor.from_pretrained("0llheaven/Conditional-detr-finetuned")
|
| 9 |
model = AutoModelForObjectDetection.from_pretrained("0llheaven/Conditional-detr-finetuned")
|
| 10 |
|
| 11 |
def detect_objects(image):
|
| 12 |
-
#
|
| 13 |
if image.mode != "RGB":
|
| 14 |
image = image.convert("RGB")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
inputs = processor(images=image, return_tensors="pt")
|
| 18 |
outputs = model(**inputs)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
target_sizes = torch.tensor([image.size[::-1]])
|
| 22 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
draw = ImageDraw.Draw(image)
|
| 26 |
for result in results:
|
| 27 |
scores = result["scores"]
|
|
@@ -34,18 +33,16 @@ def detect_objects(image):
|
|
| 34 |
draw.rectangle(box, outline="red", width=3)
|
| 35 |
draw.text((box[0], box[1]), f"{label_name}: {round(score.item(), 3)}", fill="red")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 39 |
-
return pil_imag
|
| 40 |
|
| 41 |
-
#
|
| 42 |
interface = gr.Interface(
|
| 43 |
fn=detect_objects,
|
| 44 |
inputs=gr.Image(type="pil"),
|
| 45 |
-
outputs=gr.Image(type="
|
| 46 |
title="Object Detection with Transformers",
|
| 47 |
description="Upload an image to detect objects using a fine-tuned Conditional-DETR model."
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
interface.launch()
|
|
|
|
| 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")
|
| 8 |
model = AutoModelForObjectDetection.from_pretrained("0llheaven/Conditional-detr-finetuned")
|
| 9 |
|
| 10 |
def detect_objects(image):
|
| 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 with confidence greater than 0.5
|
| 20 |
target_sizes = torch.tensor([image.size[::-1]])
|
| 21 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
| 22 |
|
| 23 |
+
# Draw bounding boxes around detected objects
|
| 24 |
draw = ImageDraw.Draw(image)
|
| 25 |
for result in results:
|
| 26 |
scores = result["scores"]
|
|
|
|
| 33 |
draw.rectangle(box, outline="red", width=3)
|
| 34 |
draw.text((box[0], box[1]), f"{label_name}: {round(score.item(), 3)}", fill="red")
|
| 35 |
|
| 36 |
+
return image
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Create the Gradio interface
|
| 39 |
interface = gr.Interface(
|
| 40 |
fn=detect_objects,
|
| 41 |
inputs=gr.Image(type="pil"),
|
| 42 |
+
outputs=gr.Image(type="pil"), # Corrected output type
|
| 43 |
title="Object Detection with Transformers",
|
| 44 |
description="Upload an image to detect objects using a fine-tuned Conditional-DETR model."
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# Launch the interface
|
| 48 |
interface.launch()
|