轴1超出了1维数组的范围

时间:2017-12-08 14:12:02

标签: python python-3.x python-2.7 face-detection

我在python中使用Face Recognition。我有一个以下程序来表彰我的个人资料。

import face_recognition

# Load some images to compare against
known_image = face_recognition.load_image_file("IMG_2051.JPG")

# Get the face encodings for the known images
face_encoding = face_recognition.face_encodings(known_image)[0]

# Load a test image and get encondings for it
image_to_test = face_recognition.load_image_file("manushi.jpg")
image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]

# See how far apart the test image is from the known faces
face_distances = face_recognition.face_distance(face_encoding, image_to_test_encoding)

for i, face_distance in enumerate(face_distances):
    print("The test image has a distance of {:.2} from known image #{}".format(face_distance, i))
    print("- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6))
    print("- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5))
    print()

但是,我收到了以下错误:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    face_distances = face_recognition.face_distance(face_encoding, image_to_test_encoding)
  File "/usr/local/lib/python2.7/dist-packages/face_recognition/api.py", line 70, in face_distance
    return np.linalg.norm(face_encodings - face_to_compare, axis=1)
  File "/usr/local/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line 2198, in norm
    return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))
numpy.core._internal.AxisError: axis 1 is out of bounds for array of dimension 1

请有人帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您的代码几乎是正确的,face_distance函数将数组作为第一个参数,因此face_encoding需要作为数组传递:

face_distances = face_recognition.face_distance([face_encoding], image_to_test_encoding)

在face_recognition github repo上提供的face_distance sample script中,您可以看到face_encoding的输入是一个数组。

我还测试了将face_encoding作为数组传递它是否有效。