def estimateBasicAttributes(): """ Estimate emotion from a warped image. """ image = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) faceDetections = detector.detect([image])[0] warper = faceEngine.createWarper() warps = [warper.warp(faceDetection) for faceDetection in faceDetections] basicAttributesEstimator = faceEngine.createBasicAttributesEstimator() pprint.pprint( basicAttributesEstimator.estimate(warps[0].warpedImage, estimateAge=True, estimateGender=True, estimateEthnicity=True).asDict()) pprint.pprint( basicAttributesEstimator.estimateBasicAttributesBatch( warps, estimateAge=True, estimateGender=True, estimateEthnicity=True)) pprint.pprint( basicAttributesEstimator.estimateBasicAttributesBatch( warps, estimateAge=True, estimateGender=True, estimateEthnicity=True, aggregate=True))
def estimateWarpQuality(): """ Create warp from detection. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) faceDetection = detector.detectOne(image) warper = faceEngine.createWarper() warp = warper.warp(faceDetection) qualityEstimator = faceEngine.createWarpQualityEstimator() pprint.pprint(qualityEstimator.estimate(warp.warpedImage).asDict())
def estimateEmotion(): """ Estimate emotion from a warped image. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) faceDetection = detector.detectOne(image) warper = faceEngine.createWarper() warp = warper.warp(faceDetection) emotionEstimator = faceEngine.createEmotionEstimator() pprint.pprint(emotionEstimator.estimate(warp.warpedImage).asDict())
def createWarp(): """ Create warp from detection. """ faceEngine = VLFaceEngine() image = VLImage.load( url= "https://cdn1.savepice.ru/uploads/2019/4/15/194734af15c4fcd06dec6db86bbeb7cd-full.jpg" ) detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) faceDetection = detector.detectOne(image) warper = faceEngine.createWarper() warp = warper.warp(faceDetection) pprint.pprint(warp.warpedImage.rect)
def createWarp(): """ Create warp from detection. """ image = VLImage.load(url="https://cdn1.savepice.ru/uploads/2019/4/15/194734af15c4fcd06dec6db86bbeb7cd-full.jpg") faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) faceDetection = detector.detectOne(image) warper = faceEngine.createWarper() warp = warper.warp(faceDetection) pprint.pprint(warp.warpedImage.rect) cv2.imshow("Wapred image", warp.warpedImage.asNPArray()) cv2.imshow("Original image", image.asNPArray()) cv2.waitKey(0) cv2.destroyAllWindows()
def estimateGazeDirection(): """ Estimate gaze direction. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) faceDetection = detector.detectOne(image, detect68Landmarks=True) warper = faceEngine.createWarper() warp = warper.warp(faceDetection) landMarks5Transformation = warper.makeWarpTransformationWithLandmarks( faceDetection, "L5") gazeEstimator = faceEngine.createGazeEstimator() pprint.pprint( gazeEstimator.estimate(landMarks5Transformation, warp).asDict())
def estimateEyes(): """ Estimate emotion from a warped image. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) faceDetection = detector.detectOne(image) warper = faceEngine.createWarper() warp = warper.warp(faceDetection) landMarks5Transformation = warper.makeWarpTransformationWithLandmarks( faceDetection, "L5") eyesEstimator = faceEngine.createEyeEstimator() pprint.pprint( eyesEstimator.estimate(landMarks5Transformation, warp.warpedImage).asDict())
class FaceEstimatorsCollection: """ Collection of lazy load face estimators. Attributes: _headPoseEstimator (Optional[HeadPoseEstimator]): lazy load head pose estimator _eyeEstimator (Optional[EyeEstimator]): lazy load eye estimator _gazeDirectionEstimator (Optional[GazeEstimator]): lazy load gaze direction estimator _mouthStateEstimator (Optional[MouthStateEstimator]): lazy load mouth state estimator _warpQualityEstimator (Optional[WarpQualityEstimator]): lazy load warp quality estimator _basicAttributesEstimator (Optional[BasicAttributesEstimator]): lazy load basic attributes estimator _emotionsEstimator (Optional[EmotionsEstimator]): lazy load emotions estimator _AGSEstimator (Optional[AGSEstimator]): lazy load ags estimator _descriptorEstimator (Optional[FaceDescriptorEstimator]): lazy load face descriptor estimator warper (Optional[Warper]): warper """ __slots__ = ( "_headPoseEstimator", "_eyeEstimator", "_gazeDirectionEstimator", "_mouthStateEstimator", "_warpQualityEstimator", "_basicAttributesEstimator", "_emotionsEstimator", "_faceEngine", "_AGSEstimator", "warper", "_descriptorEstimator", ) def __init__(self, startEstimators: Optional[List[FaceEstimator]] = None, faceEngine: Optional[VLFaceEngine] = None): """ Init. Args: startEstimators: list of estimators which will be initiate now faceEngine: faceengine, factory for estimators """ if faceEngine is None: self._faceEngine = VLFaceEngine() else: self._faceEngine = faceEngine self._basicAttributesEstimator = None self._eyeEstimator = None self._emotionsEstimator = None self._gazeDirectionEstimator = None self._mouthStateEstimator = None self._warpQualityEstimator = None self._headPoseEstimator = None self._AGSEstimator = None self._descriptorEstimator = None self.warper = self._faceEngine.createWarper() if startEstimators: for estimator in set(startEstimators): self.initEstimator(estimator) @staticmethod def _getEstimatorByAttributeName( estimatorAttributeName: str) -> FaceEstimator: """ Get face estimator by attribute name which corresponding to estimator Args: estimatorAttributeName: name Returns: corresponding face estimator Raises: ValueError("Bad attribute name"): if face estimator not found """ for estimator in FaceEstimator: if estimator.name.lower( ) == estimatorAttributeName[1:-len("Estimator")]: return estimator raise ValueError("Bad attribute name") def _getAttributeNameByEstimator(self, estimator: FaceEstimator) -> str: """ Get an estimator attribute name bya face estimator Args: estimator: face estimator Returns: corresponding attribute name Raises: ValueError("Bad estimator"): if attribute name not found """ for estimatorName in self.__slots__: if estimatorName == "_faceEngine": continue if estimator.name.lower() == estimatorName[1:-len("Estimator")]: return estimatorName raise ValueError("Bad estimator") def initEstimator(self, estimator: FaceEstimator) -> None: """ Create an estimator. Create new estimator with help self._faceengine Args: estimator: estimator for creating Raises: ValueError("Bad estimator type"): if estimator not found """ if estimator == FaceEstimator.BasicAttributes: self._basicAttributesEstimator = self._faceEngine.createBasicAttributesEstimator( ) elif estimator == FaceEstimator.Eye: self._eyeEstimator = self._faceEngine.createEyeEstimator() elif estimator == FaceEstimator.Emotions: self._emotionsEstimator = self._faceEngine.createEmotionEstimator() elif estimator == FaceEstimator.GazeDirection: self._gazeDirectionEstimator = self._faceEngine.createGazeEstimator( ) elif estimator == FaceEstimator.MouthState: self._mouthStateEstimator = self._faceEngine.createMouthEstimator() elif estimator == FaceEstimator.WarpQuality: self._warpQualityEstimator = self._faceEngine.createWarpQualityEstimator( ) elif estimator == FaceEstimator.HeadPose: self._headPoseEstimator = self._faceEngine.createHeadPoseEstimator( ) elif estimator == FaceEstimator.AGS: self._AGSEstimator = self._faceEngine.createAGSEstimator() elif estimator == FaceEstimator.Descriptor: self._descriptorEstimator = self._faceEngine.createFaceDescriptorEstimator( ) else: raise ValueError("Bad estimator type") @property def descriptorEstimator(self) -> FaceDescriptorEstimator: """ Get head pose estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._descriptorEstimator is None: self._descriptorEstimator = self._faceEngine.createFaceDescriptorEstimator( ) return self._descriptorEstimator @property def headPoseEstimator(self) -> HeadPoseEstimator: """ Get head pose estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._headPoseEstimator is None: self._headPoseEstimator = self._faceEngine.createHeadPoseEstimator( ) return self._headPoseEstimator @headPoseEstimator.setter def headPoseEstimator(self, newEstimator: HeadPoseEstimator) -> None: """ Set head pose estimator. Args: newEstimator: new estimator """ self._headPoseEstimator = newEstimator # pylint: disable=C0103 @property def AGSEstimator(self) -> AGSEstimator: """ Get ags estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._AGSEstimator is None: self._AGSEstimator = self._faceEngine.createAGSEstimator() return self._AGSEstimator # pylint: disable=C0103 @AGSEstimator.setter def AGSEstimator(self, newEstimator: AGSEstimator) -> None: """ Set ags estimator. Args: newEstimator: new estimator """ self._AGSEstimator = newEstimator @property def basicAttributesEstimator(self) -> BasicAttributesEstimator: """ Get basic attributes estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._basicAttributesEstimator is None: self._basicAttributesEstimator = self._faceEngine.createBasicAttributesEstimator( ) return self._basicAttributesEstimator @basicAttributesEstimator.setter def basicAttributesEstimator( self, newEstimator: BasicAttributesEstimator) -> None: """ Set basic attributes estimator. Args: newEstimator: new estimator """ self._basicAttributesEstimator = newEstimator @property def eyeEstimator(self) -> EyeEstimator: """ Get eye estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._eyeEstimator is None: self._eyeEstimator = self._faceEngine.createEyeEstimator() return self._eyeEstimator @eyeEstimator.setter def eyeEstimator(self, newEstimator: EyeEstimator) -> None: """ Set eye estimator. Args: newEstimator: new estimator """ self._eyeEstimator = newEstimator @property def emotionsEstimator(self) -> EmotionsEstimator: """ Get emotions estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._emotionsEstimator is None: self._emotionsEstimator = self._faceEngine.createEmotionEstimator() return self._emotionsEstimator @emotionsEstimator.setter def emotionsEstimator(self, newEstimator: EmotionsEstimator) -> None: """ Set emotions estimator. Args: newEstimator: new estimator """ self._emotionsEstimator = newEstimator @property def gazeDirectionEstimator(self) -> GazeEstimator: """ Get gaze direction estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._gazeDirectionEstimator is None: self._gazeDirectionEstimator = self._faceEngine.createGazeEstimator( ) return self._gazeDirectionEstimator @gazeDirectionEstimator.setter def gazeDirectionEstimator(self, newEstimator: GazeEstimator) -> None: """ Set gaze direction estimator. Args: newEstimator: new estimator """ self._gazeDirectionEstimator = newEstimator @property def mouthStateEstimator(self) -> MouthStateEstimator: """ Get mouth state estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._mouthStateEstimator is None: self._mouthStateEstimator = self._faceEngine.createMouthEstimator() return self._mouthStateEstimator @mouthStateEstimator.setter def mouthStateEstimator(self, newEstimator: MouthStateEstimator) -> None: """ Set mouth state estimator. Args: newEstimator: new estimator """ self._mouthStateEstimator = newEstimator @property def warpQualityEstimator(self) -> WarpQualityEstimator: """ Get warp quality estimator. If estimator is initialized it will be returned otherwise it will be initialized and returned Returns: estimator """ if self._warpQualityEstimator is None: self._warpQualityEstimator = self._faceEngine.createWarpQualityEstimator( ) return self._warpQualityEstimator @warpQualityEstimator.setter def warpQualityEstimator(self, newEstimator: WarpQualityEstimator) -> None: """ Set warp quality estimator. Args: newEstimator: new estimator """ self._warpQualityEstimator = newEstimator @property def faceEngine(self) -> VLFaceEngine: """ Get current faceengine. Returns: self._faceEngine """ return self._faceEngine @faceEngine.setter def faceEngine(self, newFaceEngine: VLFaceEngine) -> None: """ Set new faceengine. All initialize estimators will be re-initialized. Args: newFaceEngine: new faceengine """ self._faceEngine = newFaceEngine for estimatorName in self.__slots__: if estimatorName == "_faceEngine": continue if getattr(self, estimatorName) is not None: self.initEstimator( FaceEstimatorsCollection._getEstimatorByAttributeName( estimatorName)) self.warper = self._faceEngine.createWarper() def removeEstimator(self, estimator: FaceEstimator) -> None: """ Remove estimators. Args: estimator: estimator for removing """ estimatorName = self._getAttributeNameByEstimator(estimator) setattr(self, estimatorName, None)