Example #1
0
def detectFaces():
    """
    Redetect faces on images.
    """
    faceEngine = VLFaceEngine()
    detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1)

    imageWithOneFace = VLImage.load(filename=EXAMPLE_O)
    pprint.pprint(
        detector.detectOne(imageWithOneFace,
                           detect5Landmarks=False,
                           detect68Landmarks=False).asDict())
    detection = detector.detectOne(imageWithOneFace,
                                   detect5Landmarks=False,
                                   detect68Landmarks=False)
    pprint.pprint(detector.redetectOne(image=imageWithOneFace, bBox=detection))
    pprint.pprint(
        detector.redetectOne(image=imageWithOneFace,
                             bBox=detection.boundingBox.rect))

    imageWithSeveralFaces = VLImage.load(filename=EXAMPLE_SEVERAL_FACES)
    severalFaces = detector.detect([imageWithSeveralFaces],
                                   detect5Landmarks=False,
                                   detect68Landmarks=False)

    pprint.pprint(
        detector.redetect(images=[
            ImageForRedetection(
                imageWithSeveralFaces,
                [face.boundingBox.rect for face in severalFaces[0]]),
            ImageForRedetection(imageWithOneFace,
                                [detection.boundingBox.rect]),
            ImageForRedetection(imageWithOneFace, [Rect(0, 0, 1, 1)]),
        ]))
Example #2
0
 def test_batch_redetect_invalid_rectangle(self):
     """
     Test batch re-detection with an invalid rect
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             with pytest.raises(LunaSDKException) as exceptionInfo:
                 detector.redetect(images=[
                     ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                         bBoxes=[INVALID_RECT]),
                     ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                         bBoxes=[Rect(0, 0, 100, 100)]),
                 ])
             self.assertLunaVlError(
                 exceptionInfo,
                 LunaVLError.BatchedInternalError.format(
                     "Failed validation."))
             assert len(exceptionInfo.value.context
                        ) == 2, "Expect two error in exception context"
             self.assertReceivedAndRawExpectedErrors(
                 exceptionInfo.value.context[0],
                 LunaVLError.InvalidRect.format("Invalid rectangle"))
             self.assertReceivedAndRawExpectedErrors(
                 exceptionInfo.value.context[1],
                 LunaVLError.Ok.format("Ok"))
Example #3
0
def detectHumans():
    """
    Redetect human body on images.
    """
    faceEngine = VLFaceEngine()
    detector = faceEngine.createHumanDetector()

    imageWithOneHuman = VLImage.load(filename=EXAMPLE_O)
    detection = detector.detectOne(imageWithOneHuman, detectLandmarks=False)
    pprint.pprint(detector.redetectOne(image=imageWithOneHuman,
                                       bBox=detection))
    pprint.pprint(
        detector.redetectOne(image=imageWithOneHuman,
                             bBox=detection.boundingBox.rect))

    imageWithSeveralHumans = VLImage.load(filename=EXAMPLE_SEVERAL_FACES)
    severalHumans = detector.detect([imageWithSeveralHumans],
                                    detectLandmarks=False)

    pprint.pprint(
        detector.redetect(images=[
            ImageForRedetection(
                imageWithSeveralHumans,
                [human.boundingBox.rect for human in severalHumans[0]]),
            ImageForRedetection(imageWithOneHuman,
                                [detection.boundingBox.rect]),
            ImageForRedetection(imageWithOneHuman, [Rect(0, 0, 1, 1)]),
        ]))
Example #4
0
 def test_redetect_invalid_rectangle(self):
     """
     Test batch re-detection with an invalid rect
     """
     with pytest.raises(LunaSDKException) as exceptionInfo:
         self.detector.redetect(images=[
             ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                 bBoxes=[INVALID_RECT]),
             ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                 bBoxes=[Rect(0, 0, 100, 100)]),
         ])
     self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect)
Example #5
0
 def test_rect_float(self):
     """
     Test re-detection with an invalid rect
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             detector.redetect(images=[ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[ERROR_CORE_RECT])])
Example #6
0
 def test_match_redetect_one_image(self):
     """
     Test match of values at different re-detections (redetectOne and redetect) with one image
     """
     for image in (VLIMAGE_ONE_FACE, VLIMAGE_SMALL):
         for detector in self.detectors:
             with self.subTest(detectorType=detector.detectorType):
                 if detector.detectorType.name == "FACE_DET_V3":
                     self.skipTest("Skip for FaceDetV3. Different value")
                     continue
                 bBoxRect = detector.detectOne(image=image).boundingBox.rect
                 redetectOne = detector.redetectOne(image=image,
                                                    bBox=bBoxRect,
                                                    detect68Landmarks=True)
                 batchRedetect = detector.redetect(images=[
                     ImageForRedetection(image=image, bBoxes=[bBoxRect])
                 ] * 3,
                                                   detect68Landmarks=True)
                 for redetect in batchRedetect:
                     for face in redetect:
                         assert face.boundingBox.asDict(
                         ) == redetectOne.boundingBox.asDict()
                         assert face.landmarks5.asDict(
                         ) == redetectOne.landmarks5.asDict()
                         assert face.landmarks68.asDict(
                         ) == redetectOne.landmarks68.asDict()
Example #7
0
 def test_batch_redetect_in_area_outside_image(self):
     """
     Test batch re-detection in area outside image
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             if detector.detectorType.name == "FACE_DET_V3":
                 redetect = detector.redetect(
                     images=[ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[OUTSIDE_AREA])]
                 )
                 self.assertFaceDetection(redetect[0], VLIMAGE_ONE_FACE)
             else:
                 redetect = detector.redetect(
                     images=[ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[OUTSIDE_AREA])]
                 )
                 assert redetect[0][0] is None
Example #8
0
 def test_batch_redetect_in_area_outside_image(self):
     """
     Test batch re-detection in area outside image
     """
     redetect = self.detector.redetect(images=[
         ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[OUTSIDE_AREA])
     ])
     self.assertHumanDetection(redetect[0], VLIMAGE_ONE_FACE)
Example #9
0
 def test_rect_float(self):
     """
     Test re-detection with an invalid rect
     """
     self.detector.redetect(images=[
         ImageForRedetection(image=VLIMAGE_ONE_FACE,
                             bBoxes=[ERROR_CORE_RECT])
     ])
 def test_redetect_invalid_rectangle(self):
     """
     Test batch re-detection with an invalid rect
     """
     with pytest.raises(LunaSDKException) as exceptionInfo:
         self.detector.redetect(images=[
             ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                 bBoxes=[INVALID_RECT]),
             ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                 bBoxes=[Rect(0, 0, 100, 100)]),
         ])
     self.assertLunaVlError(exceptionInfo, LunaVLError.BatchedInternalError)
     assert len(exceptionInfo.value.context
                ) == 2, "Expect two errors in exception context"
     self.assertReceivedAndRawExpectedErrors(exceptionInfo.value.context[0],
                                             LunaVLError.InvalidRect)
     self.assertReceivedAndRawExpectedErrors(exceptionInfo.value.context[1],
                                             LunaVLError.Ok)
 def test_rect_float(self):
     """
     Test re-detection with an invalid rect
     """
     with pytest.raises(LunaSDKException) as exceptionInfo:
         self.detector.redetect(images=[
             ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                 bBoxes=[ERROR_CORE_RECT])
         ])
     self.assertLunaVlError(exceptionInfo, LunaVLError.BatchedInternalError)
Example #12
0
 def test_batch_redetect_with_one_human(self):
     """
     Test batch re-detection with one human image
     """
     detection = self.detector.detectOne(image=VLIMAGE_ONE_FACE)
     redetect = self.detector.redetect(images=[
         ImageForRedetection(image=VLIMAGE_ONE_FACE,
                             bBoxes=[detection.boundingBox.rect])
     ])[0]
     self.assertHumanDetection(redetect, VLIMAGE_ONE_FACE)
Example #13
0
    def test_batch_redetect(self):
        """
        Test re-detection batch of images
        """
        detectSeveral = self.detector.detect(
            images=[VLIMAGE_ONE_FACE, VLIMAGE_SEVERAL_FACE])
        redetect = self.detector.redetect(images=[
            ImageForRedetection(
                image=VLIMAGE_SEVERAL_FACE,
                bBoxes=[human.boundingBox.rect for human in detectSeveral[1]]),
            ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                bBoxes=[detectSeveral[0][0].boundingBox.rect]),
        ])

        assert 2 == len(redetect)
        self.assertHumanDetection(redetect[0], VLIMAGE_SEVERAL_FACE)
        self.assertHumanDetection(redetect[1], VLIMAGE_ONE_FACE)
        assert 5 == len(redetect[0])
        assert 1 == len(redetect[1])
Example #14
0
 def test_batch_redetect(self):
     """
     Test re-detection batch of images
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             detectSeveral = detector.detect(images=[VLIMAGE_ONE_FACE, VLIMAGE_SEVERAL_FACE])
             redetect = detector.redetect(
                 images=[
                     ImageForRedetection(
                         image=VLIMAGE_SEVERAL_FACE, bBoxes=[face.boundingBox.rect for face in detectSeveral[1]]
                     ),
                     ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[detectSeveral[0][0].boundingBox.rect]),
                 ]
             )
             self.assertFaceDetection(redetect[0], VLIMAGE_SEVERAL_FACE)
             self.assertFaceDetection(redetect[1], VLIMAGE_ONE_FACE)
             assert 2 == len(redetect)
             assert 5 == len(redetect[0])
             assert 1 == len(redetect[1])
Example #15
0
 def test_batch_redetect_with_one_face(self):
     """
     Test batch re-detection with one face image
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             detection = detector.detectOne(image=VLIMAGE_ONE_FACE)
             redetect = detector.redetect(
                 images=[ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[detection.boundingBox.rect])]
             )[0]
             self.assertFaceDetection(redetect, VLIMAGE_ONE_FACE)
Example #16
0
 def test_redetect_by_area_without_face(self):
     """
     Test re-detection by area without face
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             redetectOne = detector.redetectOne(image=VLIMAGE_ONE_FACE, bBox=Rect(0, 0, 100, 100))
             redetect = detector.redetect(
                 images=[ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[Rect(0, 0, 100, 100)])]
             )[0][0]
             assert redetectOne is None, "excepted None but found {}".format(redetectOne)
             assert redetect is None, "excepted None but found {}".format(redetectOne)
Example #17
0
 def test_redetect_by_area_without_human(self):
     """
     Test re-detection by area without human
     """
     redetectOne = self.detector.redetectOne(image=VLIMAGE_ONE_FACE,
                                             bBox=Rect(0, 0, 100, 100))
     redetect = self.detector.redetect(images=[
         ImageForRedetection(image=VLIMAGE_ONE_FACE,
                             bBoxes=[Rect(0, 0, 100, 100)])
     ])[0][0]
     assert redetectOne is None, "excepted None but found {}".format(
         redetectOne)
     assert redetect is None, "excepted None but found {}".format(
         redetectOne)
Example #18
0
 def test_redetect_invalid_rectangle(self):
     """
     Test batch re-detection with an invalid rect
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             with pytest.raises(LunaSDKException) as exceptionInfo:
                 detector.redetect(images=[
                     ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                         bBoxes=[INVALID_RECT])
                 ])
             self.assertLunaVlError(
                 exceptionInfo,
                 LunaVLError.BatchedInternalError.format("Unknown error"))
Example #19
0
 def test_match_redetect_one_image(self):
     """
     Test match of values at different re-detections (redetectOne and redetect) with one image
     """
     for image in (VLIMAGE_ONE_FACE, VLIMAGE_SMALL):
         bBoxRect = self.detector.detectOne(image=image).boundingBox.rect
         redetectOne = self.detector.redetectOne(image=image, bBox=bBoxRect)
         batchRedetect = self.detector.redetect(
             images=[ImageForRedetection(image=image, bBoxes=[bBoxRect])] *
             3)
         for redetect in batchRedetect:
             for human in redetect:
                 assert human.boundingBox.asDict(
                 ) == redetectOne.boundingBox.asDict()
Example #20
0
async def asyncRedetectFaces():
    """
    Async redetect faces on images.
    """
    faceEngine = VLFaceEngine()
    detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3)

    imageWithSeveralFaces = VLImage.load(filename=EXAMPLE_SEVERAL_FACES)
    severalFaces = detector.detect([imageWithSeveralFaces],
                                   detect5Landmarks=False,
                                   detect68Landmarks=False)

    detections = await detector.redetect(
        images=[
            ImageForRedetection(
                imageWithSeveralFaces,
                [face.boundingBox.rect for face in severalFaces[0]]),
        ],
        asyncEstimate=True,
    )
    pprint.pprint(detections)
    task1 = detector.redetect(
        images=[
            ImageForRedetection(imageWithSeveralFaces,
                                [severalFaces[0][0].boundingBox.rect]),
        ],
        asyncEstimate=True,
    )
    task2 = detector.redetect(
        images=[
            ImageForRedetection(imageWithSeveralFaces,
                                [severalFaces[0][1].boundingBox.rect]),
        ],
        asyncEstimate=True,
    )
    for task in (task1, task2):
        pprint.pprint(task.get())
 def test_rect_float(self):
     """
     Test re-detection with an invalid rect
     """
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             with pytest.raises(LunaSDKException) as exceptionInfo:
                 detector.redetect(images=[
                     ImageForRedetection(image=VLIMAGE_ONE_FACE,
                                         bBoxes=[ERROR_CORE_RECT])
                 ])
             self.assertLunaVlError(
                 exceptionInfo,
                 LunaVLError.ValidationFailed.format(
                     LunaVLError.InvalidRect.description))
Example #22
0
 def test_async_redetect_face(self):
     """
     Test async redetect face
     """
     detector = self.detectors[-1]
     detectOne = detector.detectOne(image=VLIMAGE_ONE_FACE)
     task = detector.redetectOne(image=VLIMAGE_ONE_FACE,
                                 bBox=detectOne,
                                 asyncEstimate=True)
     self.assertAsyncEstimation(task, FaceDetection)
     task = detector.redetect([
         ImageForRedetection(VLIMAGE_ONE_FACE, [detectOne.boundingBox.rect])
     ] * 2,
                              asyncEstimate=True)
     self.assertAsyncBatchEstimation(task, FaceDetection)
Example #23
0
 def test_async_redetect_human_body(self):
     """
     Test async redetect human body
     """
     detector = self.detector
     detectOne = detector.detectOne(image=VLIMAGE_ONE_FACE)
     task = detector.redetectOne(image=VLIMAGE_ONE_FACE,
                                 bBox=detectOne,
                                 asyncEstimate=True)
     self.assertAsyncEstimation(task, HumanDetection)
     task = detector.redetect([
         ImageForRedetection(VLIMAGE_ONE_FACE, [detectOne.boundingBox.rect])
     ] * 2,
                              asyncEstimate=True)
     self.assertAsyncBatchEstimation(task, HumanDetection)
 def test_redetect_in_area_outside_image(self):
     """
     Test re-detection face in area outside image
     """
     # face on the bottom image boundary
     image = VLImage.load(filename=FACE_WITH_MASK)
     for detector in self.detectors:
         with self.subTest(detectorType=detector.detectorType):
             rect = detector.detectOne(image).boundingBox.rect
             rect.width = image.rect.width - rect.x + 100
             rect.height = image.rect.height - rect.y + 100
             redetectOne = detector.redetectOne(image=image, bBox=rect)
             self.assertFaceDetection(redetectOne, image)
             redetect = detector.redetect(
                 images=[ImageForRedetection(image=image, bBoxes=[rect])])
             self.assertFaceDetection(redetect[0], image)
Example #25
0
 def test_get_landmarks_for_batch_redetect(self):
     """
     Test get and check landmark instances for batch re-detection
     """
     for case in self.landmarksCases:
         with self.subTest(landmarks5=case.detect5Landmarks, landmarks68=case.detect68Landmarks):
             for detector in self.detectors:
                 with self.subTest(detectorType=detector.detectorType):
                     detectOne = detector.detectOne(image=VLIMAGE_ONE_FACE)
                     redetect = detector.redetect(
                         images=[ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[detectOne.boundingBox.rect])],
                         detect68Landmarks=case.detect68Landmarks,
                         detect5Landmarks=case.detect5Landmarks,
                     )[0][0]
                     self.assertDetectionLandmarks(
                         detection=redetect, landmarks5=case.detect5Landmarks, landmarks68=case.detect68Landmarks
                     )