ValueError:无法将字符串转换为浮点型:'1 \ n18'

时间:2019-07-02 06:36:43

标签: python annotations yolo

我有一个名为“ convert.py”的python文件。该文件应该将txt注释文件转换为YOLO所需的适当格式。 我将图像保存在为什么存在“ convert.py”的同一文件夹中,尽管原始文件和编译“ convert.py”后将创建的文件位于同一驱动器中,但位于不同的文件夹中。

这是我的convert.py代码

import os
from os import walk, getcwd
from PIL import Image

classes = ["helmet"]

def convert(size, box):
    dw = 1./size[0]
    dh = 1./size[1]
    x = (box[0] + box[1])/2.0
    y = (box[2] + box[3])/2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)


"""-------------------------------------------------------------------""" 

""" Configure Paths"""   
mypath = "F:/Labels/helmet_original/"
outpath = "F:/Labels/helmet/"

cls = "helmet"
if cls not in classes:
    exit(0)
cls_id = classes.index(cls)

wd = getcwd()
list_file = open('%s/%s_list.txt'%(wd, cls), 'w')

""" Get input text file list """
txt_name_list = []
for (dirpath, dirnames, filenames) in walk(mypath):
    txt_name_list.extend(filenames)
    break
print(txt_name_list)

""" Process """
for txt_name in txt_name_list:
    # txt_file =  open("Labels/stop_sign/001.txt", "r")

    """ Open input text files """
    txt_path = mypath + txt_name
    print("Input:" + txt_path)
    txt_file = open(txt_path, "r")
    lines = txt_file.read().split('\r\n')   #for ubuntu, use "\r\n" instead of "\n"

    """ Open output text files """
    txt_outpath = outpath + txt_name
    print("Output:" + txt_outpath)
    txt_outfile = open(txt_outpath, "w")


    """ Convert the data to YOLO format """
    ct = 0
    for line in lines:
        #print('lenth of line is: ')
        #print(len(line))
        #print('\n')
        if(len(line) >= 2):
            ct = ct + 1
            print(line + "\n")
            elems = line.split(' ')
            print(elems)
            xmin = elems[0]
            xmax = elems[2]
            ymin = elems[1]
            ymax = elems[3]
            #
            img_path = str('%s/images/%s/%s.jpg'%(wd, cls, os.path.splitext(txt_name)[0]))
            #t = magic.from_file(img_path)
            #wh= re.search('(\d+) x (\d+)', t).groups()
            im=Image.open(img_path)
            w= int(im.size[0])
            h= int(im.size[1])
            #w = int(xmax) - int(xmin)
            #h = int(ymax) - int(ymin)
            # print(xmin)
            print(w, h)
            b = (float(xmin), float(xmax), float(ymin), float(ymax))
            bb = convert((w,h), b)
            print(bb)
            txt_outfile.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

    """ Save those images with bb into list"""
    if(ct != 0):
        list_file.write('%s/images/%s/%s.jpg\n'%(wd, cls, os.path.splitext(txt_name)[0]))

list_file.close()

编译文件后出现此错误:- 追溯(最近一次通话):   文件“ convert.py”,第92行,在     b =(float(xmin),float(xmax),float(ymin),float(ymax)) ValueError:无法将字符串转换为浮点型:'1 \ n18'

请帮助我了解我要去哪里。

1 个答案:

答案 0 :(得分:0)

您在 public static int findLongest(List<Integer> hours, int limit) { int longWork = 0; int shortWork = 0; Set<Integer> hash_set = new HashSet<Integer>(); for(int i=0 ; i<hours.size() ; i++) { if(hours.get(i) >= limit) { longWork++ ; hash_set.add(shortWork); shortWork = 0; } else if(hours.get(i) < limit) { shortWork++ ; hash_set.add(longWork); longWork = 0; } } int finalAns = Collections.max(hash_set); return finalAns ; } } 处分割了行,但是显然'\r\n'仅在文件中将它们分隔开,因此您在ymax中获得了换行符,这会导致转换错误。 试试

'\n'

而不是明确指出行定界符。