wangli commited on
Commit
4868d0a
1 Parent(s): 3d3a106

feat: init

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

Git LFS Details

  • SHA256: b2d6d1e276a467b2b778389fcac4b0cc09487bac42c5642ec0ceb72cdd5a7484
  • Pointer size: 130 Bytes
  • Size of remote file: 30.2 kB
samples/2.JPG ADDED

Git LFS Details

  • SHA256: 3e51dbd2b79c32fc1ad34c0b063ea942646cff4796cf136bb91b9fc94945d19a
  • Pointer size: 132 Bytes
  • Size of remote file: 6.32 MB