def estimateBackground(): """ Example of a face detection background estimation. """ image = VLImage.load(filename=EXAMPLE_4) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) backgroundEstimator = faceEngine.createFaceDetectionBackgroundEstimator() faceDetection = detector.detectOne(image) #: single estimation imageWithFaceDetection = ImageWithFaceDetection(image, faceDetection.boundingBox) background = backgroundEstimator.estimate(imageWithFaceDetection) pprint.pprint(background) image2 = VLImage.load(filename=EXAMPLE_4) faceDetection2 = detector.detectOne(image2) #: batch estimation imageWithFaceDetectionList = [ ImageWithFaceDetection(image, faceDetection.boundingBox), ImageWithFaceDetection(image2, faceDetection2.boundingBox), ] backgrounds = backgroundEstimator.estimateBatch(imageWithFaceDetectionList) pprint.pprint(backgrounds)
def estimateAGS(): """ Estimate face detection ags. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) agsEstimator = faceEngine.createAGSEstimator() imageWithFaceDetection = ImageWithFaceDetection(image, faceDetection.boundingBox) pprint.pprint( agsEstimator.estimate(imageWithFaceDetection=imageWithFaceDetection)) pprint.pprint(agsEstimator.estimate(faceDetection)) image2 = VLImage.load(filename=EXAMPLE_1) faceDetection2 = detector.detectOne(image2) imageWithFaceDetectionList = [ ImageWithFaceDetection(image, faceDetection.boundingBox), ImageWithFaceDetection(image2, faceDetection2.boundingBox), ] pprint.pprint(agsEstimator.estimateBatch(imageWithFaceDetectionList)) pprint.pprint( agsEstimator.estimateBatch(detections=[faceDetection, faceDetection2]))
def estimateFisheye(): """ Example of a fisheye estimation. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) fishEstimator = faceEngine.createFisheyeEstimator() faceDetection = detector.detectOne(image, detect5Landmarks=False, detect68Landmarks=True) #: single estimation imageWithFaceDetection = ImageWithFaceDetection(image, faceDetection.boundingBox) fisheye = fishEstimator.estimate(imageWithFaceDetection) pprint.pprint(fisheye) image2 = VLImage.load(filename=EXAMPLE_1) faceDetection2 = detector.detectOne(image2, detect5Landmarks=False, detect68Landmarks=True) #: batch estimation imageWithFaceDetectionList = [ ImageWithFaceDetection(image, faceDetection.boundingBox), ImageWithFaceDetection(image2, faceDetection2.boundingBox), ] fisheyeList = fishEstimator.estimateBatch(imageWithFaceDetectionList) pprint.pprint(fisheyeList)
def estimateHeadPose(): """ Example of a head pose estimation. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) headPoseEstimator = faceEngine.createHeadPoseEstimator() faceDetection = detector.detectOne(image, detect5Landmarks=False, detect68Landmarks=True) #: estimate by 68 landmarks angles = headPoseEstimator.estimateBy68Landmarks(faceDetection.landmarks68) pprint.pprint(angles.asDict()) #: get frontal type pprint.pprint(angles.getFrontalType()) #: estimate by detection imageWithFaceDetection = ImageWithFaceDetection(image, faceDetection.boundingBox) angles = headPoseEstimator.estimateByBoundingBox(imageWithFaceDetection) angles.getFrontalType() pprint.pprint(angles) image2 = VLImage.load(filename=EXAMPLE_1) faceDetection2 = detector.detectOne(image2, detect5Landmarks=False, detect68Landmarks=True) #: batch estimate by detection imageWithFaceDetectionList = [ ImageWithFaceDetection(image, faceDetection.boundingBox), ImageWithFaceDetection(image2, faceDetection2.boundingBox), ] anglesList = headPoseEstimator.estimateBatch(imageWithFaceDetectionList) pprint.pprint(anglesList)
def test_head_pose_batch_estimation(self): """ Batch estimating head pose test. """ image = VLImage.load(filename=GOST_HEAD_POSE_FACE) detection = self.detector.detectOne(image, detect5Landmarks=True, detect68Landmarks=True) imageWithFaceDetectionList = [ ImageWithFaceDetection(self.image, self.detection.boundingBox), ImageWithFaceDetection(image, detection.boundingBox), ] anglesList = self.headPoseEstimator.estimateBatch(imageWithFaceDetectionList) assert isinstance(anglesList, list) assert len(anglesList) == 2 for angles in anglesList: self.assertHeadPose(angles)
def test_estimate_head_pose_by_bounding_box(self): """ Estimating head pose by bounding box test. """ angles = TestHeadPose.headPoseEstimator.estimateByBoundingBox( ImageWithFaceDetection(self.image, self.detection.boundingBox)) self.assertHeadPose(angles)
def test_estimate_head_pose_by_bounding_box_from_other_image(self): """ Estimating head pose on image without faces by bounding box from other image. """ angles = self.headPoseEstimator.estimateByBoundingBox( ImageWithFaceDetection(self.image, self.detection.boundingBox) ) self.assertHeadPose(angles)
def test_estimate_head_pose_by_image_and_bounding_box_empty_bounding_box(self): """ Estimating head pose by image and empty bounding box """ fakeDetection = Detection(RectFloat(0.0, 0.0, 0.0, 0.0), 0.9) bBox = BoundingBox(fakeDetection) with pytest.raises(LunaSDKException) as exceptionInfo: self.headPoseEstimator.estimateByBoundingBox(ImageWithFaceDetection(self.image, bBox)) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidDetection.format("Invalid detection"))
def test_estimate_head_pose_by_image_and_bounding_box_without_intersection(self): """ Estimating head pose by image and bounding box without intersection """ fakeDetection = Detection(RectFloat(3000.0, 3000.0, 100.0, 100.0), 0.9) bBox = BoundingBox(fakeDetection) with pytest.raises(LunaSDKException) as exceptionInfo: self.headPoseEstimator.estimateByBoundingBox(ImageWithFaceDetection(self.image, bBox)) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect.format("Invalid rectangle"))
def test_estimate_background_by_image_and_bounding_box_without_intersection( self): """ Estimating background by image and bounding box without intersection """ fakeDetection = Detection(RectFloat(3000.0, 3000.0, 100.0, 100.0), 0.9) bBox = BoundingBox(fakeDetection) with pytest.raises(LunaSDKException) as exceptionInfo: self.backgroundEstimator.estimate( ImageWithFaceDetection(VLImage.load(filename=ONE_FACE), bBox)) self.assertLunaVlError( exceptionInfo, LunaVLError.InvalidRect.format("Invalid rectangle"))
def test_estimate_background_by_image_and_bounding_box_empty_bounding_box( self): """ Estimating background by image and empty bounding box """ fakeDetection = Detection(RectFloat(0.0, 0.0, 0.0, 0.0), 0.9) bBox = BoundingBox(fakeDetection) with pytest.raises(LunaSDKException) as exceptionInfo: self.backgroundEstimator.estimate( ImageWithFaceDetection(VLImage.load(filename=ONE_FACE), bBox)) self.assertLunaVlError( exceptionInfo, LunaVLError.InvalidDetection.format("Invalid detection"))
def test_async_estimate_head_pose(self): """ Test async estimate head pose """ task = self.headPoseEstimator.estimate(self.detection.landmarks68, asyncEstimate=True) self.assertAsyncEstimation(task, HeadPose) task = self.headPoseEstimator.estimateBatch([self.detection] * 2, asyncEstimate=True) self.assertAsyncBatchEstimation(task, HeadPose) task = self.headPoseEstimator.estimateByBoundingBox( ImageWithFaceDetection(self.image, self.detection.boundingBox), asyncEstimate=True ) self.assertAsyncEstimation(task, HeadPose) task = self.headPoseEstimator.estimateBy68Landmarks(self.detection.landmarks68, asyncEstimate=True) self.assertAsyncEstimation(task, HeadPose)
async def asyncEstimateBackground(): """ Example of an async background estimation. """ image = VLImage.load(filename=EXAMPLE_4) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) backgroundEstimator = faceEngine.createFaceDetectionBackgroundEstimator() faceDetection = detector.detectOne(image) # async estimation imageWithFaceDetection = ImageWithFaceDetection(image, faceDetection.boundingBox) backgrounds = await backgroundEstimator.estimate(imageWithFaceDetection, asyncEstimate=True) pprint.pprint(backgrounds.asDict()) # run tasks and get results task1 = backgroundEstimator.estimate(imageWithFaceDetection, asyncEstimate=True) task2 = backgroundEstimator.estimate(imageWithFaceDetection, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get().asDict())