Opencv面部识别器,预测方法python

时间:2017-04-10 20:57:48

标签: python opencv

我正在尝试使用Opencv的eigen和fisher面孔在python中实现面部识别,我是opencv和python的完全新手。据我所知,我已经从OpenCv的文档中实现了它,但我认为我遗漏了一些东西。

我要做的是首先使用一组图像训练识别器。然后我读取这些图像并根据训练集预测它们。 我总是收到0的置信度值。这是我的训练代码。

recognizer=cv.createEigenFaceRecognizer();
def getImageName(iPath):
    for path, subdirs, files in os.walk(iPath):
        for name in files:
            imgPath.append(os.path.join(path, name))    
    #imgPath = [os.path.join(iPath,imgid) for imgid in os.listdir(iPath)]
    faceId=[]
    imgId=[]
    #tmpId=1
    for imagePath in imgPath:
        dataImg = Image.open(imagePath).convert('L')
        faceNp=np.array(dataImg,'uint8')
        getId=int(imagePath.split('/')[1].split('s')[1])
        faceId.append(faceNp)
        imgId.append(getId)
        cv.imshow("training",faceNp)
        cv.waitKey(10)
    return faceId, imgId

faces, imgIds = getImageName("yalefaces")
recognizer.train(faces,np.array(imgIds))
recognizer.save('recognizer/trainingData.yml')

和识别器

detector= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
rec=cv2.createEigenFaceRecognizer()
rec.load('recognizer/trainingData.yml')
imgId=0
imgPath=[]
total = 0
corr = 0
def getImageName(iPath):
    global total
    global corr
    for path, subdirs, files in os.walk(iPath):
        for name in files:
            imgPath.append(os.path.join(path, name))
    faceId=[]
    imgId=[]
    tmpId=1
    for imagePath in imgPath:
        dataImg = cv2.imread(imagePath,cv2.CV_LOAD_IMAGE_GRAYSCALE)
        total=total+1
        label,conf=rec.predict(dataImg)
        val=imagePath.split('/')[1].split('s')[1]
        print "predicted{}, confidence{}, val {}, path{}".format(label,conf,val,imagePath)
        if(int(label)==int(val)):
            corr=corr+1
        if(cv2.waitKey(1)==ord('q') or cv2.waitKey(1)==ord('e')):
            break;

getImageName("yalefaces")
print total
print corr
cv2.destroyAllWindows()

这是我输出的片段。虽然标签和图像编号(val)匹配,但置信度值始终为0.

predicted4, confidence0.0, val 4, pathyalefaces/s4/6.pgm predicted15, confidence0.0, val 15, pathyalefaces/s15/1.pgm

任何想法我错过了什么或任何在python中实现了fisher或eigen face的代码。提前谢谢!

1 个答案:

答案 0 :(得分:1)

实际上置信度= 0表示置信度为100%,即完全匹配。反之亦然。

$stmt = $dbh->prepare( $sql );
if ($stmt->execute( /* your array goes here */ )) {
       $rows = $stmt->fetchAll( PDO::FETCH_ASSOC );
       if ($rows !== FALSE){
                  print_r($rows);
        }
        else 
        if ( $stmt->rowCount() === 0) {
           echo "The info provided is unknown to the database";
        }
 }   

试试这个并结帐。

有关其他信息,请参阅"测试帧识别器"部分: http://hanzratech.in/2015/02/03/face-recognition-using-opencv.html

我希望它有所帮助。