face_recognition IndexError: list index out of range

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014159143/article/details/82425393

再利用face_recognition做人脸识别的时候训练人脸图片时报错:

face_recognition IndexError: list index out of range

主要代码定位到:增加编码到训练集的face_recognition.face_encodings(...)

   #遍历训练集中每个数据集
   for class_dir in os.listdir(train_dir):
       if not os.path.isdir(os.path.join(train_dir,class_dir)):
           continue
       print "get:",class_dir
       
       #遍历每个目录下的每张照片
       for img_path in image_files_in_folder(os.path.join(train_dir,class_dir)):
           image = fr.load_image_file(img_path)
           boxes = fr.face_locations(image)
           
           #对于当前图片 增加编码到训练集
           X.append(fr.face_encodings(image,known_face_locations=boxes)[0])
           y.append(class_dir)

分析原因:

在追加数据到X中时,会将识别的图片编码,但是前提是能识别出人脸,否则编码数据则为空,这也为什么会报访问超出边界BUG,这里我们需要添加判断。

   #遍历训练集中每个数据集
   for class_dir in os.listdir(train_dir):
       if not os.path.isdir(os.path.join(train_dir,class_dir)):
           continue
       print "get:",class_dir
       
       #遍历每个目录下的每张照片
       for img_path in image_files_in_folder(os.path.join(train_dir,class_dir)):
           image = fr.load_image_file(img_path)
           boxes = fr.face_locations(image)
           
           #对于当前图片 增加编码到训练集
           encodings = fr.face_encodings(image,known_face_locations=boxes)
           
           if len(encodings) > 0:
                X.append(encodings[0])
                y.append(class_dir)
           else:
                print("No faces found in the image!")
           #X.append(fr.face_encodings(image,known_face_locations=boxes)[0])
           #y.append(class_dir)

猜你喜欢

转载自blog.csdn.net/u014159143/article/details/82425393