Anindyadeep
commited on
Commit
•
ebbfbd3
1
Parent(s):
7ebcf87
initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
from PIL import Image, ImageDraw
|
6 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
7 |
+
|
8 |
+
description = """
|
9 |
+
## This interface is made with 🤗 Gradio.
|
10 |
+
|
11 |
+
Simply upload an image of any person wearning/not-wearing helmet.
|
12 |
+
"""
|
13 |
+
|
14 |
+
image_processor = AutoImageProcessor.from_pretrained(
|
15 |
+
"devonho/detr-resnet-50_finetuned_cppe5"
|
16 |
+
)
|
17 |
+
model = AutoModelForObjectDetection.from_pretrained(
|
18 |
+
"devonho/detr-resnet-50_finetuned_cppe5"
|
19 |
+
)
|
20 |
+
|
21 |
+
# Gradio Components
|
22 |
+
|
23 |
+
image_in = gr.components.Image()
|
24 |
+
image_out = gr.components.Image()
|
25 |
+
|
26 |
+
|
27 |
+
def model_inference(img):
|
28 |
+
with torch.no_grad():
|
29 |
+
inputs = image_processor(images=img, return_tensors="pt")
|
30 |
+
outputs = model(**inputs)
|
31 |
+
target_sizes = torch.tensor([img.size[::-1]])
|
32 |
+
results = image_processor.post_process_object_detection(
|
33 |
+
outputs, threshold=0.5, target_sizes=target_sizes
|
34 |
+
)[0]
|
35 |
+
return results
|
36 |
+
|
37 |
+
|
38 |
+
def plot_results(image):
|
39 |
+
image = Image.fromarray(np.uint8(image))
|
40 |
+
results = model_inference(img=image)
|
41 |
+
draw = ImageDraw.Draw(image)
|
42 |
+
for score, label, box in zip(
|
43 |
+
results["scores"], results["labels"], results["boxes"]
|
44 |
+
):
|
45 |
+
score = score.item()
|
46 |
+
box = [round(i, 2) for i in box.tolist()]
|
47 |
+
x, y, x2, y2 = tuple(box)
|
48 |
+
draw.rectangle((x, y, x2, y2), outline="red", width=1)
|
49 |
+
draw.text((x, y), model.config.id2label[label.item()], fill="white")
|
50 |
+
draw.text((x+0.5, y-0.5), text=str(score), fill='green' if score > 0.7 else 'red')
|
51 |
+
return image
|
52 |
+
|
53 |
+
|
54 |
+
Iface = gr.Interface(
|
55 |
+
fn=plot_results,
|
56 |
+
inputs=[image_in],
|
57 |
+
outputs=image_out,
|
58 |
+
title="Object Detection Using Fine-Tuned Vision Transformers",
|
59 |
+
description=description,
|
60 |
+
).launch()
|