【问题标题】:'cv2.face_BasicFaceRecognizer' object has no attribute 'getParams' Python'cv2.face_BasicFaceRecognizer' 对象没有属性 'getParams' Python
【发布时间】:2016-01-21 01:31:43
【问题描述】:

我在使用 OpenCV 和 Python 创建人脸识别系统时遇到了一些问题。我试图使用 Philipp Wagner 提供的文档,我有以下代码:

import os
import sys
import cv2
import numpy as np

def normalize(X, low, high, dtype=None):
    """Normalizes a given array in X to a value between low and high."""
    X = np.asarray(X)
    minX, maxX = np.min(X), np.max(X)
    # normalize to [0...1].
    X = X - float(minX)
    X = X / float((maxX - minX))
    # scale to [low...high].
    X = X * (high-low)
    X = X + low
    if dtype is None:
        return np.asarray(X)
    return np.asarray(X, dtype=dtype)


def read_images(path, sz=None):
    """Reads the images in a given folder, resizes images on the fly if size is given.
    Args:
        path: Path to a folder with subfolders representing the subjects (persons).
        sz: A tuple with the size Resizes
    Returns:
        A list [X,y]

            X: The images, which is a Python list of numpy arrays.
            y: The corresponding labels (the unique number of the subject, person) in a Python list.
    """
    c = 0
    X,y = [], []
    for dirname, dirnames, filenames in os.walk(path):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                try:
                    im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
                # resize to given size (if given)
                    if (sz is not None):
                        im = cv2.resize(im, sz)
                    X.append(np.asarray(im, dtype=np.uint8))
                    y.append(c)
                except IOError, (errno, strerror):
                    print "I/O error({0}): {1}".format(errno, strerror)
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    raise
            c = c+1
    return [X,y]

if __name__ == "__main__":
    out_dir = None

    if len(sys.argv) < 2:
        print "USAGE: facerec_demo.py </path/to/images> [</path/to/store/images/at>]"
        sys.exit()

    [X,y] = read_images(sys.argv[1])

    y = np.asarray(y, dtype=np.int32)
    # If a out_dir is given, set it:
    if len(sys.argv) == 3:
        out_dir = sys.argv[2]

    model = cv2.face.createEigenFaceRecognizer()
    model.train(np.asarray(X), np.asarray(y))
    model.save('individual.xml')

    [p_label, p_confidence] = model.predict(np.asarray(X[0]))
    # Print it:
    print "Predicted label = %d (confidence=%.2f)" % (p_label, p_confidence)

    print model.getParams()
    # Now let's get some data:
    mean = model.getMat("mean")
    eigenvectors = model.getMat("eigenvectors")
    # We'll save the mean, by first normalizing it:
    mean_norm = normalize(mean, 0, 255, dtype=np.uint8)
    mean_resized = mean_norm.reshape(X[0].shape)
    if out_dir is None:
        cv2.imshow("mean", mean_resized)
    else:
        cv2.imwrite("%s/mean.png" % (out_dir), mean_resized)
    for i in xrange(min(len(X), 16)):
        eigenvector_i = eigenvectors[:,i].reshape(X[0].shape)
        eigenvector_i_norm = normalize(eigenvector_i, 0, 255, dtype=np.uint8)
        if out_dir is None:
            cv2.imshow("%s/eigenface_%d" % (out_dir,i), eigenvector_i_norm)
        else:
            cv2.imwrite("%s/eigenface_%d.png" % (out_dir,i), eigenvector_i_norm)

    if out_dir is None:
        cv2.waitKey(0)

但它让我收到以下错误:

print model.getParams()
AttributeError: 'cv2.face_BasicFaceRecognizer' object has no attribute   'getParams'

知道为什么我不能得到任何参数吗?我认为这可能是因为子模块 cv2.face 的合并,因此它可能是 model.getParams() 和 getMat() 的替代方案,但我只是猜测...... 提前致谢。

【问题讨论】:

标签: python opencv parameters face-recognition eigenvector


【解决方案1】:

也许为时已晚,但我就是这么做的。

首先,查看支持您的 cv2.face 的方法列表

model = cv2.face.createEigenFaceRecognizer ()
help (model)

您会注意到,有些更改不再使用:model.getMat ("mean") 现在只使用mean = model.getMean()

希望对你有帮助:)

【讨论】:

  • 谢谢!现在还为时不晚,因为其他人正在尝试使用此代码。
猜你喜欢
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 2014-05-08
  • 2020-09-19
  • 1970-01-01
  • 2020-06-04
  • 2019-08-14
相关资源
最近更新 更多