Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
|
| 5 |
+
# Load a pretrained YOLO model
|
| 6 |
+
model = YOLO('yolov8n.pt') # Or any other suitable YOLO model
|
| 7 |
+
|
| 8 |
+
def predict_image_class(input_img):
|
| 9 |
+
"""Predicts the class of objects in an image using the YOLO model."""
|
| 10 |
+
results = model.predict(source=input_img, conf=0.25) # Adjust confidence threshold as needed
|
| 11 |
+
|
| 12 |
+
# Process results and extract classes
|
| 13 |
+
predicted_classes = []
|
| 14 |
+
for r in results:
|
| 15 |
+
boxes = r.boxes
|
| 16 |
+
for box in boxes:
|
| 17 |
+
cls = model.names[int(box.cls)]
|
| 18 |
+
predicted_classes.append(cls)
|
| 19 |
+
|
| 20 |
+
return ", ".join(predicted_classes) # Return a string of comma-separated classes
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=predict_image_class,
|
| 24 |
+
inputs=gr.Image(type="filepath"), # Accept image filepaths as input
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="Image Class Prediction with YOLO",
|
| 27 |
+
description="Upload an image and get predictions of the object classes present."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
demo.launch(debug=True)
|