Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
+
|
| 4 |
+
# Use a pipeline as a high-level helper
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
object_detector = pipeline("object-detection", model="facebook/detr-resnet-101-dc5")
|
| 8 |
+
|
| 9 |
+
def draw_bounding_boxes(image, detections, font_path=None, font_size=20):
|
| 10 |
+
"""
|
| 11 |
+
Draws bounding boxes on the given image based on the detections.
|
| 12 |
+
:param image: PIL.Image object
|
| 13 |
+
:param detections: List of detection results, where each result is a dictionary containing
|
| 14 |
+
'score', 'label', and 'box' keys. 'box' itself is a dictionary with 'xmin',
|
| 15 |
+
'ymin', 'xmax', 'ymax'.
|
| 16 |
+
:param font_path: Path to the TrueType font file to use for text.
|
| 17 |
+
:param font_size: Size of the font to use for text.
|
| 18 |
+
:return: PIL.Image object with bounding boxes drawn.
|
| 19 |
+
"""
|
| 20 |
+
# Make a copy of the image to draw on
|
| 21 |
+
draw_image = image.copy()
|
| 22 |
+
draw = ImageDraw.Draw(draw_image)
|
| 23 |
+
|
| 24 |
+
# Load custom font or default font if path not provided
|
| 25 |
+
if font_path:
|
| 26 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 27 |
+
else:
|
| 28 |
+
# When font_path is not provided, load default font but it's size is fixed
|
| 29 |
+
font = ImageFont.load_default()
|
| 30 |
+
# Increase font size workaround by using a TTF font file, if needed, can download and specify the path
|
| 31 |
+
|
| 32 |
+
for detection in detections:
|
| 33 |
+
box = detection['box']
|
| 34 |
+
xmin = box['xmin']
|
| 35 |
+
ymin = box['ymin']
|
| 36 |
+
xmax = box['xmax']
|
| 37 |
+
ymax = box['ymax']
|
| 38 |
+
|
| 39 |
+
# Draw the bounding box
|
| 40 |
+
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
|
| 41 |
+
|
| 42 |
+
# Optionally, you can also draw the label and score
|
| 43 |
+
label = detection['label']
|
| 44 |
+
score = detection['score']
|
| 45 |
+
text = f"{label} {score:.2f}"
|
| 46 |
+
|
| 47 |
+
# Draw text with background rectangle for visibility
|
| 48 |
+
if font_path: # Use the custom font with increased size
|
| 49 |
+
text_size = draw.textbbox((xmin, ymin), text, font=font)
|
| 50 |
+
else:
|
| 51 |
+
# Calculate text size using the default font
|
| 52 |
+
text_size = draw.textbbox((xmin, ymin), text)
|
| 53 |
+
|
| 54 |
+
draw.rectangle([(text_size[0], text_size[1]), (text_size[2], text_size[3])], fill="red")
|
| 55 |
+
draw.text((xmin, ymin), text, fill="white", font=font)
|
| 56 |
+
|
| 57 |
+
return draw_image
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def detect_object(image):
|
| 61 |
+
raw_image = image
|
| 62 |
+
output = object_detector(raw_image)
|
| 63 |
+
processed_image = draw_bounding_boxes(raw_image, output)
|
| 64 |
+
return processed_image
|
| 65 |
+
|
| 66 |
+
demo = gr.Interface(fn=detect_object,
|
| 67 |
+
inputs=[gr.Image(label="Bild auswählen",type="pil")],
|
| 68 |
+
outputs=[gr.Image(label="Verarbeitetes Bild", type="pil")],
|
| 69 |
+
title="Project 6: Objekt-Detektor",
|
| 70 |
+
description="DIESE ANWENDUNG WIRD VERWENDET, UM OBJEKTE IN DEM ANGEGEBENEN EINGABEBILD ZU ERKENNEN.",
|
| 71 |
+
allow_flagging="never",
|
| 72 |
+
submit_btn="Übermitteln",
|
| 73 |
+
clear_btn="Bereinigen")
|
| 74 |
+
|
| 75 |
+
demo.launch()
|