2017-10-18 2 views
-1

私はYoloネットワーク上で自分のデータを訓練しようとしていますが、その前に境界ボックスの座標を必要なフォームに変換する必要があります。変換中、浮動小数点の無効なリテラル

0 
53 19 163 116 

と私はネットワークは、次のと連動し、フォームに変換しようとしている:

ファイルの内容は次のようにしています。

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

classes = ["stopsign"] 

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 = "/home/decentmakeover2/BBox-Label-Tool/Labels/002/" 
outpath = "/home/decentmakeover2/output/" 

cls = "stopsign" 
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.JPEG'%(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.JPEG\n'%(wd, cls, 
os.path.splitext(txt_name)[0])) 

list_file.close()   

と私はエラーを取得:

コードがある 最初のそれは私が本当にないんだけど

['0\n53', '19', '163', '116\n'] 
(262, 192) 
Traceback (most recent call last): 
    File "text.py", line 84, in <module> 
    b = (float(xmin), float(xmax), float(ymin), float(ymax)) 
ValueError: invalid literal for float(): 0 
53 

、その後、すべてのファイル名とデータの内容を出力しますここで何をすべきか確かめてください。

提案がありますか?

+2

明らかに '0 \ n53'は明白に浮動小数点に変換できません。入力データ形式を解析し直します。あなたは '\ r \ n 'で分割しますが、いくつかの' \ n'区切り文字もあなたのファイルにあります。あるいは、 '.splitlines()'メソッドがあります。 –

+0

チップをありがとう!、きれいに働いた! – Ryan

答えて

1

エラーメッセージに表示されているように、最初の語句は'0\n53'です。ここでは、'0'、次にである必要があります。フロートとして検出されることはありません。ちょうど'\n'で分割するとうまくいくはずです。

+0

私は申し訳ありませんが、exacltyは '\ n'で分割するはずです – Ryan

+0

ありがとうございます。 – Ryan

関連する問題