def buildDescriptorIndex(): """ Build index and search. """ faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) warper = faceEngine.createFaceWarper() extractor = faceEngine.createFaceDescriptorEstimator() descriptorsBatch = faceEngine.createFaceDescriptorFactory( ).generateDescriptorsBatch(2) for image in (EXAMPLE_O, EXAMPLE_1): vlImage = VLImage.load(filename=image) faceDetection = detector.detectOne(vlImage) warp = warper.warp(faceDetection) faceDescriptor = extractor.estimate(warp.warpedImage) descriptorsBatch.append(faceDescriptor) indexBuilder = faceEngine.createIndexBuilder() indexBuilder.appendBatch(descriptorsBatch) pprint.pprint(f"index buf size: {indexBuilder.bufSize}") index = indexBuilder.buildIndex() pprint.pprint(index[0]) result = index.search(faceDescriptor, 1) pprint.pprint(f"result: {result}")
async def asyncEstimateBasicAttributes(): """ Async estimate basic attributes. """ image = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetections = detector.detect([image])[0] warper = faceEngine.createFaceWarper() warps = [warper.warp(faceDetection) for faceDetection in faceDetections] basicAttributesEstimator = faceEngine.createBasicAttributesEstimator() basicAttributes = await basicAttributesEstimator.estimate( warps[0].warpedImage, estimateAge=True, estimateGender=True, estimateEthnicity=True, asyncEstimate=True) pprint.pprint(basicAttributes.asDict()) task1 = basicAttributesEstimator.estimate(warps[0].warpedImage, estimateAge=True, estimateGender=True, estimateEthnicity=True, asyncEstimate=True) task2 = basicAttributesEstimator.estimate(warps[0].warpedImage, estimateAge=True, estimateGender=True, estimateEthnicity=True, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get().asDict())
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.createFaceWarper() warp = warper.warp(faceDetection) landMarks5Transformation = warper.makeWarpTransformationWithLandmarks(faceDetection, "L5") gazeEstimator = faceEngine.createGazeEstimator() warpWithLandmarks5 = WarpWithLandmarks5(warp, landMarks5Transformation) pprint.pprint(gazeEstimator.estimate(warpWithLandmarks5).asDict()) faceDetection2 = detector.detectOne(VLImage.load(filename=EXAMPLE_1), detect68Landmarks=True) warp2 = warper.warp(faceDetection2) landMarks5Transformation2 = warper.makeWarpTransformationWithLandmarks(faceDetection2, "L5") warpWithLandmarks5List = [ WarpWithLandmarks5(warp, landMarks5Transformation), WarpWithLandmarks5(warp2, landMarks5Transformation2), ] estimations = gazeEstimator.estimateBatch(warpWithLandmarks5List) pprint.pprint([estimation.asDict() for estimation in estimations])
def matchFacesFromImages(): """ Match faces from images. """ faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) extractor = faceEngine.createFaceDescriptorEstimator() warper = faceEngine.createFaceWarper() matcher = faceEngine.createFaceMatcher() image1 = VLImage.load(filename=EXAMPLE_O) faceDetection1 = detector.detectOne(image1) warp1 = warper.warp(faceDetection1) descriptor1 = extractor.estimate(warp1.warpedImage) image2 = VLImage.load(filename=EXAMPLE_1) faceDetection2 = detector.detectOne(image2) warp2 = warper.warp(faceDetection2) descriptor2 = extractor.estimate(warp2.warpedImage) batch, _ = extractor.estimateDescriptorsBatch( [warp1.warpedImage, warp2.warpedImage]) print(matcher.match(descriptor1, descriptor2)) print(matcher.match(descriptor1, batch)) print(matcher.match(descriptor1, [descriptor2, descriptor1]))
async def asyncEstimateMedicalMask(): """ Async medical mask estimation example """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) medicalMaskEstimator = faceEngine.createMaskEstimator() # Estimate from detection pprint.pprint(medicalMaskEstimator.estimate(faceDetection).asDict()) # Estimate from wrap warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) mask = await medicalMaskEstimator.estimate(warp.warpedImage, asyncEstimate=True) pprint.pprint(mask.asDict()) warp2 = warper.warp(detector.detectOne(VLImage.load(filename=EXAMPLE_1))) task1 = medicalMaskEstimator.estimate(warp.warpedImage, asyncEstimate=True) task2 = medicalMaskEstimator.estimate(warp2.warpedImage, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get())
def estimateRedEye(): """ Red-eye estimation example. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) landMarks5Transformation = warper.makeWarpTransformationWithLandmarks( faceDetection, "L5") redEyeEstimator = faceEngine.createRedEyeEstimator() warpWithLandmarks = WarpWithLandmarks5(warp, landMarks5Transformation) pprint.pprint(redEyeEstimator.estimate(warpWithLandmarks).asDict()) image2 = VLImage.load(filename=EXAMPLE_1) faceDetection2 = detector.detectOne(image2) warp2 = warper.warp(faceDetection2) landMarks5Transformation2 = warper.makeWarpTransformationWithLandmarks( faceDetection2, "L5") warpWithLandmarksList = [ WarpWithLandmarks5(warp, landMarks5Transformation), WarpWithLandmarks5(warp2, landMarks5Transformation2), ] estimations = redEyeEstimator.estimateBatch(warpWithLandmarksList) pprint.pprint([estimation.asDict() for estimation in estimations])
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.createFaceWarper() 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))
async def asyncEstimateEyes(): """ Async eyes estimation example. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) landMarks5Transformation = warper.makeWarpTransformationWithLandmarks( faceDetection, "L5") eyesEstimator = faceEngine.createEyeEstimator() warpWithLandmarks = WarpWithLandmarks(warp, landMarks5Transformation) eyes = await eyesEstimator.estimate(warpWithLandmarks, asyncEstimate=True) pprint.pprint(eyes.asDict()) image2 = VLImage.load(filename=EXAMPLE_1) faceDetection2 = detector.detectOne(image2) warp2 = warper.warp(faceDetection2) landMarks5Transformation2 = warper.makeWarpTransformationWithLandmarks( faceDetection2, "L5") task1 = eyesEstimator.estimateBatch( [WarpWithLandmarks(warp, landMarks5Transformation)], asyncEstimate=True) task2 = eyesEstimator.estimateBatch( [WarpWithLandmarks(warp2, landMarks5Transformation2)], asyncEstimate=True) for task in (task1, task2): estimations = task.get() pprint.pprint([estimation.asDict() for estimation in estimations])
def createWarp(): """ Create face warp from detection. """ faceEngine = VLFaceEngine() image = VLImage.load(filename=EXAMPLE_O) detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) pprint.pprint(warp.warpedImage.rect)
def estimateGlasses(): """ Create warp to detection. """ image = VLImage.load(filename=EXAMPLE_3) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) glassesEstimator = faceEngine.createGlassesEstimator() pprint.pprint(glassesEstimator.estimate(warp.warpedImage).asDict())
def estimateCredibility(): """ Estimate credibility of a person """ image = VLImage.load(filename=EXAMPLE_1) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) credibilityEstimator = faceEngine.createCredibilityEstimator() pprint.pprint(credibilityEstimator.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.createFaceWarper() warp = warper.warp(faceDetection) emotionEstimator = faceEngine.createEmotionEstimator() pprint.pprint(emotionEstimator.estimate(warp.warpedImage).asDict())
def estimateHeadwear(): """ Estimate headwear from a warped image. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) eyebrowEstimator = faceEngine.createHeadwearEstimator() pprint.pprint(eyebrowEstimator.estimate(warp.warpedImage).asDict())
def estimateImageColorType(): """ Estimate image color type from a warped image. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) imageColorTypeEstimator = faceEngine.createImageColorTypeEstimator() pprint.pprint(imageColorTypeEstimator.estimate(warp.warpedImage).asDict())
def estimateWarpQuality(): """ Create warp from detection. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) qualityEstimator = faceEngine.createWarpQualityEstimator() pprint.pprint(qualityEstimator.estimate(warp.warpedImage).asDict())
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.createFaceWarper() 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()
async def asyncEstimateEmotion(): """ Async estimate emotion from a warped image. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) emotionEstimator = faceEngine.createEmotionEstimator() emotions = await emotionEstimator.estimate(warp.warpedImage, asyncEstimate=True) pprint.pprint(emotions.asDict()) task = emotionEstimator.estimate(warp.warpedImage, asyncEstimate=True) pprint.pprint(task.get().asDict())
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.createFaceWarper() 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.createFaceWarper() warp = warper.warp(faceDetection) landMarks5Transformation = warper.makeWarpTransformationWithLandmarks( faceDetection, "L5") eyesEstimator = faceEngine.createEyeEstimator() pprint.pprint( eyesEstimator.estimate(landMarks5Transformation, warp.warpedImage).asDict())
async def asyncEstimateDescriptor(): """ Async estimate face descriptor. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) extractor = faceEngine.createFaceDescriptorEstimator() pprint.pprint(await extractor.estimateDescriptorsBatch( [warp.warpedImage, warp.warpedImage], asyncEstimate=True)) # run tasks and get results task1 = extractor.estimate(warp.warpedImage, asyncEstimate=True) task2 = extractor.estimate(warp.warpedImage, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get().asDict())
async def asyncEstimateMouthState(): """ Async mouth state estimation example. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) mouthEstimator = faceEngine.createMouthEstimator() mouth = await mouthEstimator.estimate(warp.warpedImage, asyncEstimate=True) pprint.pprint(mouth.asDict()) task1 = mouthEstimator.estimate(warp.warpedImage, asyncEstimate=True) task2 = mouthEstimator.estimate(warp.warpedImage, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get())
async def asyncEstimateCredibility(): """ Async estimate credibility of a person """ image = VLImage.load(filename=EXAMPLE_1) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) credibilityEstimator = faceEngine.createCredibilityEstimator() estimation = await credibilityEstimator.estimate(warp.warpedImage, asyncEstimate=True) pprint.pprint(estimation.asDict()) task1 = credibilityEstimator.estimate(warp.warpedImage, asyncEstimate=True) task2 = credibilityEstimator.estimate(warp.warpedImage, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get().asDict())
def estimateDescriptor(): """ Estimate face descriptor. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) extractor = faceEngine.createFaceDescriptorEstimator() pprint.pprint(extractor.estimate(warp.warpedImage)) pprint.pprint( extractor.estimateDescriptorsBatch( [warp.warpedImage, warp.warpedImage])) batch, aggregateDescriptor = extractor.estimateDescriptorsBatch( [warp.warpedImage, warp.warpedImage], aggregate=True) pprint.pprint(batch) pprint.pprint(aggregateDescriptor)
def estimateMedicalMask(): """ Medical mask estimation example """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) medicalMaskEstimator = faceEngine.createMaskEstimator() # Estimate from detection pprint.pprint(medicalMaskEstimator.estimate(faceDetection).asDict()) # Estimate from wrap warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) pprint.pprint(medicalMaskEstimator.estimate(warp.warpedImage).asDict()) warp2 = warper.warp(detector.detectOne(VLImage.load(filename=EXAMPLE_1))) pprint.pprint(medicalMaskEstimator.estimateBatch([warp, warp2]))
async def estimateGlassesAsync(): """ Example of an async glasses estimation. """ image = VLImage.load(filename=EXAMPLE_3) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) # async estimation glassesEstimator = faceEngine.createGlassesEstimator() pprint.pprint((await glassesEstimator.estimate(warp.warpedImage, asyncEstimate=True)).asDict()) # run tasks and get results task1 = glassesEstimator.estimate(warp.warpedImage, asyncEstimate=True) task2 = glassesEstimator.estimate(warp.warpedImage, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get().asDict())
async def estimateGazeDirectionAsync(): """ Example of an async gaze direction estimation. """ image = VLImage.load(filename=EXAMPLE_O) faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V3) faceDetection = detector.detectOne(image, detect68Landmarks=True) warper = faceEngine.createFaceWarper() warp = warper.warp(faceDetection) landMarks5Transformation = warper.makeWarpTransformationWithLandmarks(faceDetection, "L5") gazeEstimator = faceEngine.createGazeEstimator() warpWithLandmarks5 = WarpWithLandmarks5(warp, landMarks5Transformation) # async estimation pprint.pprint((await gazeEstimator.estimate(warpWithLandmarks5, asyncEstimate=True)).asDict()) # run tasks and get results task1 = gazeEstimator.estimate(warpWithLandmarks5, asyncEstimate=True) task2 = gazeEstimator.estimate(warpWithLandmarks5, asyncEstimate=True) for task in (task1, task2): pprint.pprint(task.get().asDict())