尝试从注释文件读取时将字符串转换为浮点值时获取值错误

2024-10-02 02:26:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在尝试为自己的数据集训练模型时,我遇到以下错误:

ValueError: could not convert string to float: '281.46762 316.82224 306.0123 270.65768 136.2258 160.62991 176.29044 151.88632 तब'
File "/Users/shwaitkumar/Downloads/EAST-master/icdar.py", line 609, in generator    
text_polys, text_tags = load_annoataion(txt_fn)

如何正确地将字符串转换为浮点? 以下是以txt格式加载注释的代码:

def load_annoataion(p):
    text_polys = []
    text_tags = []
    if not os.path.exists(p):
        return np.array(text_polys, dtype=np.float32)
    with open(p, 'r') as f:
        reader = csv.reader(f)
        for line in reader:
            label = line[-1]
            line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]
            x1, y1, x2, y2, x3, y3, x4, y4 = list(map(float, line[:8]))
            text_polys.append([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
            if label == '*' or label == '###':
                text_tags.append(True)
            else:
                text_tags.append(False)
        return np.array(text_polys, dtype=np.float32), np.array(text_tags, dtype=np.bool)

Tags: textinnplinetagsnotloadfloat
1条回答
网友
1楼 · 发布于 2024-10-02 02:26:56

您必须清理数据,但根据错误中的这一行,这里有一些东西可以帮助您解决问题

def floats_from_string(line):
    nums = []
    try:
        for num in line.split(" "):
            nums.append(float(num))  # <  cast your string to a float
    except ValueError:  # <- catch the possible non floats
        pass  # <  don't really need them so do nothing
    return nums # <- return  list of float

相关问题 更多 >

    热门问题