Python 如何解决:内部错误:';numpy.ndarray和#x27;对象不可调用?
我有这个问题。我在FlaskAPI上运行此代码Python 如何解决:内部错误:';numpy.ndarray和#x27;对象不可调用?,python,numpy,Python,Numpy,我有这个问题。我在FlaskAPI上运行此代码 # face verification with the VGGFace2 model from matplotlib import pyplot from PIL import Image from numpy import asarray from scipy.spatial.distance import cosine from mtcnn.mtcnn import MTCNN from keras_vggface.vggface impor
# face verification with the VGGFace2 model
from matplotlib import pyplot
from PIL import Image
from numpy import asarray
from scipy.spatial.distance import cosine
from mtcnn.mtcnn import MTCNN
from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
# extract a single face from a given photograph
def extract_face(filename, required_size=(254, 254)):
# load image from file
pixels = pyplot.imread(filename)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
results = detector.detect_faces(pixels)
# extract the bounding box from the first face
x1, y1, width, height = results[0]['box']
x2, y2 = x1 + width, y1 + height
# extract the face
face = pixels[y1:y2, x1:x2]
# resize pixels to the model size
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = asarray(image)
# print(face_array)
return face_array
# extract faces and calculate face embeddings for a list of photo files
def get_embeddings(filenames):
# extract faces
faces = [extract_face(f) for f in filenames]
# convert into an array of samples
samples = asarray(faces, 'float32')
# prepare the face for the model, e.g. center pixels
samples = preprocess_input(samples, version=2)
# create a vggface model
model = VGGFace(model='vgg16', include_top=False, input_shape=(254, 254, 3), pooling='max')
# perform prediction
yhat = model.predict(samples)
return yhat
# determine if a candidate face is a match for a known face
def is_match(known_embedding, candidate_embedding, thresh=0.45):
# calculate distance between embeddings
score = cosine(known_embedding, candidate_embedding)
print('Match percentage (%.3f)' % (100 - (100 * score)))
print('>face is a Match (%.3f <= %.3f)' % (score, thresh))
# define filenames
filenames = ['audacious.jpg', 'face-20190717050545949130_123.jpg']
# get embeddings file filenames
embeddings = get_embeddings(filenames)
# define sharon stone
sharon_id = embeddings[0]
# verify known photos of sharon
print('Positive Tests')
is_match(embeddings[0], embeddings[1])
使用VGGFace2模型进行面部验证
从matplotlib导入pyplot
从PIL导入图像
从numpy进口asarray
从scipy.spatial.distance导入余弦
从mtcnn.mtcnn导入mtcnn
从keras_vggface.vggface导入vggface
从keras_vggface.utils导入预处理_输入
#从给定的照片中提取一张脸
def extract_face(文件名,所需_大小=(254,254)):
#从文件中加载图像
像素=pyplot.imread(文件名)
#使用默认权重创建检测器
检测器=MTCNN()
#检测图像中的人脸
结果=检测器。检测面(像素)
#从第一个面提取边界框
x1,y1,宽度,高度=结果[0]['box']
x2,y2=x1+宽度,y1+高度
#洗脸
面=像素[y1:y2,x1:x2]
#将像素调整为模型大小
image=image.fromarray(面)
image=image.resize(所需大小)
面_阵列=阵列(图像)
#打印(面阵列)
返回面数组
#提取面并计算照片文件列表的面嵌入
def get_嵌入(文件名):
#提取人脸
faces=[为文件名中的f提取_面(f)]
#转换为样本数组
样本=阵列(面“float32”)
#为模型准备面,例如中心像素
样本=预处理\输入(样本,版本=2)
#创建VGFace模型
model=VGGFace(model='vgg16',include_top=False,input_shape=(254,254,3),pooling='max')
#执行预测
yhat=模型预测(样本)
返回yhat
#确定候选人脸是否与已知人脸匹配
def是_匹配(已知_嵌入,候选_嵌入,阈值=0.45):
#计算嵌入件之间的距离
分数=余弦(已知嵌入、候选嵌入)
打印('Match percentage(%.3f)'(100-(100*score)))
打印('>面匹配(%.3f检查此行:
samples=asarray(面,'float32')
并尝试将其替换为:
samples=asarray(faces,dtype=np.float32)
我已经尝试过了,但不起作用。仍然有相同的错误,有什么线索吗?是的,我已经做了更改:samples=asarray(faces,dtype=np.float32),仍然在第二次我点击get-same-error-get\u嵌入:无法将提要dict键解释为张量:张量张量(“占位符:0”,shape=(3,3,3,64),dtype=float32)不是此图形的元素“numpy.ndarray”对象不可调用