python IndexError:列表索引超出边界框

问题描述

由于某种原因,我的从图像中提取人脸功能有问题

一些面孔,但随后停止工作并打印错误

问题在于 x1,y1,width,height = results[0]['Box']

IndexError: 列表索引超出范围

这是我的功能

def extract_face(path_to_filename,detector,required_size=(160,160),save_faces=True):
    image = Image.open(path_to_filename)  # load image from file
    image = image.convert('RGB')  # convert image to RGB
    pixels = asarray(image)  # convert image to array
    results = detector.detect_faces(pixels)  # detect the face in image by MTCNN
    x1,height = results[0]['Box']  # extract the bounding Box from the face
    x1,y1 = abs(x1),abs(y1)  # avoid bug
    x2,y2 = x1 + width,y1 + height  # avoid bug
    face = pixels[y1:y2,x1:x2]  # extract the face
    image = Image.fromarray(face)  # convert image to its object
    image = image.resize(required_size)  # resize image
    if save_faces:
        path = os.path.split(os.path.abspath(path_to_filename))[0]  # get the path
        print(path)
        file_name = os.path.split(os.path.abspath(path_to_filename))[1]  # get file names
        print(path_to_filename)
        person_name = os.path.basename(os.path.normpath(Path(path)))  # get person name based on folder name

        print(person_name)

        project_folder = Path(path).parent.parent  # go to parent parent directory
        target_folder = os.path.join(project_folder,'faces_mini_dataset',person_name)  # set detected face in folder person name and images
        if not os.path.exists(target_folder):  # if not directory
            os.makedirs(target_folder)  # create directory
        target_face_file_path = os.path.join(target_folder,file_name)  # set detected face in folder person name and images
        print(target_face_file_path)
        image.save(target_face_file_path)  # save images in directory
    face_array = asarray(image)
    return face_array

meesgae 日志

/home/mani/Desktop/master/dip/final_project/dip_1/venv/bin/python /home/mani/Desktop/master/dip/final_project/dip_1/generate_faces_from_image.py 使用 TensorFlow 后端。正在加载图片... ['scarlett','zakir','obama'] 加载 Scarlett 我们将从 [ Scarlett ] 加载 [ 10 ] 个文件 class ...加载 zakir 我们将从 [ zakir ] 类加载 [ 10 ] 个文件 ... 加载 obama 我们将从 [ obama ] 类加载 [ 10 ] 个文件 ... 文件加载完成。加载数据集......-> 加载面-> 警告:张量流:来自 /home/mani/Desktop/master/dip/final_project/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:74: 名称 tf.get_default_graph 已弃用。请用 tf.compat.v1.get_default_graph 代替。

警告:张量流:来自 /home/mani/Desktop/master/dip/final_project/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:517: 名称 tf.placeholder 已弃用。请用 tf.compat.v1.placeholder 代替。

警告:张量流:来自 /home/mani/Desktop/master/dip/final_project/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:4138: 名称 tf.random_uniform 已弃用。请使用 tf.random.uniform 相反。

警告:张量流:来自 /home/mani/Desktop/master/dip/final_project/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3976: 名称 tf.nn.max_pool 已弃用。请使用 tf.nn.max_pool2d 相反。

警告:张量流:来自 /home/mani/Desktop/master/dip/final_project/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:174: 名称 tf.get_default_session 已弃用。请用 tf.compat.v1.get_default_session 代替。

警告:张量流:来自 /home/mani/Desktop/master/dip/final_project/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:181: 名称 tf.ConfigProto 已弃用。请用 tf.compat.v1.ConfigProto 代替。

提取人脸 /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett/ ..... /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett/contrast4_image.jpg 斯嘉丽 /home/mani/Desktop/master/dip/final_project/dip_1/faces_mini_dataset/scarlett/contrast4_image.jpg /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett/brightness3_image.jpg 斯嘉丽 /home/mani/Desktop/master/dip/final_project/dip_1/faces_mini_dataset/scarlett/brightness3_image.jpg /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett/img_blue_2.jpg 斯嘉丽 /home/mani/Desktop/master/dip/final_project/dip_1/faces_mini_dataset/scarlett/img_blue_2.jpg /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett/brightness4_image.jpg 斯嘉丽 /home/mani/Desktop/master/dip/final_project/dip_1/faces_mini_dataset/scarlett/brightness4_image.jpg /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett /home/mani/Desktop/master/dip/final_project/dip_1/persons_dataset/scarlett/img_blue_1.jpg 斯嘉丽 /home/mani/Desktop/master/dip/final_project/dip_1/faces_mini_dataset/scarlett/img_blue_1.jpg 回溯(最近一次通话):文件 "/home/mani/Desktop/master/dip/final_project/dip_1/generate_faces_from_image.py",第 90 行,在 面孔,标签 = generate_faces_from_images(persons_dataset_path) 文件 "/home/mani/Desktop/master/dip/final_project/dip_1/generate_faces_from_image.py",第 81 行,在 generate_faces_from_images 中 faces1 = extract_faces(path) 文件“/home/mani/Desktop/master/dip/final_project/dip_1/generate_faces_from_image.py”, 第 66 行,在 extract_faces 中 face =extract_face(path,save_faces=True) 文件“/home/mani/Desktop/master/dip/final_project/dip_1/generate_faces_from_image.py”, 第 30 行,在 extract_face 中 x1,height = results[0]['Box'] # 从人脸提取边界框 IndexError: list index out of range

进程以退出代码 1 结束

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)