vincentb25 commited on
Commit
d41ea92
·
1 Parent(s): 6b08156

Added imference time display

Browse files
Files changed (1) hide show
  1. yolo-inference-project/src/app.py +33 -13
yolo-inference-project/src/app.py CHANGED
@@ -2,17 +2,9 @@ import gradio as gr
2
  import os
3
  from model import load_model, perform_inference
4
  from utils import draw_bounding_boxes
5
-
6
- def inference(image, model_name, conf_thresh):
7
- if not model_name:
8
- raise gr.Error("No model selected. Please select a model.")
9
- if not image:
10
- raise gr.Error("No image provided. Please upload an image.")
11
-
12
- model = load_model("models/" + model_name)
13
- results = perform_inference(model, image)
14
- output_image = draw_bounding_boxes(results, conf_thresh)
15
- return output_image
16
 
17
  def get_model_names():
18
  model_dir = "models" # Update this path to your models directory
@@ -26,6 +18,31 @@ image_paths= [['examples/smaller_many_cans.jpg', 'yolo11m.pt', 0.5],
26
  ['examples/real_plank.jpg', 'yolo11m.pt', 0.5],
27
  ]
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  demo = gr.Interface(
30
  fn=inference,
31
  inputs=[
@@ -33,11 +50,14 @@ demo = gr.Interface(
33
  gr.Dropdown(choices=model_names, label="Select Model"),
34
  gr.Slider(minimum=0, maximum=1, step=0.01, value=0.5, label="Confidence Threshold")
35
  ],
36
- outputs=gr.Image(type="numpy", label="Output Image"),
 
 
 
37
  title="YOLO Model Inference",
38
  description="Select a YOLO model, upload an image, and set the confidence threshold to perform inference.",
39
  examples=image_paths,
40
- # flagging_mode="auto"
41
  )
42
 
43
  demo.launch()
 
2
  import os
3
  from model import load_model, perform_inference
4
  from utils import draw_bounding_boxes
5
+ import time
6
+ import subprocess
7
+ import re
 
 
 
 
 
 
 
 
8
 
9
  def get_model_names():
10
  model_dir = "models" # Update this path to your models directory
 
18
  ['examples/real_plank.jpg', 'yolo11m.pt', 0.5],
19
  ]
20
 
21
+ def inference(image, model_name, conf_thresh):
22
+ if not model_name:
23
+ raise gr.Error("No model selected. Please select a model.")
24
+ if not image:
25
+ raise gr.Error("No image provided. Please upload an image.")
26
+
27
+ model = load_model("models/" + model_name)
28
+ results = perform_inference(model, image)
29
+ output_image = draw_bounding_boxes(results, conf_thresh)
30
+ speed = results[0].speed
31
+ speed_str = "Speed: {:.1f}ms preprocess, {:.1f}ms inference, {:.1f}ms postprocess per image".format(
32
+ *tuple(speed.values()))
33
+ speed_str += "\nTotal : {:.1f}ms".format(sum(speed.values()))
34
+ command = "cat /proc/cpuinfo"
35
+
36
+ all_info = subprocess.check_output(command, shell=True).decode().strip()
37
+ for line in all_info.split("\n"):
38
+ if "model name" in line:
39
+ proc_name = re.sub( ".*model name.*:", "", line,1)
40
+ speed_str += "\nRunning on '" + proc_name + "'"
41
+ break
42
+
43
+ print(speed)
44
+ return output_image,speed_str
45
+
46
  demo = gr.Interface(
47
  fn=inference,
48
  inputs=[
 
50
  gr.Dropdown(choices=model_names, label="Select Model"),
51
  gr.Slider(minimum=0, maximum=1, step=0.01, value=0.5, label="Confidence Threshold")
52
  ],
53
+ outputs=[
54
+ gr.Image(type="numpy", label="Output Image"),
55
+ gr.Textbox(label="Inference Time")
56
+ ],
57
  title="YOLO Model Inference",
58
  description="Select a YOLO model, upload an image, and set the confidence threshold to perform inference.",
59
  examples=image_paths,
60
+ # flagging_mode="auto"
61
  )
62
 
63
  demo.launch()