kisa-misa commited on
Commit
f7171a3
·
1 Parent(s): 195912f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -100
app.py CHANGED
@@ -14,76 +14,13 @@ torch.hub.download_url_to_file('https://tochkanews.ru/wp-content/uploads/2020/09
14
  torch.hub.download_url_to_file('https://s.rdrom.ru/1/pubs/4/35893/1906770.jpg', '2.jpg')
15
  torch.hub.download_url_to_file('https://static.mk.ru/upload/entities/2022/04/17/07/articles/detailPicture/5b/39/28/b6/ffb1aa636dd62c30e6ff670f84474f75.jpg', '3.jpg')
16
 
17
- def yolox_inference(
18
- image_path: gr.inputs.Image = None,
19
- model_path: gr.inputs.Dropdown = 'kadirnar/yolox_s-v0.1.1',
20
- config_path: gr.inputs.Textbox = 'configs.yolox_s',
21
- image_size: gr.inputs.Slider = 640
22
- ):
23
- """
24
- YOLOX inference function
25
- Args:
26
- image: Input image
27
- model_path: Path to the model
28
- config_path: Path to the config file
29
- image_size: Image size
30
- Returns:
31
- Rendered image
32
- """
33
-
34
- model = YoloxDetector(model_path, config_path=config_path, device="cpu", hf_model=True)
35
- pred = model.predict(image_path=image_path, image_size=image_size)
36
- return pred
37
-
38
-
39
- inputs = [
40
- gr.inputs.Image(type="filepath", label="Input Image"),
41
- gr.inputs.Dropdown(
42
- label="Model Path",
43
- choices=[
44
- "kadirnar/yolox_s-v0.1.1",
45
- "kadirnar/yolox_m-v0.1.1",
46
- "kadirnar/yolox_tiny-v0.1.1",
47
- ],
48
- default="kadirnar/yolox_s-v0.1.1",
49
- ),
50
- gr.inputs.Dropdown(
51
- label="Config Path",
52
- choices=[
53
- "configs.yolox_s",
54
- "configs.yolox_m",
55
- "configs.yolox_tiny",
56
- ],
57
- default="configs.yolox_s",
58
- ),
59
- gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
60
- ]
61
-
62
- outputs = gr.outputs.Image(type="filepath", label="Output Image")
63
- title = "YOLOX is a high-performance anchor-free YOLO."
64
-
65
- examples = [
66
- ["1.jpg", "kadirnar/yolox_m-v0.1.1", "configs.yolox_m", 640],
67
- ["2.jpg", "kadirnar/yolox_s-v0.1.1", "configs.yolox_s", 640],
68
- ["3.jpg", "kadirnar/yolox_tiny-v0.1.1", "configs.yolox_tiny", 640],
69
- ]
70
-
71
- demo_app = gr.Interface(
72
- fn=yolox_inference,
73
- inputs=inputs,
74
- outputs=outputs,
75
- title=title,
76
- examples=examples,
77
- cache_examples=True,
78
- theme='huggingface',
79
- )
80
- demo_app.launch(debug=True, enable_queue=True)
81
-
82
 
83
  COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
84
  [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
85
 
86
 
 
 
87
  def get_class_list_from_input(classes_string: str):
88
  if classes_string == "":
89
  return []
@@ -91,29 +28,6 @@ def get_class_list_from_input(classes_string: str):
91
  classes_list = [x.strip() for x in classes_list]
92
  return classes_list
93
 
94
- def infer(img, model_name: str, prob_threshold: int, classes_to_show = str):
95
- feature_extractor = AutoFeatureExtractor.from_pretrained(f"hustvl/{model_name}")
96
- model = YolosForObjectDetection.from_pretrained(f"hustvl/{model_name}")
97
-
98
- img = Image.fromarray(img)
99
-
100
- pixel_values = feature_extractor(img, return_tensors="pt").pixel_values
101
-
102
- with torch.no_grad():
103
- outputs = model(pixel_values, output_attentions=True)
104
-
105
- probas = outputs.logits.softmax(-1)[0, :, :-1]
106
- keep = probas.max(-1).values > prob_threshold
107
-
108
- target_sizes = torch.tensor(img.size[::-1]).unsqueeze(0)
109
- postprocessed_outputs = feature_extractor.post_process(outputs, target_sizes)
110
- bboxes_scaled = postprocessed_outputs[0]['boxes']
111
-
112
- classes_list = get_class_list_from_input(classes_to_show)
113
- res_img = plot_results(img, probas[keep], bboxes_scaled[keep], model, classes_list)
114
-
115
- return res_img
116
-
117
  def plot_results(pil_img, prob, boxes, model, classes_list):
118
  plt.figure(figsize=(16,10))
119
  plt.imshow(pil_img)
@@ -142,17 +56,81 @@ def fig2img(fig):
142
  img = Image.open(buf)
143
  return img
144
 
145
- image_in = gr.components.Image()
146
- image_out = gr.components.Image()
147
- model_choice = gr.components.Dropdown(["yolos-tiny", "yolos-small", "yolos-base", "yolos-small-300", "yolos-small-dwr"], value="yolos-small", label="YOLOS Model")
148
- prob_threshold_slider = gr.components.Slider(minimum=0, maximum=1.0, step=0.01, value=0.9, label="Probability Threshold")
149
- classes_to_show = gr.components.Textbox(placeholder="e.g. car, truck, traffic light", label="Classes to use (empty means all classes)")
150
 
151
- Iface = gr.Interface(
152
- fn=infer,
153
- inputs=[image_in,model_choice, prob_threshold_slider, classes_to_show],
154
- outputs=image_out,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  examples=examples,
156
- title="Object Detection with YOLOS",
157
- description=description,
158
- ).launch()
 
 
14
  torch.hub.download_url_to_file('https://s.rdrom.ru/1/pubs/4/35893/1906770.jpg', '2.jpg')
15
  torch.hub.download_url_to_file('https://static.mk.ru/upload/entities/2022/04/17/07/articles/detailPicture/5b/39/28/b6/ffb1aa636dd62c30e6ff670f84474f75.jpg', '3.jpg')
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
19
  [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
20
 
21
 
22
+
23
+
24
  def get_class_list_from_input(classes_string: str):
25
  if classes_string == "":
26
  return []
 
28
  classes_list = [x.strip() for x in classes_list]
29
  return classes_list
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def plot_results(pil_img, prob, boxes, model, classes_list):
32
  plt.figure(figsize=(16,10))
33
  plt.imshow(pil_img)
 
56
  img = Image.open(buf)
57
  return img
58
 
 
 
 
 
 
59
 
60
+
61
+ def inference(
62
+ image_path: gr.inputs.Image = None,
63
+ model_path: gr.inputs.Dropdown = 'kadirnar/yolox_s-v0.1.1',
64
+ image_size: gr.inputs.Slider = 640
65
+ ):
66
+
67
+ if model_name in ("yolox_s-v0.1.1", "yolox_m-v0.1.1", "yolox_tiny-v0.1.1"):
68
+ model = YoloxDetector(f"kadirnar/{model_name}", device="cpu", hf_model=True)
69
+ pred = model.predict(image_path=image_path, image_size=image_size)
70
+ return pred
71
+
72
+ else:
73
+ feature_extractor = AutoFeatureExtractor.from_pretrained(f"hustvl/{model_name}")
74
+ model = YolosForObjectDetection.from_pretrained(f"hustvl/{model_name}")
75
+
76
+ img = Image.fromarray(img)
77
+
78
+ pixel_values = feature_extractor(img, return_tensors="pt").pixel_values
79
+
80
+ with torch.no_grad():
81
+ outputs = model(pixel_values, output_attentions=True)
82
+
83
+ probas = outputs.logits.softmax(-1)[0, :, :-1]
84
+ keep = probas.max(-1).values > prob_threshold
85
+
86
+ target_sizes = torch.tensor(img.size[::-1]).unsqueeze(0)
87
+ postprocessed_outputs = feature_extractor.post_process(outputs, target_sizes)
88
+ bboxes_scaled = postprocessed_outputs[0]['boxes']
89
+
90
+ classes_list = get_class_list_from_input(classes_to_show)
91
+ res_img = plot_results(img, probas[keep], bboxes_scaled[keep], model, classes_list)
92
+
93
+ return res_img
94
+
95
+
96
+
97
+ inputs = [
98
+ gr.inputs.Image(type="filepath", label="Input Image"),
99
+ image_in,model_choice, prob_threshold_slider, classes_to_show
100
+ gr.inputs.Dropdown(
101
+ label="Model Path",
102
+ choices=[
103
+ "yolox_s-v0.1.1",
104
+ "yolox_m-v0.1.1",
105
+ "yolox_tiny-v0.1.1",
106
+ "yolos-tiny",
107
+ "yolos-small",
108
+ "yolos-base",
109
+ "yolos-small-300",
110
+ "yolos-small-dwr"
111
+ ],
112
+ default="kadirnar/yolox_s-v0.1.1",
113
+ ),
114
+ gr.inputs.Slider(minimum=0, maximum=1.0, step=0.01, value=0.9, label="Probability Threshold"),
115
+ gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
116
+ gr.inputs.Textbox(placeholder="e.g. car, truck, traffic light", label="Classes to use (empty means all classes)"),
117
+ ]
118
+
119
+ outputs = gr.outputs.Image(type="filepath", label="Output Image")
120
+
121
+ examples = [
122
+ ["1.jpg", "kadirnar/yolox_m-v0.1.1", 0.8, 640, ""],
123
+ ["2.jpg", "kadirnar/yolox_s-v0.1.1", 0.8, 640, ""],
124
+ ["3.jpg", "kadirnar/yolox_tiny-v0.1.1", 0.8, 640, ""],
125
+ ]
126
+
127
+ demo_app = gr.Interface(
128
+ fn=inference,
129
+ inputs=inputs,
130
+ outputs=outputs,
131
+ title="Object Detection with YOLO",
132
  examples=examples,
133
+ cache_examples=True,
134
+ theme='huggingface',
135
+ )
136
+ demo_app.launch(debug=True, enable_queue=True)