Spaces:
Build error
Build error
Mnjar commited on
Commit ·
21b5ffe
1
Parent(s): 861f651
Add app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
|
| 5 |
+
# Load YOLO model
|
| 6 |
+
model = YOLO('TrashDetection/trash_detection.pt') # Ganti dengan path model YOLO Anda
|
| 7 |
+
|
| 8 |
+
def predict(image):
|
| 9 |
+
"""
|
| 10 |
+
Function to make predictions using YOLO model.
|
| 11 |
+
Args:
|
| 12 |
+
image (PIL.Image): Input image.
|
| 13 |
+
Returns:
|
| 14 |
+
List[List]: Predictions with labels, confidence, and bounding boxes.
|
| 15 |
+
"""
|
| 16 |
+
# Convert PIL image to numpy array
|
| 17 |
+
img = np.array(image)
|
| 18 |
+
|
| 19 |
+
# Get predictions from the model
|
| 20 |
+
results = model(img)
|
| 21 |
+
|
| 22 |
+
# Extract predictions
|
| 23 |
+
predictions = results.pandas().xyxy[0] # Pandas DataFrame of predictions
|
| 24 |
+
# Select necessary columns and convert to list
|
| 25 |
+
output = predictions[['name', 'confidence', 'xmin', 'ymin', 'xmax', 'ymax']].values.tolist()
|
| 26 |
+
|
| 27 |
+
return output
|
| 28 |
+
|
| 29 |
+
# Create Gradio interface
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=predict,
|
| 32 |
+
inputs=gr.Image(type="pil"), # Input image as PIL
|
| 33 |
+
outputs=gr.Dataframe(
|
| 34 |
+
headers=["Label", "Confidence", "Xmin", "Ymin", "Xmax", "Ymax"],
|
| 35 |
+
label="Predictions"
|
| 36 |
+
),
|
| 37 |
+
title="YOLO Object Detection",
|
| 38 |
+
description="Upload an image to detect objects using YOLO."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the app
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
iface.launch(share=True)
|