Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,18 +6,39 @@ pipe = pipeline("object-detection" , model = "hustvl/yolos-tiny")
|
|
| 6 |
|
| 7 |
def detect_objects(image):
|
| 8 |
results = pipe(image)
|
|
|
|
| 9 |
draw = ImageDraw.Draw(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
box = r['box']
|
| 13 |
draw.rectangle(
|
| 14 |
-
[(box[
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
return image
|
| 23 |
|
|
|
|
| 6 |
|
| 7 |
def detect_objects(image):
|
| 8 |
results = pipe(image)
|
| 9 |
+
|
| 10 |
draw = ImageDraw.Draw(image)
|
| 11 |
+
try:
|
| 12 |
+
font = ImageFont.truetype("arial.ttf", 18)
|
| 13 |
+
except:
|
| 14 |
+
font = ImageFont.load_default()
|
| 15 |
+
|
| 16 |
+
for r in results:
|
| 17 |
+
box = r["box"]
|
| 18 |
+
label = f"{r['label']} {r['score']:.2f}"
|
| 19 |
+
|
| 20 |
+
# stil ayarları
|
| 21 |
+
color = "red"
|
| 22 |
+
box_width = 4
|
| 23 |
+
text_padding = 3
|
| 24 |
|
| 25 |
+
# Bounding Box
|
|
|
|
| 26 |
draw.rectangle(
|
| 27 |
+
[(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
|
| 28 |
+
outline=color, width=box_width
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Label
|
| 32 |
+
text_w, text_h = draw.textsize(label, font=font)
|
| 33 |
+
text_bg = [
|
| 34 |
+
(box["xmin"], box["ymin"] - text_h - text_padding),
|
| 35 |
+
(box["xmin"] + text_w + text_padding * 2, box["ymin"])
|
| 36 |
+
]
|
| 37 |
+
draw.rectangle(text_bg, fill=color)
|
| 38 |
+
draw.text(
|
| 39 |
+
(box["xmin"] + text_padding, box["ymin"] - text_h - text_padding),
|
| 40 |
+
label, fill="white", font=font
|
| 41 |
+
)
|
| 42 |
|
| 43 |
return image
|
| 44 |
|