Spaces:
Runtime error
Runtime error
Output Return an image
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
-
from PIL import Image
|
| 4 |
import torch
|
| 5 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 6 |
|
|
@@ -20,16 +20,27 @@ def predict(image):
|
|
| 20 |
# Extract bounding boxes and class labels
|
| 21 |
predicted_boxes = outputs.pred_boxes[0].cpu().numpy() # First image
|
| 22 |
predicted_classes = outputs.logits.argmax(-1).cpu().numpy() # Class predictions
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Set up the Gradio interface
|
| 28 |
interface = gr.Interface(
|
| 29 |
fn=predict, # The function that gets called when an image is uploaded
|
| 30 |
inputs=gr.Image(type="pil"), # Image input (as PIL image)
|
| 31 |
-
outputs="
|
| 32 |
)
|
| 33 |
|
| 34 |
# Launch the Gradio app
|
| 35 |
interface.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
import torch
|
| 5 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 6 |
|
|
|
|
| 20 |
# Extract bounding boxes and class labels
|
| 21 |
predicted_boxes = outputs.pred_boxes[0].cpu().numpy() # First image
|
| 22 |
predicted_classes = outputs.logits.argmax(-1).cpu().numpy() # Class predictions
|
| 23 |
+
|
| 24 |
+
# Create a drawing context for the image
|
| 25 |
+
draw = ImageDraw.Draw(image)
|
| 26 |
+
width, height = image.size
|
| 27 |
+
|
| 28 |
+
# Loop over all detected boxes and draw them on the image
|
| 29 |
+
for box in predicted_boxes:
|
| 30 |
+
# Box coordinates are normalized, so multiply by image dimensions
|
| 31 |
+
x0, y0, x1, y1 = box
|
| 32 |
+
draw.rectangle([x0 * width, y0 * height, x1 * width, y1 * height], outline="red", width=3)
|
| 33 |
+
|
| 34 |
+
# Return the image with bounding boxes drawn
|
| 35 |
+
return image
|
| 36 |
|
| 37 |
# Set up the Gradio interface
|
| 38 |
interface = gr.Interface(
|
| 39 |
fn=predict, # The function that gets called when an image is uploaded
|
| 40 |
inputs=gr.Image(type="pil"), # Image input (as PIL image)
|
| 41 |
+
outputs=gr.Image(type="pil"), # Outputting the image with boxes drawn
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the Gradio app
|
| 45 |
interface.launch()
|
| 46 |
+
|