Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
|
|
3 |
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
demo = gr.Interface(
|
10 |
-
|
11 |
gr.Image(type="pil"),
|
12 |
"image",
|
13 |
examples=[
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import ImageDraw
|
4 |
+
from transformers import AutoModelForObjectDetection, AutoImageProcessor
|
5 |
|
6 |
|
7 |
+
processor = AutoImageProcessor.from_pretrained("tanukinet/hanko")
|
8 |
+
model = AutoModelForObjectDetection.from_pretrained("tanukinet/hanko", ignore_mismatched_sizes=True,)
|
9 |
+
|
10 |
+
|
11 |
+
def object_detection(image):
|
12 |
+
image = image.copy()
|
13 |
+
inputs = processor(images=image, return_tensors="pt")
|
14 |
+
outputs = model(**inputs)
|
15 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
16 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.8)[0]
|
17 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
18 |
+
box = [round(i, 2) for i in box.tolist()]
|
19 |
+
print(
|
20 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
21 |
+
f"{round(score.item(), 3)} at location {box}"
|
22 |
+
)
|
23 |
+
draw = ImageDraw.Draw(image)
|
24 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
25 |
+
box = [round(i, 2) for i in box.tolist()]
|
26 |
+
x, y, x2, y2 = tuple(box)
|
27 |
+
draw.rectangle((x, y, x2, y2), outline="red", width=1)
|
28 |
+
draw.text((x, y), model.config.id2label[label.item()], fill="white")
|
29 |
+
return image
|
30 |
|
31 |
|
32 |
demo = gr.Interface(
|
33 |
+
object_detection,
|
34 |
gr.Image(type="pil"),
|
35 |
"image",
|
36 |
examples=[
|