face_recognition IndexError: list index out of range

再利用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)

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值