def detectHumanBody(): """ Detect one human body on an image. """ faceEngine = VLFaceEngine() detector = faceEngine.createHumanDetector() imageWithOneHuman = VLImage.load(filename=EXAMPLE_O) pprint.pprint( detector.detectOne(imageWithOneHuman, detectLandmarks=False).asDict()) imageWithSeveralHumans = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) pprint.pprint( detector.detectOne(imageWithSeveralHumans, detectLandmarks=False).asDict()) severalHumans = detector.detect([imageWithSeveralHumans], detectLandmarks=True) pprint.pprint([human.asDict() for human in severalHumans[0]]) imageWithoutHuman = VLImage.load(filename=EXAMPLE_WITHOUT_FACES) pprint.pprint( detector.detectOne(imageWithoutHuman, detectLandmarks=False) is None) severalHumans = detector.detect( [ImageForDetection(imageWithSeveralHumans, Rect(1, 1, 300.0, 300.0))]) pprint.pprint(severalHumans)
def test_batch_detect_by_area_with_human(self): """ Test batch human detection by area with human """ detection = self.detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=GOOD_AREA) ]) assert 1 == len(detection[0]) self.assertHumanDetection(detection[0], VLIMAGE_ONE_FACE)
def test_image_detection_with_transfer_option(self): """ Test structure image for detection """ detection = self.detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=GOOD_AREA) ]) self.assertHumanDetection(detection[0], VLIMAGE_ONE_FACE) assert 1 == len(detection)
def test_detect_limit_bad_param(self): """ Test batch detection with negative limit number """ imageWithManyFaces = VLImage.load(filename=MANY_FACES) self.detector.detect(images=[ ImageForDetection(image=imageWithManyFaces, detectArea=GOOD_AREA) ], limit=-1)
def test_batch_detect_by_area_without_human(self): """ Test batch human detection by area without human """ detection = self.detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=AREA_WITHOUT_FACE) ]) assert 1 == len(detection) assert 0 == len(detection[0])
def test_image_detection_with_transfer_option(self): """ Test structure image for detection """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): detection = detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=GOOD_AREA) ]) self.assertFaceDetection(detection[0], VLIMAGE_ONE_FACE) assert 1 == len(detection)
def test_batch_detect_by_area_with_face(self): """ Test batch face detection by area with face """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): detection = detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=GOOD_AREA) ]) assert 1 == len(detection[0]) self.assertFaceDetection(detection[0], VLIMAGE_ONE_FACE)
def test_batch_detect_by_area_without_face(self): """ Test batch face detection by area without face """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): detection = detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=AREA_WITHOUT_FACE) ]) assert 1 == len(detection) assert 0 == len(detection[0])
def test_batch_detect_invalid_rectangle(self): """ Test batch face detection with an invalid rect """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(LunaSDKException) as exceptionInfo: detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=Rect()) ]) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect)
def test_batch_detect_in_area_outside_image(self): """ Test batch detection in area outside image """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(LunaSDKException) as exceptionInfo: detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=OUTSIDE_AREA) ]) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect)
def test_detect_one_limit_bad_param(self): """ Test batch detection with negative limit number """ imageWithManyFaces = VLImage.load(filename=MANY_FACES) for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(TypeError) as exceptionInfo: detector.detectOne(image=ImageForDetection( image=imageWithManyFaces, detectArea=GOOD_AREA), limit=-1) assert isinstance(exceptionInfo.value, TypeError) is True, "expected TypeError"
def test_batch_detect_invalid_rectangle(self): """ Test batch human detection with an invalid rect """ with pytest.raises(LunaSDKException) as exceptionInfo: self.detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=Rect()) ]) self.assertLunaVlError(exceptionInfo, LunaVLError.BatchedInternalError) assert len(exceptionInfo.value.context ) == 1, "Expect one error in exception context" self.assertReceivedAndRawExpectedErrors(exceptionInfo.value.context[0], LunaVLError.InvalidRect)
def test_batch_detect_in_area_outside_image(self): """ Test batch detection in area outside image """ with pytest.raises(LunaSDKException) as exceptionInfo: self.detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=OUTSIDE_AREA) ]) self.assertLunaVlError(exceptionInfo, LunaVLError.BatchedInternalError) assert len(exceptionInfo.value.context ) == 1, "Expect one error in exception context" self.assertReceivedAndRawExpectedErrors(exceptionInfo.value.context[0], LunaVLError.InvalidRect)
def test_batch_detect_invalid_rectangle(self): """ Test batch face detection with an invalid rect """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(LunaSDKException) as exceptionInfo: detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=Rect()) ]) self.assertLunaVlError( exceptionInfo, LunaVLError.BatchedInternalError.format( "Failed validation.")) assert len(exceptionInfo.value.context ) == 1, "Expect one error in exception context" self.assertReceivedAndRawExpectedErrors( exceptionInfo.value.context[0], LunaVLError.InvalidRect.format("Invalid rectangle"))
def detectFaces(): """ Detect one face on an image. """ 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()) imageWithSeveralFaces = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) pprint.pprint(detector.detectOne(imageWithSeveralFaces, detect5Landmarks=False, detect68Landmarks=False).asDict()) severalFaces = detector.detect([imageWithSeveralFaces], detect5Landmarks=False, detect68Landmarks=False) pprint.pprint([face.asDict() for face in severalFaces[0]]) imageWithoutFace = VLImage.load(filename=EXAMPLE_WITHOUT_FACES) pprint.pprint(detector.detectOne(imageWithoutFace, detect5Landmarks=False, detect68Landmarks=False) is None) severalFaces = detector.detect( [ImageForDetection(imageWithSeveralFaces, Rect(1, 1, 300.0, 300.0))], detect5Landmarks=False, detect68Landmarks=False, ) pprint.pprint(severalFaces)