解决报错:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xab in position 105: illegal multibyte seque

解决报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xab in position 105: illegal multibyte sequence

目标检测YOLO系列转数据集格式的时候报错如下:
在这里插入图片描述
最后发现是编码器的问题,解决方法如下:

def convert_annotation(image_id):
    in_file = open('data/Annotations/%s.xml' % (image_id), 'r', encoding="UTF-8")
    out_file = open('data/labels/%s.txt' % (image_id), 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

在in_file = open(‘data/Annotations/%s.xml’ % (image_id)后加上 ‘r’, encoding=“UTF-8”。
再次运行代码,报错已经解决。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/magicnnonn/article/details/124342022