Q&A
現在convert.pyを実行するために画像データとテキストデータを作った状態でconvert.pyを実行しようとすると
['out19.csv', 'out11.csv', 'out16.csv', 'out17.csv', 'out12.csv', 'out13.csv', 'out18.csv', 'out14.csv', 'out15.csv', 'out10.csv', 'out6.csv', 'out2.csv', 'out9.csv', 'out7.csv', 'out5.csv', 'out3.csv', 'out1.csv', 'out8.csv', 'out4.csv', 'out0.csv', 'out26.csv', 'out27.csv', 'out28.csv', 'out23.csv', 'out24.csv', 'out22.csv', 'out21.csv', 'out25.csv', 'out20.csv']
Input:labels/stopsign_original/out19.csv
Output:labels/stopsign/out19.csv
47,103,470,581,tops
['47,103,470,581,tops\n']
Traceback (most recent call last):
File "convert.py", line 78, in <module>
xmax = elems[1]
IndexError: list index out of range
上記のようなエラーが出てしまいます。
解決策を教えてください。
お願いします。
これだけでは足りないと思うので解決する為に必要な情報があったら教えてください。
環境はgooglecolabです。
python
1convert.pyのコード 2```# -*- coding: utf-8 -*- 3""" 4Created on Wed Dec 9 14:55:43 2015 5This script is to convert the txt annotation files to appropriate format needed by YOLO 6@author: Guanghan Ning 7Email: gnxr9@mail.missouri.edu 8""" 9 10import os 11from os import walk, getcwd 12from PIL import Image 13 14classes = ["stopsign"] 15 16def convert(size, box): 17 dw = 1./size[0] 18 dh = 1./size[1] 19 x = (box[0] + box[1])/2.0 20 y = (box[2] + box[3])/2.0 21 w = box[1] - box[0] 22 h = box[3] - box[2] 23 x = x*dw 24 w = w*dw 25 y = y*dh 26 h = h*dh 27 return (x,y,w,h) 28 29 30"""-------------------------------------------------------------------""" 31 32""" Configure Paths""" 33mypath = "labels/stopsign_original/" 34outpath = "labels/stopsign/" 35 36cls = "stopsign" 37if cls not in classes: 38 exit(0) 39cls_id = classes.index(cls) 40 41wd = getcwd() 42list_file = open('%s/%s_list.txt'%(wd, cls), 'w') 43 44""" Get input text file list """ 45txt_name_list = [] 46for (dirpath, dirnames, filenames) in walk(mypath): 47 txt_name_list.extend(filenames) 48 break 49print(txt_name_list) 50 51""" Process """ 52for txt_name in txt_name_list: 53 # txt_file = open("Labels/stop_sign/001.txt", "r") 54 55 """ Open input text files """ 56 txt_path = mypath + txt_name 57 print("Input:" + txt_path) 58 txt_file = open(txt_path, "r") 59 lines = txt_file.read().split('\r\n') #for ubuntu, use "\r\n" instead of "\n" 60 61 """ Open output text files """ 62 txt_outpath = outpath + txt_name 63 print("Output:" + txt_outpath) 64 txt_outfile = open(txt_outpath, "w") 65 66 67 """ Convert the data to YOLO format """ 68 ct = 0 69 for line in lines: 70 #print('lenth of line is: ') 71 #print(len(line)) 72 #print('\n') 73 if(len(line) >= 2): 74 ct = ct + 1 75 print(line + "\n") 76 elems = line.split(' ') 77 print(elems) 78 xmin = elems[0] 79 xmax = elems[1] 80 ymin = elems[2] 81 ymax = elems[3] 82 # 83 img_path = str('%s/images/%s/%s.JPEG'%(wd, cls, os.path.splitext(txt_name)[0])) 84 #t = magic.from_file(img_path) 85 #wh= re.search('(\d+) x (\d+)', t).groups() 86 im=Image.open(img_path) 87 w= int(im.size[0]) 88 h= int(im.size[1]) 89 #w = int(xmax) - int(xmin) 90 #h = int(ymax) - int(ymin) 91 # print(xmin) 92 print(w, h) 93 b = (float(xmin), float(xmax), float(ymin), float(ymax)) 94 bb = convert((w,h), b) 95 print(bb) 96 txt_outfile.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') 97 98 """ Save those images with bb into list""" 99 if(ct != 0): 100 list_file.write('%s/images/%s/%s.JPEG\n'%(wd, cls, os.path.splitext(txt_name)[0])) 101 102list_file.close()
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2018/12/13 11:11