GwFirman commited on
Commit
67cb61c
·
verified ·
1 Parent(s): e0da7bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -1,23 +1,47 @@
1
  import gradio as gr
2
- from ultralytics import YOLO
3
- from onnxruntime import InferenceSession
 
 
4
 
5
  # Muat model ONNX
6
- model_path = "best.onnx"
7
- session = InferenceSession(model_path)
8
- # # Muat model custom yang telah dilatih
9
- # model = YOLO("best.pt") # Pastikan file best.pt ada di direktori yang sama
10
 
11
  # Fungsi untuk melakukan prediksi
12
  def predict_image(img, conf_threshold, iou_threshold):
13
- results = model.predict(
14
- source=img,
15
- conf=conf_threshold,
16
- iou=iou_threshold,
17
- show_labels=True,
18
- show_conf=True,
19
- )
20
- return results[0].plot() if results else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Gradio Interface
23
  iface = gr.Interface(
@@ -28,8 +52,8 @@ iface = gr.Interface(
28
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"), # IoU threshold
29
  ],
30
  outputs=gr.Image(type="pil", label="Result"), # Output gambar dengan bounding box
31
- title="Ultralytics Gradio YOLO11 - Custom Model", # Judul aplikasi
32
- description="Upload images for custom YOLO11 object detection.", # Deskripsi aplikasi
33
  )
34
 
35
  # Luncurkan aplikasi
 
1
  import gradio as gr
2
+ import onnxruntime as ort
3
+ import numpy as np
4
+ from PIL import Image
5
+ import cv2
6
 
7
  # Muat model ONNX
8
+ onnx_model_path = "best.onnx" # Pastikan file best.onnx ada di direktori yang sama
9
+ session = ort.InferenceSession(onnx_model_path)
 
 
10
 
11
  # Fungsi untuk melakukan prediksi
12
  def predict_image(img, conf_threshold, iou_threshold):
13
+ # Konversi gambar ke format yang bisa diterima model ONNX
14
+ img = np.array(img)
15
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Ubah ke format BGR (OpenCV default)
16
+
17
+ # Preprocessing: Resize dan normalisasi
18
+ img_resized = cv2.resize(img, (640, 640)) # Sesuaikan dengan ukuran input model
19
+ img_normalized = img_resized / 255.0 # Normalisasi ke rentang 0-1
20
+ img_input = np.expand_dims(img_normalized, axis=0).astype(np.float32)
21
+
22
+ # Lakukan inferensi dengan ONNX Runtime
23
+ inputs = {session.get_inputs()[0].name: img_input}
24
+ outputs = session.run(None, inputs)
25
+
26
+ # Ambil hasil deteksi dan bounding box (misalnya, hasil berada di output[0])
27
+ boxes = outputs[0] # Sesuaikan dengan output yang relevan dari model ONNX
28
+ confidences = outputs[1] # Confidence score
29
+ labels = outputs[2] # Label kelas (jika ada)
30
+
31
+ # Filter prediksi dengan threshold
32
+ valid_boxes = []
33
+ for i, conf in enumerate(confidences):
34
+ if conf > conf_threshold:
35
+ valid_boxes.append(boxes[i])
36
+
37
+ # Plot hasil deteksi pada gambar
38
+ for box in valid_boxes:
39
+ x1, y1, x2, y2 = box
40
+ cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), 2)
41
+
42
+ # Kembalikan hasil sebagai gambar dengan bounding box
43
+ result_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
44
+ return result_img
45
 
46
  # Gradio Interface
47
  iface = gr.Interface(
 
52
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"), # IoU threshold
53
  ],
54
  outputs=gr.Image(type="pil", label="Result"), # Output gambar dengan bounding box
55
+ title="ONNX YOLO - Custom Model", # Judul aplikasi
56
+ description="Upload images for custom YOLO object detection using ONNX model.", # Deskripsi aplikasi
57
  )
58
 
59
  # Luncurkan aplikasi