def main(rootDir): detector = FaceDetector() for iDir, subjectDirs, files in os.walk(rootDir): for subjectDir in subjectDirs: print('Walking subject folder at %s' % subjectDir) subjectPath = os.path.join(rootDir, subjectDir) # Each session for jDir, sessionDirs, subjectFiles in os.walk(subjectPath): for sessionDir in sessionDirs: print('Walking sessions folder at %s' % sessionDir) sessionPath = os.path.join(subjectPath, sessionDir) img = cv2.imread(sessionPath + '/im0.bmp', 0) face_coords = detector.detect(img) for i in range(4): imgFilePath = sessionPath + '/im%s.bmp' % i imgCroppedFilePath = sessionPath + '/im%s_cropped.bmp' % i if os.path.isfile(imgFilePath): print('Cropping the image %s' % imgFilePath) img = cv2.imread(imgFilePath) if face_coords is None: print( 'Did not find a face, just using normal image' ) cv2.imwrite(imgCroppedFilePath, img) else: face = detector.crop_face(img, face_coords) cv2.imwrite(imgCroppedFilePath, face)
def main(args): # Create algorithm objects lbp = LBP() detector = FaceDetector() svm = SVM() knn = KNearest() # Get subjects to train the svm on imgs = [ '/home/arthur/Downloads/lfw_funneled/Gian_Marco/Gian_Marco_0001.jpg', '/home/arthur/Downloads/lfw_funneled/Micky_Ward/Micky_Ward_0001.jpg', '/home/arthur/Downloads/lfw_funneled/Ziwang_Xu/Ziwang_Xu_0001.jpg', '/home/arthur/Downloads/lfw_funneled/Zhu_Rongji/Zhu_Rongji_0001.jpg' ] # Load the subjects and extract their features hists, labels = load_subjects(imgs, detector, lbp) # Transform to np arrays samples = np.array(hists, dtype=np.float32) labels = np.array(labels, dtype=np.int) # Train classifiers svm.train(samples, labels) knn.train(samples, labels) # Check which mode the app is running in (image vs. video) if args.image is not None: # Read the image from the file path provided img = cv2.imread(args.image, 0) # Check the image exists if img is not None: # Run face recognition algorithm classify_snapshot(img, detector, lbp, knn) else: print('The image could not be found...') return # Establish connection to camera cap = cv2.VideoCapture(0) # Continuously grab the next frame from the camera while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() # Start timer for performance logging start = time.time() # Convert frame to gray scale for face detector gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect a face in the frame and crop the image face_coords = detector.detect(gray) face = detector.crop_face(gray, face_coords) # Check we have detected a face if face is not None: # Apply LBP operator to get feature descriptor hist, bins = lbp.run(face, False) # Convert the LBP descriptor to numpy array for opencv classifiers test_sample = np.array([hist], dtype=np.float32) # Get the class of id of the closest neighbour and its distance dist, class_id = knn.predict(test_sample) # Draw the face if found util.draw_face(dist, class_id, frame, face_coords) # util.segment_face(frame) # Processing finished end = time.time() # Write the fps to the video util.write_fps(start, end, frame) # Display the resulting frame cv2.imshow('frame', frame) # Check if we should stop the application if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()