Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,19 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from sahi.prediction import ObjectPrediction
|
| 4 |
-
from sahi.utils.cv import visualize_object_predictions, read_image
|
| 5 |
-
from ultralyticsplus import YOLO
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
torch.hub.download_url_to_file('https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg', 'highway1.jpg')
|
| 10 |
-
torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
image: Input image
|
| 23 |
-
model_path: Path to the model
|
| 24 |
-
image_size: Image size
|
| 25 |
-
conf_threshold: Confidence threshold
|
| 26 |
-
iou_threshold: IOU threshold
|
| 27 |
-
Returns:
|
| 28 |
-
Rendered image
|
| 29 |
-
"""
|
| 30 |
-
model = YOLO(model_path)
|
| 31 |
-
model.conf = conf_threshold
|
| 32 |
-
model.iou = iou_threshold
|
| 33 |
-
results = model.predict(image, imgsz=image_size, return_outputs=True)
|
| 34 |
-
object_prediction_list = []
|
| 35 |
-
for _, image_results in enumerate(results):
|
| 36 |
-
if len(image_results)!=0:
|
| 37 |
-
image_predictions_in_xyxy_format = image_results['det']
|
| 38 |
-
for pred in image_predictions_in_xyxy_format:
|
| 39 |
-
x1, y1, x2, y2 = (
|
| 40 |
-
int(pred[0]),
|
| 41 |
-
int(pred[1]),
|
| 42 |
-
int(pred[2]),
|
| 43 |
-
int(pred[3]),
|
| 44 |
-
)
|
| 45 |
-
bbox = [x1, y1, x2, y2]
|
| 46 |
-
score = pred[4]
|
| 47 |
-
category_name = model.model.names[int(pred[5])]
|
| 48 |
-
category_id = pred[5]
|
| 49 |
-
object_prediction = ObjectPrediction(
|
| 50 |
-
bbox=bbox,
|
| 51 |
-
category_id=int(category_id),
|
| 52 |
-
score=score,
|
| 53 |
-
category_name=category_name,
|
| 54 |
-
)
|
| 55 |
-
object_prediction_list.append(object_prediction)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return output_image['image']
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
inputs = [
|
| 63 |
-
gr.inputs.Image(type="filepath", label="Input Image"),
|
| 64 |
-
gr.inputs.Dropdown(["kadirnar/yolov8n-v8.0", "kadirnar/yolov8m-v8.0", "kadirnar/yolov8l-v8.0", "ultralyticsplus/yolov8s", "Snearec/detectorMalezasYolo8"],
|
| 65 |
-
default="Snearec/detectorMalezasYolo8", label="Model"),
|
| 66 |
-
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
| 67 |
-
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
| 68 |
-
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
| 69 |
-
]
|
| 70 |
-
|
| 71 |
-
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
| 72 |
-
title = "Ultralytics YOLOv8: State-of-the-Art YOLO Models"
|
| 73 |
-
|
| 74 |
-
demo_app = gr.Interface(
|
| 75 |
-
fn=yolov8_inference,
|
| 76 |
-
inputs=inputs,
|
| 77 |
-
outputs=outputs,
|
| 78 |
-
title=title,
|
| 79 |
-
examples=examples,
|
| 80 |
-
cache_examples=True,
|
| 81 |
-
theme='huggingface',
|
| 82 |
-
)
|
| 83 |
-
demo_app.launch(debug=True, enable_queue=True)
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Cargar un modelo YOLOv8n preentrenado
|
| 6 |
+
model = YOLO('best.pt')
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def detect_objects(image: Image.Image):
|
| 9 |
+
# Realizar la inferencia
|
| 10 |
+
results = model(image)
|
| 11 |
+
|
| 12 |
+
# Obtener y mostrar los resultados
|
| 13 |
+
for r in results:
|
| 14 |
+
im_array = r.plot() # plot a BGR numpy array of predictions
|
| 15 |
+
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
|
| 16 |
+
return im # retornar la imagen con los objetos detectados
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Crear la interfaz de Gradio
|
| 19 |
+
gr.Interface(fn=detect_objects, inputs="image", outputs="image").launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|