0

我是unis的学生junho。我试图研究此代码以进行图像预处理。我试图将面部识别更改为标签识别以正确裁剪。但错误发生如下代码。这似乎是列表问题。但即使我尝试过,我也无法弄清楚。有没有人能知道这个问题??

这是代码:

global_label_index = 0 
global_label_number = [0 for x in range(1000)]
global_image_hash = []



def skew_angle(self):
    return None


# read files in src_dir and crop label only and write it into des_dir
def crop_labels_dir(self,src_dir,des_dir,maxnum):

    # training data will be written in $des_dir/training
    # validation data will be written in $des_dir/validate

    des_dir_training = os.path.join(des_dir,'training')
    des_dir_validate = os.path.join(des_dir,'validate')

    if not os.path.exists(des_dir):
        os.makedirs(des_dir)
    if not os.path.exists(des_dir_training):
        os.makedirs(des_dir_training)
    if not os.path.exists(des_dir_validate):
        os.makedirs(des_dir_validate)

    path,folder_name = os.path.split(src_dir)
    label = folder_name

    # create label file. it will contains file location 
    # and label for each file
    training_file = open(des_dir+'/training_file.txt','a')
    validate_file = open(des_dir+'/validate_file.txt','a')

    files = self.getfiles(src_dir)
    global global_label_index
    cnt = 0 
    num = 0 # number of training data
    for f in files:
        rect = self.detect_label(f)

        # replace ',' in file name to '.'
        # because ',' is used for deliminator of image file name and its label
        des_file_name = os.path.basename(f)
        des_file_name = des_file_name.replace(',','_')

        if rect != None:
            # 70% of file will be stored in training data directory
            if(cnt < 8):
                des_file = os.path.join(des_dir_training,des_file_name)
                # if we already have duplicated image, crop_face will return None
                if self.crop_face(f, rect, des_file ) != None:
                    training_file.write("%s,%s,%d\n"%(des_file,label,global_label_index) )
                    num = num + 1
                    global_label_number[global_label_index] = num
                    cnt = cnt+1

                if (num>=maxnum):
                    break
            # 30% of files will be stored in validation data directory
            else: # for validation data
                des_file = os.path.join(des_dir_validate,des_file_name)
                if self.crop_face(f, rect, des_file) != None:
                    validate_file.write("%s,%s,%d\n"%(des_file,label,global_label_index) )
                    cnt = cnt+1

            if(cnt>9): 
                cnt = 0
    #increase index for image label

    global_label_index = global_label_index + 1 
    print('## label %s has %s of training data' %(global_label_index,num))
    training_file.close()
    validate_file.close()


def getdirs(self,dir):
    dirs = []
    for f in os.listdir(dir):
        f=os.path.join(dir,f)
        if os.path.isdir(f):
            if not f.startswith('.'):
                dirs.append(f)

    return dirs


def crop_labels_rootdir(self,src_dir,des_dir,maxnum):
    # crop file from sub-directoris in src_dir
    dirs = self.getdirs(src_dir)

    #list sub directory
    for d in dirs:
        print('[INFO] : ### Starting cropping in directory %s ###'%d)
        self.crop_labels_dir(d, des_dir,maxnum)
    #loop and run face crop
    global global_label_number
    print("number of datas per label ", global_label_number)


def main(argv):
    srcdir= argv[1]
    desdir = argv[2]
    maxnum = int(argv[3])

    detector = LabelDetector()

    detector.crop_labels_rootdir(srcdir, desdir,maxnum)


if __name__ == "__main__":
    main(sys.argv)

错误代码是这样的: IndexError Traceback (last recent call last) in () 17 18 if name == " main ": ---> 19 main(sys.argv)

<ipython-input-32-ab2ad6c296c9> in main(argv)
      6     srcdir= argv[1]
      7     desdir = argv[2]
----> 8     maxnum = int(argv[3])
      9 
     10     detector = LabelDetector()

IndexError: list index out of range
4

0 回答 0