简体   繁体   中英

Python OpenCV - Eigenfaces face recognition

I'm trying to create simple Eigenfaces face recognition app using Python and OpenCV. Unfortunately when I try to play app, then I got result: (-1, '\n', 1.7976931348623157e+308) , where -1 stands for not found and confidence... Is quite high...

Is there possibility to put by someone the most basic OpenCV implementation of Eigenfaces?

Here is my approach to the problem. I use Python2, as it is suggested in official documentation (due to some problems with 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 is another image of person with label = 0;

I would look at the sizing of the testImage. Also, I used a different sizing method than you used and got it working.

    face_resized = cv2.resize(img, (299, 299))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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