kvignesh17 commited on
Commit
3e244d6
1 Parent(s): 494c20f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoFeatureExtractor, YolosForObjectDetection
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import torch
5
+ import matplotlib.pyplot as plt
6
+ import io
7
+ import numpy as np
8
+
9
+
10
+ COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
11
+ [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
12
+
13
+
14
+ def get_class_list_from_input(classes_string: str):
15
+ if classes_string == "":
16
+ return []
17
+ classes_list = classes_string.split(",")
18
+ classes_list = [x.strip() for x in classes_list]
19
+ return classes_list
20
+
21
+ def infer(img, model_name: str, prob_threshold: int, classes_to_show = str):
22
+ feature_extractor = AutoFeatureExtractor.from_pretrained(f"hustvl/{model_name}")
23
+ model = YolosForObjectDetection.from_pretrained(f"hustvl/{model_name}")
24
+
25
+ img = Image.fromarray(img)
26
+
27
+ pixel_values = feature_extractor(img, return_tensors="pt").pixel_values
28
+
29
+ with torch.no_grad():
30
+ outputs = model(pixel_values, output_attentions=True)
31
+
32
+ probas = outputs.logits.softmax(-1)[0, :, :-1]
33
+ keep = probas.max(-1).values > prob_threshold
34
+
35
+ target_sizes = torch.tensor(img.size[::-1]).unsqueeze(0)
36
+ postprocessed_outputs = feature_extractor.post_process(outputs, target_sizes)
37
+ bboxes_scaled = postprocessed_outputs[0]['boxes']
38
+
39
+ classes_list = get_class_list_from_input(classes_to_show)
40
+ res_img = plot_results(img, probas[keep], bboxes_scaled[keep], model, classes_list)
41
+
42
+ return res_img
43
+
44
+ def plot_results(pil_img, prob, boxes, model, classes_list):
45
+ plt.figure(figsize=(16,10))
46
+ plt.imshow(pil_img)
47
+ ax = plt.gca()
48
+ colors = COLORS * 100
49
+ for p, (xmin, ymin, xmax, ymax), c in zip(prob, boxes.tolist(), colors):
50
+ cl = p.argmax()
51
+ object_class = model.config.id2label[cl.item()]
52
+
53
+ if len(classes_list) > 0 :
54
+ if object_class not in classes_list:
55
+ continue
56
+
57
+ ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
58
+ fill=False, color=c, linewidth=3))
59
+ text = f'{object_class}: {p[cl]:0.2f}'
60
+ ax.text(xmin, ymin, text, fontsize=15,
61
+ bbox=dict(facecolor='yellow', alpha=0.5))
62
+ plt.axis('off')
63
+ return fig2img(plt.gcf())
64
+
65
+ def fig2img(fig):
66
+ buf = io.BytesIO()
67
+ fig.savefig(buf)
68
+ buf.seek(0)
69
+ img = Image.open(buf)
70
+ return img
71
+
72
+ description = """Object Detection with YOLOS. Choose https://github.com/amikelive/coco-labels/blob/master/coco-labels-2014_2017.txtyour model and you're good to go.
73
+ You can adapt the minimum probability threshold with the slider.
74
+ Additionally you can restrict the classes that will be shown by putting in a comma separated list of
75
+ [COCO classes](https://github.com/amikelive/coco-labels/blob/master/coco-labels-2014_2017.txt).
76
+ Leaving the field empty will show all classes"""
77
+
78
+ image_in = gr.components.Image()
79
+ image_out = gr.components.Image()
80
+ model_choice = gr.components.Dropdown(["yolos-tiny", "yolos-small", "yolos-base", "yolos-small-300", "yolos-small-dwr"], value="yolos-small", label="YOLOS Model")
81
+ prob_threshold_slider = gr.components.Slider(minimum=0, maximum=1.0, step=0.01, value=0.9, label="Probability Threshold")
82
+ classes_to_show = gr.components.Textbox(placeholder="e.g. person, boat", label="Classes to use (empty means all classes)")
83
+
84
+ Iface = gr.Interface(
85
+ fn=infer,
86
+ inputs=[image_in,model_choice, prob_threshold_slider, classes_to_show],
87
+ outputs=image_out,
88
+ #examples=[["examples/10_People_Marching_People_Marching_2_120.jpg"], ["examples/12_Group_Group_12_Group_Group_12_26.jpg"], ["examples/43_Row_Boat_Canoe_43_247.jpg"]],
89
+ title="Object Detection with YOLOS",
90
+ description=description,
91
+ ).launch()