成功解决.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])



相关文章
|
8月前
|
Python
Python报错ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Python报错ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
|
5月前
ES6的Array.from({length:N})方法创建长度为N的undefined数组,等价于 [...Array(N)]
ES6的Array.from({length:N})方法创建长度为N的undefined数组,等价于 [...Array(N)]
|
10月前
|
算法 IDE 开发工具
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
|
11月前
|
JavaScript API
Array.apply(null,{length: 99}) 逻辑解析
Array.apply(null,{length: 99}) 逻辑解析
55 0
在运行python程序时 self.adjacency_list.append([int(tempInfo[0])-1,int(tempInfo[1])-1]) 报错ValueError: inv
在运行python程序时 self.adjacency_list.append([int(tempInfo[0])-1,int(tempInfo[1])-1]) 报错ValueError: inv
在运行python程序时 self.adjacency_list.append([int(tempInfo[0])-1,int(tempInfo[1])-1]) 报错ValueError: inv
|
Python
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or
401 0
TypeError: cannot concatenate ‘str‘ and ‘list‘ objects
TypeError: cannot concatenate ‘str‘ and ‘list‘ objects
|
Linux Python
ValueError: empty range for randrange() (0, 0, 0)
ValueError: empty range for randrange() (0, 0, 0)