Yolo环境配置总结(四)-Gradio平台

✅ 方案 1:设置文件编码为 UTF-8

在 Visual Studio 中:

  1. 打开文件 axx.py

  2. 点击菜单栏 文件 > 另存为

  3. 在保存对话框中点击右下角的 小三角 ▾,选择 另存为编码(Save with Encoding)

  4. 在编码选项中,选中 UTF-8(无 BOM)UTF-8

  5. 保存并重新运行程序。

或者,用记事本/VSCode 打开并另存为 UTF-8。


✅ 方案 2:在文件开头声明编码

如果你确定编辑器已经是 UTF-8,但系统仍然出错,可以尝试在文件顶部添加:

# -*- coding: utf-8 -*-

放在第一行或第二行位置:

# axx.py 
# -*- coding: utf-8 -*- 
import gradio as gr
...

✅ 方案 3:修复乱码字符串

将代码中出错的字符串修复,比如:

title="目标检测 Demo", # 替换乱码内容
import gradio as gr
from ultralytics import YOLO
from PIL import Image

# 加载预训练的 YOLO 模型
model = YOLO('yolo11n.pt')

def predict_image(image, conf_threshold, iou_threshold):
    # 使用模型进行推理
    results = model.predict(
        source=image, 
        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

# 定义 Gradio 接口
demo = gr.Interface(
    fn=predict_image,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
        gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
    ],
    outputs=gr.Image(type="pil", label="Result"),
    title="猫狗检测Demo",
    description="传一张带有猫狗的图像来进行推理。",
)

# 启动 Gradio 应用
if __name__ == "__main__":
    demo.launch()

 

posted @ 2025-06-20 16:13  多见多闻  阅读(65)  评论(0)    收藏  举报