Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

how to dispaly percentage pridiction in fisher face recoginition algorithm

$
0
0
i am using fisher-face to recognize faces and i want to show a percentage prediction on a rectangle with the matching name. secondly, i want to set a value e.g if the matching percentage is 80% it should display the name of a person otherwise it should display unknown. third, i do not want to run recognition all the time. once the face is recognized by the algorithm as a known or unknown person it should not do it again and again. in short it should run recognition once on every face. if someone can help i will be very grateful. # facerec.py import cv2, sys, numpy, os size = 4 fn_haar = 'haarcascade_frontalface_default.xml' fn_dir = 'att_faces' # Part 1: Create fisherRecognizer print('Training...') # Create a list of images and a list of corresponding names (images, lables, names, id) = ([], [], {}, 0) for (subdirs, dirs, files) in os.walk(fn_dir): for subdir in dirs: names[id] = subdir subjectpath = os.path.join(fn_dir, subdir) for filename in os.listdir(subjectpath): path = subjectpath + '/' + filename lable = id images.append(cv2.imread(path, 0)) lables.append(int(lable)) id += 1 (im_width, im_height) = (112, 92) # Create a Numpy array from the two lists above (images, lables) = [numpy.array(lis) for lis in [images, lables]] # OpenCV trains a model from the images # NOTE FOR OpenCV2: remove '.face' model = cv2.createFisherFaceRecognizer() model.train(images, lables) # Part 2: Use fisherRecognizer on camera stream haar_cascade = cv2.CascadeClassifier(fn_haar) webcam = cv2.VideoCapture(0) while True: (rval, frame) = webcam.read() frame=cv2.flip(frame,1,0) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size)) faces = haar_cascade.detectMultiScale(mini) for i in range(len(faces)): face_i = faces[i] (x, y, w, h) = [v * size for v in face_i] face = gray[y:y + h, x:x + w] face_resize = cv2.resize(face, (im_width, im_height)) # Try to recognize the face prediction = model.predict(face_resize) result = { 'face': { 'distance': prediction[1], 'coords': { 'x': str(faces[0][0]), 'y': str(faces[0][1]), 'width': str(faces[0][2]), 'height': str(faces[0][3]) } } } print result print "prediction",prediction cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) # Write the name of recognized face # [1] if prediction[1]<700: cv2.putText(frame, '%s - %.0f' % (names[prediction[0]],prediction[1]), (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0)) else: cv2.putText(frame, 'Unknown', (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0)) cv2.imshow('OpenCV', frame) key = cv2.waitKey(10) if key == 27: break

Viewing all articles
Browse latest Browse all 2088

Trending Articles