Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
3 |
-
from PIL import Image
|
4 |
import torch
|
5 |
|
6 |
image_processor = AutoImageProcessor.from_pretrained('hustvl/yolos-small')
|
@@ -15,11 +15,16 @@ def detect(image):
|
|
15 |
results = image_processor.post_process_object_detection(outputs,
|
16 |
threshold=0.9,
|
17 |
target_sizes=target_sizes)[0]
|
|
|
|
|
|
|
18 |
|
19 |
# model predicts bounding boxes and corresponding COCO classes
|
20 |
#logits = outputs.logits
|
21 |
#bboxes = outputs.pred_boxes
|
22 |
|
|
|
|
|
23 |
# label and the count
|
24 |
counts = {}
|
25 |
|
@@ -30,12 +35,16 @@ def detect(image):
|
|
30 |
counts[label_name] = 0
|
31 |
counts[label_name] += 1
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
demo = gr.Interface(
|
36 |
fn=detect,
|
37 |
inputs=[gr.inputs.Image(label="Input image", type="pil")],
|
38 |
-
outputs=["text"], #, gr.Label(num_top_classes=10)],
|
39 |
title="Object Counts in Image"
|
40 |
)
|
41 |
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
3 |
+
from PIL import Image, ImageDraw
|
4 |
import torch
|
5 |
|
6 |
image_processor = AutoImageProcessor.from_pretrained('hustvl/yolos-small')
|
|
|
15 |
results = image_processor.post_process_object_detection(outputs,
|
16 |
threshold=0.9,
|
17 |
target_sizes=target_sizes)[0]
|
18 |
+
|
19 |
+
# Bounding box in COCO format:
|
20 |
+
# [x_min, y_min, width, height]
|
21 |
|
22 |
# model predicts bounding boxes and corresponding COCO classes
|
23 |
#logits = outputs.logits
|
24 |
#bboxes = outputs.pred_boxes
|
25 |
|
26 |
+
draw = ImageDraw.Draw(image)
|
27 |
+
|
28 |
# label and the count
|
29 |
counts = {}
|
30 |
|
|
|
35 |
counts[label_name] = 0
|
36 |
counts[label_name] += 1
|
37 |
|
38 |
+
x, y, w, h = tuple(box)
|
39 |
+
draw.rectangle((x, y, x+w, y+h), outline="red", width=1)
|
40 |
+
draw.text((x, y), label_name, fill="white")
|
41 |
+
|
42 |
+
return results, image
|
43 |
|
44 |
demo = gr.Interface(
|
45 |
fn=detect,
|
46 |
inputs=[gr.inputs.Image(label="Input image", type="pil")],
|
47 |
+
outputs=["text", "image"], #, gr.Label(num_top_classes=10)],
|
48 |
title="Object Counts in Image"
|
49 |
)
|
50 |
|