YOLO_Detection / app.py
jamesthong's picture
Create app.py
2170970 verified
import gradio as gr
import PIL.Image as Image
from ultralytics import ASSETS, YOLO
model = YOLO("yolov8n.pt")
def predict_image(img, conf_threshold, iou_threshold):
"""Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
results = model.predict(
source=img,
conf=conf_threshold,
iou=iou_threshold,
show_labels=True,
show_conf=True,
imgsz=640,
)
for r in results:
im_array = r.plot()
im = Image.fromarray(im_array[..., ::-1])
return im
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Upload Image")
conf = gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold")
iou = gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
with gr.Row():
reset = gr.ClearButton([input_image])
submit = gr.Button("Submit")
with gr.Column():
output_image = gr.Image(type="pil", label="Result")
submit.click(fn=predict_image, inputs=[input_image, conf,iou], outputs=[output_image])
examples = gr.Examples(([
['https://ultralytics.com/images/zidane.jpg', 0.25, 0.45],
['https://unsplash.com/photos/2pPw5Glro5I/download?ixid=M3wxMjA3fDB8MXxzZWFyY2h8Mnx8dXJsfGVufDB8fHx8MTcyMTgwNzkyMnww&force=true', 0.5, 0.3],
['https://unsplash.com/photos/5CUyfyde_io/download?ixid=M3wxMjA3fDB8MXxzZWFyY2h8OHx8dG9reW98ZW58MHx8fHwxNzIxODY4MzQzfDA&force=true', 0.3, 0.3]
]),[input_image,conf,iou]),
if __name__ == "__main__":
demo.launch()