labelme UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xaf in position 227: illegal mult

问题

在win10中直接安装labelme(pip install labelme),然后运行labelme,结果报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xaf in position 227: illegal multibyte sequence.

第一眼就感觉是编码问题,于是在github上面下载源码,准备在本地运行、调试,下载下来后,果然也报这个错误,下面是报错信息。

[INFO   ] __init__:get_config:71 - Loading config file from:C:\Users\xxx\.labelmerc
Traceback (most recent call last):
  File .... line 191, in <module>
    main()
  File ...., line 145, in main
    config = get_config(config_file_or_yaml, config_from_args)
  File "...\labelme-master\labelme\config\__init__.py", line 73, in get_config
    config_from_yaml = yaml.safe_load(f)
  File "D:\Program Files\Anaconda3\envs\py37\lib\site-packages\yaml\__init__.py", line 162, in safe_load
    return load(stream, SafeLoader)
  File "D:\Program Files\Anaconda3\envs\py37\lib\site-packages\yaml\__init__.py", line 112, in load
    loader = Loader(stream)
  File "D:\Program Files\Anaconda3\envs\py37\lib\site-packages\yaml\loader.py", line 34, in __init__
    Reader.__init__(self, stream)
  File "D:\Program Files\Anaconda3\envs\py37\lib\site-packages\yaml\reader.py", line 85, in __init__
    self.determine_encoding()
  File "D:\Program Files\Anaconda3\envs\py37\lib\site-packages\yaml\reader.py", line 124, in determine_encoding
    self.update_raw()
  File "D:\Program Files\Anaconda3\envs\py37\lib\site-packages\yaml\reader.py", line 178, in update_raw
    data = self.stream.read(size)
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 227: illegal multibyte sequence

Process finished with exit code 1

分析问题

根据提示,程序在加载这个C:\Users\xxx.labelmerc (请替换成自己的路径)文件时报错。部分文件如下:

flags: null
label_flags: null
labels: null
file_search: null
sort_labels: true
# 可控制该标注模式是否可用
validate_label: null

可以看到里面有中文。

解决方案

根据提示,可以发现是在读取配置信息时报错,所以找到config = get_config(config_file_or_yaml, config_from_args),点击get_config函数,修改里面

with open(config_from_yaml) as f:

改为:

with open(config_from_yaml, encoding='utf-8') as f:

以utf-8编码的形式读入该文件就不会报错了

        if not isinstance(config_from_yaml, dict):
            with open(config_from_yaml, encoding='utf-8') as f:
                logger.info(
                    "Loading config file from: {}".format(config_from_yaml)
                )
                config_from_yaml = yaml.safe_load(f)
        update_dict(
            config, config_from_yaml, validate_item=validate_config_item
        )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值