解决yolov5中文显示乱码问题

第一种:训练自己的数据集支持中文,解决办法

yolov5/utils/general.py - > check_dataset(data, autodownload=True)方法下的 

def check_dataset(data, autodownload=True):
    # Download and/or unzip dataset if not found locally
    # Usage: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128_with_yaml.zip

    # Download (optional)
    extract_dir = ''
    if isinstance(data, (str, Path)) and str(data).endswith('.zip'):  # i.e. gs://bucket/dir/coco128.zip
        download(data, dir=DATASETS_DIR, unzip=True, delete=False, curl=False, threads=1)
        data = next((DATASETS_DIR / Path(data).stem).rglob('*.yaml'))
        extract_dir, autodownload = data.parent, False

    # Read yaml (optional)
    if isinstance(data, (str, Path)):
        # with open(data, errors='ignore') as f: #原代码
        with open(data, errors='ignore',encoding='UTF-8') as f: #新代码添加encoding='UTF-8'用于支持中文编码
            data = yaml.safe_load(f)  # dictionary

    # Resolve paths
    path = Path(extract_dir or data.get('path') or '')  # optional 'path' default to '.'
    if not path.is_absolute():
        path = (ROOT / path).resolve()
    for k in 'train', 'val', 'test':
        if data.get(k):  # prepend path
            data[k] = str(path / data[k]) if isinstance(data[k], str) else [str(path / x) for x in data[k]]

    # Parse yaml
    assert 'nc' in data, "Dataset 'nc' key missing."
    if 'names' not in data:
        data['names'] = [f'class{i}' for i in range(data['nc'])]  # assign class names if missing
    train, val, test, s = (data.get(x) for x in ('train', 'val', 'test', 'download'))

 第二种:训练完自己的数据集后,使用权重文件 weights/best.pt 验证时,标签显示中文乱码的解决办法

        1)单独创建以个py文件,并写入代码,运行

import torch
#%%
ckpt1 = torch.load('runs\\weights\\best.pt')

        2)在python控制台将显示ckpt1变量信息

 3)写代码修改乱码为正常显示中文并保存

import torch

# %%
ckpt1 = torch.load('runs\\weights\\best.pt')

ckpt1['model'].names = ['电动车']  # 修改乱码为正常中文显示

torch.save(ckpt1, 'runs\\weights\\best.pt')  # 重新保存文件

4)再次打开权重文件就显示正常中文了

5)测试 python detect.py --source data/images --weights runs/weights/best.pt

 

  • 12
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

华尔街小贩

轻研究重应用

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值