Пример #1
0
    def test_face(self):
        image_path = os.path.join(os.path.dirname(__file__),
                                  'testdata/face.jpg')
        image = cv2.imread(image_path)
        with mp_faces.FaceDetection(min_detection_confidence=0.5) as faces:
            for idx in range(5):
                results = faces.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
                self._annotate(image.copy(), results, idx)
                location_data = results.detections[0].location_data
                x = [
                    keypoint.x for keypoint in location_data.relative_keypoints
                ]
                y = [
                    keypoint.y for keypoint in location_data.relative_keypoints
                ]
                face_keypoints = np.transpose(np.stack(
                    (y, x))) * image.shape[0:2]
                prediction_error = np.abs(
                    np.asarray(face_keypoints) -
                    np.asarray(EXPECTED_FACE_KEY_POINTS))

                self.assertLen(results.detections, 1)
                self.assertLen(location_data.relative_keypoints, 6)
                npt.assert_array_less(prediction_error, DIFF_THRESHOLD)
Пример #2
0
 def test_blank_image(self):
     image = np.zeros([100, 100, 3], dtype=np.uint8)
     image.fill(255)
     with mp_faces.FaceDetection(min_detection_confidence=0.5) as faces:
         results = faces.process(image)
         self.assertIsNone(results.detections)
Пример #3
0
 def test_invalid_image_shape(self):
     with mp_faces.FaceDetection() as faces:
         with self.assertRaisesRegex(
                 ValueError,
                 'Input image must contain three channel rgb data.'):
             faces.process(np.arange(36, dtype=np.uint8).reshape(3, 3, 4))