繁体   English   中英

如何解决:internal_error: 'numpy.ndarray' 对象不可调用?

[英]How to solve : internal_error: 'numpy.ndarray' object is not callable?

我有这个问题。 我在烧瓶 api 上运行此代码

# 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])

我用第一次命中测试,这个过程运行良好。 但是当第二次命中时给出错误:

“numpy.ndarray”对象不可调用

'无法将 feed_dict 键解释为 Tensor:Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 64), dty pe=float32) 不是此图的元素。'

如果我不是在 API 上运行,只是在文件中运行:python3 file.py,任何时候我运行都不会给出任何错误任何线索?

检查这一行:

samples = asarray(faces, 'float32')

并尝试将其替换为:

samples = asarray(faces, dtype=np.float32)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM