代码
import gradio as gr
import pandas as pd
from ultralytics import YOLO
from skimage import data
from PIL import Image
model = YOLO('yolov8n-cls.pt')
def predict(img):
logging.info("Gradio 调用开始")
result = model.predict(source=img)
logging.info("Gradio 调用结束")
df = pd.Series(result[0].names).to_frame()
df.columns = ['names']
df['probs'] = result[0].probs.data.cpu().numpy()
df = df.sort_values('probs',ascending=False)
res = dict(zip(df['names'],df['probs']))
logging.info("Gradio 调用结束%s",res)
return res
gr.close_all()
demo = gr.Interface(fn = predict,inputs = gr.Image(type='pil'), outputs = gr.Label(num_top_classes=5),
examples = ['cat.jpeg','people.jpeg','coffee.jpeg'])
demo.launch()
界面
点击或者上传图片 点击提交 即可做到图片分类