xsolve-pl/face-validator-bundle

Symfony3 扩展包,用于使用 MS Azure Face API 在图片上验证人脸

v1.0.1 2018-01-19 15:51 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:00:23 UTC


README

XSolve Face Validator 扩展包

Build Status Scrutinizer Code Quality

============================

目录

简介

此 Symfony3 扩展包允许验证图像(例如由您应用程序的用户上传)是否包含人脸。内部使用 MS Azure Face API,因此要使用它,您需要在 MS Azure 中创建一个账户。在免费套餐中,API 允许每月进行 30,000 次请求,每分钟 20 次,因此对于低流量应用程序来说应该足够使用。

以下所有功能都可以在约束级别进行配置,并且可以轻松启用/禁用

  • 要求人脸尺寸(相对于图像尺寸的比例)
  • 当人脸被遮挡时不允许图像
  • 要求头发可见(图像不得被裁剪)
  • 允许人脸在任何三个轴向上旋转到给定水平
  • 不允许佩戴眼镜
  • 不允许佩戴太阳镜
  • 不允许任何化妆
  • 要求图像不得在给定水平上模糊(低/中/高)
  • 要求图像不得在给定水平上包含噪音(低/中/高)

许可证

此库受 MIT 许可证的保护。完整的许可证请参阅 LICENSE 文件。

入门

使用 Composer 将扩展包添加到您的 Symfony3 项目中

$ composer require xsolve-pl/face-validator-bundle

您还需要在内核中注册此扩展包

<?php
// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new XSolve\FaceValidatorBundle\XSolveFaceValidatorBundle(),
        ];
    }
}

在配置文件中,您必须提供您的 MS Azure Face API 的订阅密钥 和区域名称(可以从 MS Azure 站点的 Endpoint 部分获取)。可以通过运行以下命令预览可用区域:

$ bin/console config:dump-reference xsolve_face_validator

示例配置

# app/config/config.yml

xsolve_face_validator:
    azure_subscription_key: your-subscription-key
    region: westeurope

用法

如果您想了解如何使用 Symfony 表单和验证允许用户上传文件,请参阅 此文档。假设在您的应用程序中,您已经有一些代表用户(例如 Doctrine 实体)的模型,并且它用于在表单中收集用户数据并执行验证,它已经包含了一个个人资料图片

// src/AppBundle/Entity/User.php

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use XSolve\FaceValidatorBundle\Validator\Constraints as XSolveAssert;

class User
{
    /**
     * @var UploadedFile
     *
     * @Assert\Image()
     * @XSolveAssert\Face()
     */
    public $profilePicture;
}

或使用 YML

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\User:
    properties:
        profilePicture:
            - Image
            - XSolve\FaceValidatorBundle\Validator\Constraints\Face

有关其他验证配置格式,请参阅 专门的文档部分

现在,在执行常规验证时,例如在您的控制器中

// src/AppBundle/Controller/UserController.php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\User;

class UserController extends Controller
{
    /**
     * @Route("/user/new", name="app_user_new")
     */
    public function newAction(Request $request)
    {
        $user = new User();
        $form = $this->createFormBuilder($user)
            ->add('profilePicture', FileType::class)
            ->getForm();
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // ...
        }

        // ...
    }
}

将验证图像是否包含人脸。人脸的验证方式是可定制的,下面展示了所有可能的选项及其默认值

// src/AppBundle/Entity/User.php

use Symfony\Component\Validator\Constraints as Assert;
use XSolve\FaceValidatorBundle\Validator\Constraints as XSolveAssert;

class User
{
    /**
     * @var Symfony\Component\HttpFoundation\File\UploadedFile

     * @Assert\Image()
     * @XSolveAssert\Face(
     *     minFaceRatio = 0.15,
     *     allowCoveringFace = true,
     *     maxFaceRotation = 20.0,
     *     allowGlasses = true,
     *     allowSunglasses = true,
     *     allowMakeup = true,
     *     allowNoHair = true,
     *     maxBlurLevel = high,
     *     maxNoiseLevel = high,
     *     noFaceMessage = 'Face is not visible.',
     *     faceTooSmallMessage = 'Face is too small.',
     *     faceCoveredMessage = 'Face cannot be covered.',
     *     hairCoveredMessage = 'Hair cannot be covered.',
     *     tooMuchRotatedMessage = 'Face is too much rotated.',
     *     glassesMessage = 'There should be no glasses in the picture.',
     *     sunglassesMessage = 'There should be no sunglasses in the picture.',
     *     makeupMessage = 'The person should not be wearing any makeup.',
     *     blurredMessage = 'The picture is too blurred.',
     *     noiseMessage = 'The picture is too noisy.'
     * )
     */
    public $profilePicture;
}

请注意,您可以选择省略上述任何(甚至所有)选项,然后将使用默认值。

对于模糊和噪音级别,可能的选项有

同样,您也可以像使用任何其他 Symfony 验证器一样直接针对给定值(文件路径或 \SplFileInfo 实例)使用它。

// src/AppBundle/Controller/ImageController.php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class ImageController extends Controller
{
    public function validateAction(Request $request)
    {
        /* @var $validator ValidatorInterface */
        $validator = $this->get('validator');
        $constraintViolations = $validator->validate(
            '/path/to/your/image/file.png',
            new Face([
                // you can pass the options mentioned before to the validation constraint
            ])
        );
    }
}