Ask Your Question

Giving Arbitrary value in Face ID in face Verification from webcam

asked 2018-08-13 21:38:29 -0500

updated 2019-12-19 11:32:13 -0500

supra56 gravatar image

Hello, i am facing problem while recognizing unknown faces. Code works fine on known face but when i give unknown face it is giving true label when it false. also thereafter kmown face ID is getting changed every time

Everything working fine on my face. but problem occurs while i am testing it on faces which was not trained. below is code i am using for comparing the image:

while True:
                ret, im
                faces=faceCascade.detectMultiScale(im, 1.3,5)
                for(x,y,w,h) in faces:
                    Id, conf = recognizer.predict(gray[y:y+h,x:x+w])

I saved the image in ID:1, it gives correct result before trying with unknown face but thereafter it gives arbitrary ID value say,40, 50, 10 etc.

Please assist me for getting correct ID

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2018-08-13 22:28:56 -0500

berak gravatar image

updated 2018-08-14 07:36:49 -0500

your results are somewhat "expected", it's the wrong tool for your job.

the FaceRecognizer classes in opencv are doing identification (get the closest from a database of known faces, there is no real concept of "unknown" persons here.).

if your problem is verification (do those 2 images show the same person ?), you should not use those. (rather use the cv2.dnn, and train some threshold value for same/not-same).

if your problem is authentication (is that me? , finding a single person) , try with cv2.face.MACE instead.


or use opencv's dnn based face recognition: get the model from here: ""

nn = cv2.dnn.readNetFromTorch(path_to_model)
# now, to compare 2 face images:
nn.setInput(cv2.dnn.blobFromImage(image1, 1./255, (96,96), (), true, false);
f1 = net.forward()

nn.setInput(cv2.dnn.blobFromImage(image2, 1./255, (96,96), (), true, false);
f2 = net.forward()

distance = cv2.norm(f1,f2); # about < 0.6 for "same"
edit flag offensive delete link more


Hi Berak,

Thanks for the reply.

I found cv2.MACE but as i am new in python programming so unable to implement this class in my program. Would you please share some code or links for the same ?

it will be very helpful for me.

Thanks Ashutosh Malviya

ashueice gravatar imageashueice ( 2018-08-14 04:30:51 -0500 )edit

there is a c++ sample here

basically, you'd:

mace = cv2.face.MACE_create();
# later:
yes = mace.same(face_image)
berak gravatar imageberak ( 2018-08-14 05:03:39 -0500 )edit

Hi Berek I have few question on above code: 1) Application throws error no Module name “MACE” when i added above code in my script 2) I already created trainer.yml for training the data , could I have to again train the data with MAC.Train() method during the verification time or should I add it before 3) Can we pass live web cam image frame as a “face_image” parameter Thanks Ashutosh Malviya

ashueice gravatar imageashueice ( 2018-08-14 05:30:29 -0500 )edit
  1. check >>> help(cv2.face) you either have the MACE class there, or not (old cv2 version, then)
  2. no entirely different data. you need a list of images of the person you want to predict ONLY.
  3. yes, ofc. you can. should be cropped from face detection first, though.
berak gravatar imageberak ( 2018-08-14 05:40:14 -0500 )edit

Updated the Opencv and "opencv-3.4.1-py36h597e314_201" folder is also created but when i run the script having below code: mace = cv2.face.MACE_create()

i got below error: module 'cv2.face' has no attribute 'MACE_create'

Unable to download opencv-3.4.2 , could you please tell me the path to download 3.4.2 version in conda environment.

ashueice gravatar imageashueice ( 2018-08-14 07:13:30 -0500 )edit

sorry, but i'm not using conda, so no idea. (and indeed, the 3.4.1 release does not have it, same here)

so, plan B ? (see updated answer)

berak gravatar imageberak ( 2018-08-14 07:28:58 -0500 )edit

Question Tools

1 follower


Asked: 2018-08-13 21:38:29 -0500

Seen: 317 times

Last updated: Dec 19 '19