mkhug98 commited on
Commit
dd25b82
·
verified ·
1 Parent(s): 453592e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -1,26 +1,31 @@
1
  import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
- import torch
4
- from ultralytics import YOLO
5
 
6
- model = YOLO("best.onnx")
 
 
 
 
7
 
8
  # Function to perform object detection
9
  def detect_objects(image):
10
- # Perform inference with the YOLOv5 model
11
- results = model(image)
12
-
13
- # Get the detected bounding boxes and labels
14
- bboxes = results.xyxy[0].cpu().numpy()
15
- labels = results.names
 
16
 
17
  # Create a list of dictionaries for each detected object
18
  detections = []
19
- for bbox, label in zip(bboxes, labels):
20
- x1, y1, x2, y2, conf, cls = bbox
 
21
  detections.append({
22
  'label': label,
23
- 'confidence': float(conf),
24
  'x1': float(x1),
25
  'y1': float(y1),
26
  'x2': float(x2),
 
1
  import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
+ import onnxruntime
 
4
 
5
+ # Download the ONNX model from Hugging Face
6
+ model_path = hf_hub_download(repo_id="mkhug98/EchoYolo", filename="best.onnx")
7
+
8
+ # Load the ONNX model
9
+ session = onnxruntime.InferenceSession(model_path)
10
 
11
  # Function to perform object detection
12
  def detect_objects(image):
13
+ # Preprocess the image
14
+ image = image.resize((640, 640)) # Resize the image to the expected input size
15
+ input_data = image.transpose(2, 0, 1).numpy() # Rearrange the dimensions for ONNX
16
+
17
+ # Perform inference with the ONNX model
18
+ outputs = session.run(None, {"images": input_data.astype("float32")})
19
+ bboxes, scores, class_ids = outputs
20
 
21
  # Create a list of dictionaries for each detected object
22
  detections = []
23
+ for bbox, score, class_id in zip(bboxes[0], scores[0], class_ids[0]):
24
+ x1, y1, x2, y2 = bbox
25
+ label = session.get_modelmeta().custom_metadata_map["names"][int(class_id)]
26
  detections.append({
27
  'label': label,
28
+ 'confidence': float(score),
29
  'x1': float(x1),
30
  'y1': float(y1),
31
  'x2': float(x2),