错误:“图块无法扩展到图像之外”

问题描述 投票:0回答:1

hell我的实验室用于人脸识别,我的数据是一个名为10_celebrity的文件夹,里面有10个包含图像的子文件夹。我构建了 2 个函数来提取面部和提取特征,我想将其应用于我的数据集并将其附加到下一步,但它运行了一段时间并出现错误 这是我的代码=>>

import tensorflow
import keras
from keras.preprocessing import image
import numpy as np
from tqdm import tqdm
from sklearn.metrics.pairwise import cosine_similarity
from PIL import Image
import cv2
from matplotlib import pyplot
from mtcnn import MTCNN
import os
import splitfolders
from sklearn.model_selection import train_test_split
import streamlit as st
from matplotlib import pyplot as plt

X = []
y = []

model = keras.applications.resnet50.ResNet50(
    include_top=False,
    input_shape=(224,224,3),
    pooling='avg'

detector = MTCNN()

def extract_face_from_image(img, detector):
    results = detector.detect_faces(img)
    if results:
        # Consider only the largest face detected
        largest_face_idx = np.argmax([result['box'][2] * result['box'][3] for result in results])
        x, y, width, height = results[largest_face_idx]['box']
        face = img[y:y+height,x:x+width]
        return face
    else:
        return None

def feature_extractor(image, model):    
    image = Image.fromarray(image)
    image = image.resize((224,224))
    face_array = np.asarray(image)
    face_array = face_array.astype('float32')
    expanded_img = np.expand_dims(face_array, axis=0)
    preprocessed_img = keras.applications.efficientnet.preprocess_input(expanded_img)
    result = model.predict(preprocessed_img).flatten()
    return result

dataset = "10_celebrity"

for folder in os.listdir(dataset):
    curr_path = os.path.join(dataset, folder)
    for file in os.listdir(curr_path):
        curr_file = os.path.join(curr_path, file)

        image = cv2.imread(curr_file)
        X.append(image)
        y.append(folder)

features_list = []

for file in X:
    face_image = extract_face_from_image(file, detector)
    if face_image is not None:
        features_list.append(feature_extractor(face_image, model))
    else:
        features_list.append(file)

这是错误=>>

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 6
      4 face_image = extract_face_from_image(file, detector)
      5 if face_image is not None:
----> 6     features_list.append(feature_extractor(face_image, model))
      7 else:
      8     features_list.append(file)

Cell In[6], line 2
      1 def feature_extractor(image, model):    
----> 2     image = Image.fromarray(image)
      3     image = image.resize((224,224))
      4     face_array = np.asarray(image)

File ~\AppData\Roaming\Python\Python312\site-packages\PIL\Image.py:3118, in fromarray(obj, mode)
   3115     else:
   3116         obj = obj.tostring()
-> 3118 return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)

File ~\AppData\Roaming\Python\Python312\site-packages\PIL\Image.py:3034, in frombuffer(mode, size, data, decoder_name, *args)
   3031         im.readonly = 1
   3032         return im
-> 3034 return frombytes(mode, size, data, decoder_name, args)
...
--> 804 d.setimage(self.im)
    805 s = d.decode(data)
    807 if s[0] >= 0:

ValueError: tile cannot extend outside image\

我尝试使用 if else 来提取面部,如果提取面部工作将其附加到列表

X
,如果不附加原始内容,但它仍然不起作用。我如何解决它 ?感谢您的阅读。

python image image-processing computer-vision face-recognition
1个回答
-1
投票

您可以分享您的数据集链接吗?

© www.soinside.com 2019 - 2024. All rights reserved.