def testDetectFaces(self):
     '''
     Go through two images, the first with 1 face, the second with 4 faces
     Assert that the detected faces are correct, and draw them.
     Also creates output images of the padded faces
     '''
     fnames = ['./resources/cascade/Fayssal_Mekdad_0002.jpg', './resources/cascade/family-home.png']
     
     expected_faces = [[],[]]
     
     expected_faces[0].append(CascadeResult(box_with_score = ([61,62,132,132], 344), cascade_type = 'haar', angle = 0.0))
     
     expected_faces[1].append(CascadeResult(box_with_score = ([327,101,119,119], 244), cascade_type = 'haar', angle = 0.0))
     expected_faces[1].append(CascadeResult(box_with_score = ([238,107,111,111], 135), cascade_type = 'lbp', angle = 0.0))
     expected_faces[1].append(CascadeResult(box_with_score = ([163,48,93,93], 51), cascade_type = 'lbp', angle = 0.0))
     expected_faces[1].append(CascadeResult(box_with_score = ([433,86,95,95], 92), cascade_type = 'lbp', angle = 0.0))
 
     for n_images, fname in enumerate(fnames):
         _, base_fname = os.path.split(fname)
         img = cv2.imread(fname)
         gray_img = cv2.imread(fname, 0)
         
         face_finder = CascadeFaceFinder(haar_file = '../resources/haarcascade_frontalface_default.xml',
                                         lbp_file = '../resources/lbpcascade_frontalface.xml')
         faces = face_finder.get_faces_list_in_photo(gray_img)
         
         img_to_draw_on = img.copy()
         
         for n_face, face in enumerate(faces):
             self.assertAlmostEqual(face.overlap(expected_faces[n_images][n_face]) / face.area, 1.00, 0.01)
             
             draw_rect(img_to_draw_on, face)
             
             padded_face, bounding_box_in_padded_face, _, _ = extract_box(img, face, padding_factor = 0.25)
             new_face_file = os.path.join('./outputs/cascade/1/', base_fname.split('.')[0] + '.face.%d.png' %n_face)
             cv2.imwrite(new_face_file, padded_face)
             padded_face_loaded = cv2.imread(new_face_file)
             draw_rect(padded_face_loaded, bounding_box_in_padded_face)
             cv2.imshow('face %d' %n_face, padded_face_loaded)
             cv2.waitKey()
             
         cv2.imshow('faces detected', img_to_draw_on)
         cv2.waitKey()
Beispiel #2
0
 def testDetectFaces(self):
     '''
     Go through two images, the first with 1 face, the second with 4 faces
     Assert that the detected faces are correct, and draw them.
     Also creates output images of the padded faces
     '''
     fnames = ['./resources/cascade/janice2.jpg',
     './resources/cascade/albert4.jpg',
     './resources/cascade/albert5.jpg',
     './resources/cascade/david1.jpg']
     
     for n_images, fname in enumerate(fnames):
         _, base_fname = os.path.split(fname)
         img = cv2.imread(fname)
         gray_img = cv2.imread(fname, 0)
         
         face_finder = CascadeFaceFinder(haar_file = '../resources/haarcascade_frontalface_default.xml',
                                         lbp_file = '../resources/lbpcascade_frontalface.xml')
         faces = face_finder.get_faces_list_in_photo(gray_img)
         
         img_to_draw_on = img.copy()
         
         for n_face, face in enumerate(faces):
             
             draw_rect(img_to_draw_on, face)
             
             padded_face, bounding_box_in_padded_face, _, _ = extract_box(img, face, padding_factor = 0.25)
             new_face_file = os.path.join('./outputs/cascade/1/', base_fname.split('.')[0] + '.face.%d.png' %n_face)
             cv2.imwrite(new_face_file, padded_face)
             padded_face_loaded = cv2.imread(new_face_file)
             draw_rect(padded_face_loaded, bounding_box_in_padded_face)
             cv2.imshow('face %d' %n_face, padded_face_loaded)
             cv2.waitKey()
             
         cv2.imshow('faces detected', img_to_draw_on)
         cv2.waitKey()
    def testDetectFaces(self):
        '''
        Go through two images, the first with 1 face, the second with 4 faces
        Assert that the detected faces are correct, and draw them.
        Also creates output images of the padded faces
        '''
        fnames = [
            './resources/cascade/Fayssal_Mekdad_0002.jpg',
            './resources/cascade/family-home.png'
        ]

        expected_faces = [[], []]

        expected_faces[0].append(
            CascadeResult(box_with_score=([61, 62, 132, 132], 344),
                          cascade_type='haar',
                          angle=0.0))

        expected_faces[1].append(
            CascadeResult(box_with_score=([327, 101, 119, 119], 244),
                          cascade_type='haar',
                          angle=0.0))
        expected_faces[1].append(
            CascadeResult(box_with_score=([238, 107, 111, 111], 135),
                          cascade_type='lbp',
                          angle=0.0))
        expected_faces[1].append(
            CascadeResult(box_with_score=([163, 48, 93, 93], 51),
                          cascade_type='lbp',
                          angle=0.0))
        expected_faces[1].append(
            CascadeResult(box_with_score=([433, 86, 95, 95], 92),
                          cascade_type='lbp',
                          angle=0.0))

        for n_images, fname in enumerate(fnames):
            _, base_fname = os.path.split(fname)
            img = cv2.imread(fname)
            gray_img = cv2.imread(fname, 0)

            face_finder = CascadeFaceFinder(
                haar_file='../resources/haarcascade_frontalface_default.xml',
                lbp_file='../resources/lbpcascade_frontalface.xml')
            faces = face_finder.get_faces_list_in_photo(gray_img)

            img_to_draw_on = img.copy()

            for n_face, face in enumerate(faces):
                self.assertAlmostEqual(
                    face.overlap(expected_faces[n_images][n_face]) / face.area,
                    1.00, 0.01)

                draw_rect(img_to_draw_on, face)

                padded_face, bounding_box_in_padded_face, _, _ = extract_box(
                    img, face, padding_factor=0.25)
                new_face_file = os.path.join(
                    './outputs/cascade/1/',
                    base_fname.split('.')[0] + '.face.%d.png' % n_face)
                cv2.imwrite(new_face_file, padded_face)
                padded_face_loaded = cv2.imread(new_face_file)
                draw_rect(padded_face_loaded, bounding_box_in_padded_face)
                cv2.imshow('face %d' % n_face, padded_face_loaded)
                cv2.waitKey()

            cv2.imshow('faces detected', img_to_draw_on)
            cv2.waitKey()