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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -56
app.py CHANGED
@@ -1,60 +1,40 @@
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(
48
- fn=predict_image,
49
- inputs=[
50
- gr.Image(type="pil", label="Upload Image"), # Input gambar
51
- gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"), # Confidence threshold
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
60
- iface.launch()
 
 
1
+ import torch
 
 
2
  from PIL import Image
3
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Load trained model
6
+ model = torch.load("best.pt", map_location=torch.device("cpu"))
7
+ model.eval()
8
+
9
+ # Function to process image
10
+ def predict(image):
11
+ # Convert to PIL image if needed
12
+ if not isinstance(image, Image.Image):
13
+ image = Image.fromarray(image)
14
+
15
+ # Preprocess image (adjust as per your model's requirements)
16
+ results = model([image]) # Assuming YOLOv11 inference works like this
17
+ detections = results.xyxy[0].numpy() # Extract bounding boxes, scores, etc.
18
+
19
+ # Draw boxes on image
20
+ for box in detections:
21
+ x1, y1, x2, y2, conf, cls = box
22
+ label = f"Class {int(cls)}: {conf:.2f}"
23
+ draw = ImageDraw.Draw(image)
24
+ draw.rectangle([(x1, y1), (x2, y2)], outline="red", width=3)
25
+ draw.text((x1, y1), label, fill="red")
26
+
27
+ return image
28
+
29
+ # Gradio interface
30
+ interface = gr.Interface(
31
+ fn=predict,
32
+ inputs=gr.Image(type="pil"),
33
+ outputs="image",
34
+ title="YOLOv11 Object Detection",
35
+ description="Upload an image to detect objects using YOLOv11.",
36
  )
37
 
38
+ # Launch app
39
+ if __name__ == "__main__":
40
+ interface.launch()