muttalib1326 commited on
Commit
228b670
1 Parent(s): 6ccba06

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +84 -0
  2. gitignore.txt +1 -0
  3. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from transformers import AutoImageProcessor, AutoModelForObjectDetection
4
+ #from transformers import pipeline
5
+
6
+ from PIL import Image
7
+ import matplotlib.pyplot as plt
8
+ import matplotlib.patches as patches
9
+
10
+ import io
11
+ from random import choice
12
+
13
+ image_processor_tiny = AutoImageProcessor.from_pretrained("hustvl/yolos-tiny")
14
+ model_tiny = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-tiny")
15
+
16
+ import gradio as gr
17
+
18
+ COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
19
+ "#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
20
+ "#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
21
+
22
+ fdic = {
23
+ "family" : "DejaVu Serif",
24
+ "style" : "normal",
25
+ "size" : 18,
26
+ "color" : "yellow",
27
+ "weight" : "bold"
28
+ }
29
+
30
+
31
+ def get_figure(in_pil_img, in_results):
32
+ plt.figure(figsize=(16, 10))
33
+ plt.imshow(in_pil_img)
34
+ ax = plt.gca()
35
+
36
+ for score, label, box in zip(in_results["scores"], in_results["labels"], in_results["boxes"]):
37
+ selected_color = choice(COLORS)
38
+
39
+ box_int = [i.item() for i in torch.round(box).to(torch.int32)]
40
+ x, y, w, h = box_int[0], box_int[1], box_int[2]-box_int[0], box_int[3]-box_int[1]
41
+ #x, y, w, h = torch.round(box[0]).item(), torch.round(box[1]).item(), torch.round(box[2]-box[0]).item(), torch.round(box[3]-box[1]).item()
42
+
43
+ ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3, alpha=0.8))
44
+ ax.text(x, y, f"{model_tiny.config.id2label[label.item()]}: {round(score.item()*100, 2)}%", fontdict=fdic, alpha=0.8)
45
+
46
+ plt.axis("off")
47
+
48
+ return plt.gcf()
49
+
50
+
51
+ def infer(in_pil_img, in_threshold=0.9):
52
+ target_sizes = torch.tensor([in_pil_img.size[::-1]])
53
+
54
+ inputs = image_processor_tiny(images=in_pil_img, return_tensors="pt")
55
+ outputs = model_tiny(**inputs)
56
+
57
+ # convert outputs (bounding boxes and class logits) to COCO API
58
+ results = image_processor_tiny.post_process_object_detection(outputs, threshold=in_threshold, target_sizes=target_sizes)[0]
59
+
60
+ figure = get_figure(in_pil_img, results)
61
+
62
+ buf = io.BytesIO()
63
+ figure.savefig(buf, bbox_inches='tight')
64
+ buf.seek(0)
65
+ output_pil_img = Image.open(buf)
66
+
67
+ return output_pil_img
68
+
69
+
70
+ with gr.Blocks(title="Object Detection") as demo:
71
+
72
+ with gr.Row():
73
+ input_image = gr.Image(label="Input image", type="pil")
74
+ output_image = gr.Image(label="Output image with predicted instances", type="pil")
75
+
76
+ gr.Examples(['samples/1.jpeg', 'samples/2.JPG'], inputs=input_image)
77
+
78
+ threshold = gr.Slider(0, 1.0, value=0.9, label='threshold')
79
+
80
+ send_btn = gr.Button("Infer")
81
+ send_btn.click(fn=infer, inputs=[input_image, threshold], outputs=[output_image])
82
+
83
+ #demo.queue()
84
+ demo.launch(debug=True)
gitignore.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ .idea/
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ transformers[timm]