成功解决.append方法出现错误IndexError: list index out of range

成功解决.append方法出现错误IndexError: list index out of range

目录

解决问题

解决方法


解决问题

.append方法出现错误IndexError: list index out of range

解决方法

X.append(fr.face_encodings(image,known_face_locations=boxes)[0])
print(fr.face_encodings(image,known_face_locations=boxes)),

发现有张图片输出的fr.face_encodings(image,known_face_locations=boxes)的列表为  [ ]  即是空的,所以出现了list index out of range,

T1、只需要删除那张图片即可!

T2、改写函数,增加判断,如果当前数值列表为空,则跳出循环执行下一次循环

        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)
            print(img_path)
#             print(fr.face_encodings(image,known_face_locations=boxes))
            
            if len(fr.face_encodings(image,known_face_locations=boxes))==0:   #判断空列表
                print("这张图片不合法,请删除!即将退出程序",img_path)
                continue
#                 sys.exit()
            # 对于当前图片,增加编码到训练集,返回128个值放到X集合里(X.append)
            X.append(fr.face_encodings(image,known_face_locations=boxes)[0])
发布了1605 篇原创文章 · 获赞 6377 · 访问量 1258万+

猜你喜欢

转载自blog.csdn.net/qq_41185868/article/details/80945291