Ask Your Question
1

Using trained file for predictions

asked 2017-08-13 14:21:00 -0500

asciime gravatar image

updated 2017-08-13 15:04:45 -0500

Hello, I am trying to create a way to recognize faces. In one file the code generates a trainner.yml. Then in another file I am trying to load the file so then it can (when it detects there is a face) recognize the face and give the id. Here is the trainer code:

import cv2,os
import numpy as np
from PIL import Image

recognizer = cv2.face.LBPHFaceRecognizer_create()
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

def getImagesAndLabels(path):
    #get the path of all the files in the folder
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)] 
    #create empth face list
    faceSamples=[]
    #create empty ID list
    Ids=[]
    #now looping through all the image paths and loading the Ids and the images
    for imagePath in imagePaths:
        #loading the image and converting it to gray scale
        pilImage=Image.open(imagePath).convert('L')
        #Now we are converting the PIL image into numpy array
        imageNp=np.array(pilImage,'uint8')
        #getting the Id from the image
        Id=int(os.path.split(imagePath)[-1].split(".")[1])
        # extract the face from the training image sample
        faces=detector.detectMultiScale(imageNp)
        #If a face is there then append that in the list as well as Id of it
        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)
    return faceSamples,Ids

faces,Ids = getImagesAndLabels('dataSet')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainner/trainner.yml')

Here is the code that tries to recognize the face:

import cv2
import numpy as np

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.load('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)


cam = cv2.VideoCapture(0)
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.2,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        if(conf<50):
            if(Id==1):
                Id="asciime"
            elif(Id==2):
                Id="Something"
        else:
            Id="Unknown"
        cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
        cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)
    cv2.imshow('im',im) 
    if cv2.waitKey(10) & 0xFF==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

The error that I am getting is:

File "faceRecog.py", line 5, in <module>
 recognizer.load('trainner/trainner.yml')
 AttributeError: 'cv2.face_LBPHFaceRecognizer' object has no attribute 'load'

I am using python 3, and openCV 3.3.

I figured this out. Now because I'm a new user I need to wait until tomorrow - No need to find this out.

edit retag flag offensive close merge delete

Comments

and, what happens ?

berak gravatar imageberak ( 2017-08-13 14:23:12 -0500 )edit

'3.3.0'

When I was installing this on my computer, I accidentally made the import cv2, not cv3

asciime gravatar imageasciime ( 2017-08-13 14:37:59 -0500 )edit

it's still cv2

berak gravatar imageberak ( 2017-08-13 14:39:04 -0500 )edit

So how would I change it to cv3?

asciime gravatar imageasciime ( 2017-08-13 14:41:01 -0500 )edit

there is no "load()" method in the (opencv 3.3) interface.

also see here, similar problem in java

berak gravatar imageberak ( 2017-08-13 14:41:36 -0500 )edit

there is no such thing as cv3

berak gravatar imageberak ( 2017-08-13 14:42:23 -0500 )edit

How would I load the .yml file into the program?

asciime gravatar imageasciime ( 2017-08-13 14:42:58 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-11-06 12:16:49 -0500

leto96 gravatar image

Change:

recognizer.load('trainner/trainner.yml')

to:

recognizer.read('trainner/trainner.yml')
edit flag offensive delete link more

Comments

Yes, man, yes.

natanMaia gravatar imagenatanMaia ( 2017-11-30 12:00:07 -0500 )edit

after doing this i had got a new error cv2.error: C:\projects\opencv-python\opencv_contrib\modules\face\src\facerec.cpp:61: error: (-2) File can't be opened for reading! in function cv::face::FaceRecognizer::read

sumanth alapati gravatar imagesumanth alapati ( 2017-12-28 09:31:17 -0500 )edit
0

answered 2017-08-20 21:58:55 -0500

Prakash gravatar image

in Opencv 3.3 syntax is changed.
You need to pass the path of your trained ".yml" file in the constructor. like shown here
I don't have python opencv installed so i'm not sure about python syntax

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-08-13 14:21:00 -0500

Seen: 5,746 times

Last updated: Aug 20 '17