andrewgleave commited on
Commit
19a011f
·
1 Parent(s): b37b4db

Working prototype

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -1,10 +1,64 @@
 
 
 
1
  import gradio as gr
2
- import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
 
 
 
 
 
4
 
5
- def flip(im):
6
- return np.flipud(im)
 
 
 
 
 
 
 
 
 
 
 
7
 
 
 
 
 
 
 
 
 
8
 
9
- demo = gr.Interface(flip, gr.Image(source="webcam", streaming=True), "image", live=True)
10
  demo.launch()
 
1
+ import io
2
+
3
+ import torch
4
  import gradio as gr
5
+ import matplotlib
6
+ import matplotlib.pyplot as plt
7
+ from PIL import Image
8
+
9
+ from transformers import AutoFeatureExtractor, AutoModelForObjectDetection
10
+
11
+ extractor = AutoFeatureExtractor.from_pretrained("hustvl/yolos-tiny")
12
+ model = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-tiny")
13
+
14
+ matplotlib.pyplot.switch_backend('Agg')
15
+
16
+ COLORS = [
17
+ [0.000, 0.447, 0.741],
18
+ [0.850, 0.325, 0.098],
19
+ [0.929, 0.694, 0.125],
20
+ [0.494, 0.184, 0.556],
21
+ [0.466, 0.674, 0.188],
22
+ [0.301, 0.745, 0.933]
23
+ ]
24
+
25
+ PRED_THRESHOLD = 0.90
26
+
27
+ def fig2img(fig):
28
+ buf = io.BytesIO()
29
+ fig.savefig(buf)
30
+ buf.seek(0)
31
+ img = Image.open(buf)
32
+ return img
33
 
34
+ def composite_predictions(img, processed_predictions):
35
+ keep = processed_predictions["labels"] == 1 # only interested in people
36
+ boxes = processed_predictions["boxes"][keep].tolist()
37
+ scores = processed_predictions["scores"][keep].tolist()
38
+ labels = processed_predictions["labels"][keep].tolist()
39
 
40
+ labels = [model.config.id2label[x] for x in labels]
41
+
42
+ plt.figure(figsize=(16, 10))
43
+ plt.imshow(img)
44
+ axis = plt.gca()
45
+ colors = COLORS * 100
46
+ for score, (xmin, ymin, xmax, ymax), label, color in zip(scores, boxes, labels, colors):
47
+ axis.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, color=color, linewidth=3))
48
+ axis.text(xmin, ymin, f"{label}: {score:0.2f}", fontsize=15, bbox=dict(facecolor="yellow", alpha=0.5))
49
+ plt.axis("off")
50
+ img = fig2img(plt.gcf())
51
+ matplotlib.pyplot.close()
52
+ return img
53
 
54
+ def process(img):
55
+ inputs = extractor(images=img, return_tensors="pt")
56
+ outputs = model(**inputs)
57
+ img_size = torch.tensor([tuple(reversed(img.size))])
58
+ processed = extractor.post_process_object_detection(outputs, PRED_THRESHOLD, img_size)
59
+
60
+ # Composite image and prediction bounding boxes + labels prediction
61
+ return composite_predictions(img, processed[0])
62
 
63
+ demo = gr.Interface(fn=process, inputs=[gr.Image(source="webcam", streaming=True, type='pil')], outputs=["image"], live=True)
64
  demo.launch()