python - Python OpenCV - Eigenfaces 人脸识别
问题描述
我正在尝试使用 Python 和 OpenCV 创建简单的 Eigenfaces 人脸识别应用程序。不幸的是,当我尝试玩应用程序时,我得到了 result:
(-1, '\n', 1.7976931348623157e+308)
,其中 -1 代表 not found 和 confidence... 相当高...
是否有可能由某人提供最基本的 OpenCV 特征脸实现?
这是我解决问题的方法。我使用 Python2,正如官方文档中所建议的那样(由于 P3 的一些问题)。
import cv2 as cv
import numpy as np
import os
num_components = 10
threshold = 10.0
faceRecognizer = cv.face_EigenFaceRecognizer.create(num_components, threshold)
images = []
labels = []
textLabels = ["Person1", "Person2", "Person3"]
destinedIm = cv.imread("images/set1/1.jpg", cv.IMREAD_GRAYSCALE)
destinedSize = destinedIm.shape
#Person1
img = cv.imread("images/set1/1.jpg", cv.IMREAD_GRAYSCALE)
imResized = cv.resize(img, destinedSize)
images.append(imResized)
labels.append(0)
#In similar way I read total 8 images of set1 and 6 images of set2 (2 different people, with label 0 and 1 respectively)
cv.imwrite("images/set2/resized.jpg", imResized) #this doesn't work
numpyImages = np.array(images)
numpyLabels = np.array(labels)
# cv.face_FaceRecognizer.train(self=faceRecognizer, src=images, labels=labels)
faceRecognizer.train(src=images, labels=numpyLabels)
testImage = cv.imread("images/set1/testIm.jpg", cv.IMREAD_GRAYSCALE)
# cv.face_FaceRecognizer.predict()
resultLabel, resultConfidence = faceRecognizer.predict(testImage)
print (resultLabel, "\n" ,resultConfidence)
testImage 是另一个带有 label = 0 的人的图像;
解决方案
我会看看 testImage 的大小。此外,我使用了与您使用的不同的尺寸调整方法并使其正常工作。
face_resized = cv2.resize(img, (299, 299))
推荐阅读
- reactjs - ExpectSaga put 断言因多个 put 效果而失败
- java - Springboot findBy 列表
休眠@Query - android - 使用导航时如何不刷新列表?
- html - 使用 XPATH 在 div 中查找 div 中的文本
- apache-kafka-streams - 从转换中的数据更新全局存储
- c# - 使用 Vision 框架 NSInvalidArgumentException 的 xamarin iOS 人脸检测
- r - 转置一个df,每行作为R中的一列
- javascript - 根据子字符串选择 ID
- c++ - `cin.clear()` 使输入流处于失败状态
- git - 如何在 git 日志中查询 Co-Authored-By