|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
from PIL import Image |
|
from ultralyticsplus import YOLO, render_result |
|
import cv2 |
|
import numpy as np |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
label = {0:'grenade', 1: 'knife', 2: 'pistol', 3: 'rifle'} |
|
|
|
def response2(image: gr.Image = None,image_size: gr.Slider = 640, conf_threshold: gr.Slider = 0.3, iou_threshold: gr.Slider = 0.6): |
|
|
|
model = YOLO('best (1).pt') |
|
|
|
results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size) |
|
|
|
box = results[0].boxes |
|
|
|
render = render_result(model=model, image=image, result=results[0], rect_th = 1, text_th = 1) |
|
|
|
text = "" |
|
|
|
for r in results: |
|
conf = r.boxes.conf |
|
cls = r.boxes.cls |
|
|
|
text += (f"Detected {label[int(cls)]]} with confidence {double(conf)}\n") |
|
|
|
return render,text |
|
|
|
|
|
inputs = [ |
|
gr.Image(type="filepath", label="Input Image"), |
|
gr.Slider(minimum=320, maximum=1280, value=640, |
|
step=32, label="Image Size"), |
|
gr.Slider(minimum=0.0, maximum=1.0, value=0.3, |
|
step=0.05, label="Confidence Threshold"), |
|
gr.Slider(minimum=0.0, maximum=1.0, value=0.6, |
|
step=0.05, label="IOU Threshold"), |
|
] |
|
|
|
|
|
outputs = [gr.Image( type="filepath", label="Output Image"), |
|
gr.Textbox() |
|
] |
|
|
|
title = "YOLOv8 Custom Object Detection by Uyen Nguyen" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface(fn=response2, inputs=inputs, outputs=outputs) |
|
iface.launch() |
|
|