gurwindersingh commited on
Commit
20a5b70
·
1 Parent(s): cb9d9e9

Upload 2 files

Browse files
Files changed (2) hide show
  1. app (1).py +85 -0
  2. requirements (1).txt +2 -0
app (1).py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('pip uninstall gradio')
3
+ os.system('pip install gradio==3.50.2')
4
+ import gradio as gr
5
+ import pandas as pd
6
+ from transformers import AutoImageProcessor, AutoModelForObjectDetection
7
+ from PIL import Image, ImageDraw
8
+ import torch
9
+
10
+ image_processor = AutoImageProcessor.from_pretrained('hustvl/yolos-small')
11
+ model = AutoModelForObjectDetection.from_pretrained('hustvl/yolos-small')
12
+
13
+ colors = ["red",
14
+ "orange",
15
+ "yellow",
16
+ "green",
17
+ "blue",
18
+ "indigo",
19
+ "violet",
20
+ "brown",
21
+ "black",
22
+ "slategray",
23
+ ]
24
+
25
+ # Resized image width
26
+ WIDTH = 600
27
+
28
+ def detect(image):
29
+ width, height = image.size
30
+ ratio = float(WIDTH) / float(width)
31
+ new_h = height * ratio
32
+
33
+ image = image.resize((int(WIDTH), int(new_h)), Image.Resampling.LANCZOS)
34
+
35
+ inputs = image_processor(images=image, return_tensors="pt")
36
+ outputs = model(**inputs)
37
+
38
+ # convert outputs to COCO API
39
+ target_sizes = torch.tensor([image.size[::-1]])
40
+ results = image_processor.post_process_object_detection(outputs,
41
+ threshold=0.9,
42
+ target_sizes=target_sizes)[0]
43
+
44
+ draw = ImageDraw.Draw(image)
45
+
46
+ # label and the count
47
+ counts = {}
48
+
49
+ for score, label in zip(results["scores"], results["labels"]):
50
+ label_name = model.config.id2label[label.item()]
51
+ if label_name not in counts:
52
+ counts[label_name] = 0
53
+ counts[label_name] += 1
54
+
55
+ count_results = {k: v for k, v in (sorted(counts.items(), key=lambda item: item[1], reverse=True)[:10])}
56
+ label2color = {}
57
+ for idx, label in enumerate(count_results):
58
+ label2color[label] = colors[idx]
59
+
60
+ for label, box in zip(results["labels"], results["boxes"]):
61
+ label_name = model.config.id2label[label.item()]
62
+
63
+ if label_name in count_results:
64
+ box = [round(i, 4) for i in box.tolist()]
65
+ x1, y1, x2, y2 = tuple(box)
66
+ draw.rectangle((x1, y1, x2, y2), outline=label2color[label_name], width=2)
67
+ draw.text((x1, y1), label_name, fill="white")
68
+
69
+ df = pd.DataFrame({
70
+ 'label': [label for label in count_results],
71
+ 'counts': [counts[label] for label in count_results]
72
+ })
73
+
74
+ return image, df, count_results
75
+
76
+ demo = gr.Interface(
77
+ fn=detect,
78
+
79
+ inputs=[gr.inputs.Image(label="Input image", type="pil")],
80
+ outputs=[gr.Image(label="Output image"), gr.BarPlot(show_label=False, x="label", y="counts", x_title="Labels", y_title="Counts", vertical=False), gr.Textbox(show_label=False)],
81
+ title="YOLO Object Detection",
82
+ cache_examples=False
83
+ )
84
+
85
+ demo.launch()
requirements (1).txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ transformers