Giter VIP home page Giter VIP logo

face-anti-spoofing's Introduction

Viet Money - Face detection & anti-spoofing

Pure Python - Face detection & anti-spoofing. Support Web API & Command-line interface.

Prerequisite

Manual


# Install Pytorch cuda if using NVIDIA GPU device. Default: CPU device

> pip3 install torch==1.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

or

# CUDA 10.2
> pip3 install torch==1.5.0

# CUDA 10.1
> pip3 install torch==1.5.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

# CUDA 9.2
> pip3 install torch==1.5.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html
  • Python package requirements:
> pip3 install -r requirements.txt

Docker


Support Docker with environment setting:

# edit API config in `.example` or container env
> cp .example .env
> docker-compose build && docker-compose up -d

Table of Contents

Features

Face detection


drawing

Face anti-spoofing detection


drawing

Get started

NOTE: all method work in RGB pixel format. (OpenCV pixel format is BGR -> convert before using)

Face detection

drawing

  • Python API
from library.util.image import imread
from library.face_detector import FaceDetector

face_detector = FaceDetector("data/pretrained/retina_face.pth.tar")

image = imread("images/fake_001.jpg") # image in RGB format
faces = face_detector(image)

>>> faces # [[box, score, land_mark]]
[(array([181,   5, 551, 441], dtype=int32), 
  0.99992156, 
  array([[249, 147],
         [412, 145],
         [306, 192],
         [266, 313],
         [404, 311]], dtype=int32))]
  • CLI
> python3 service.py detect data/test/*{.jpg,.png}

  • Web API
> curl --location --request POST 'http://localhost:8000/detect' --form 'image=@"data/test/fake_001.jpg"'

Face anti-spoofing detection


  • Python API
from library.util.image import imread
from library.face_detector import FaceDetector
from library.face_antspoofing import SpoofingDetector

face_detector = FaceDetector("data/pretrained/retina_face.pth.tar")
face_antispoofing = SpoofingDetector("data/pretrained/fasnet_v1se_v2.pth.tar")

image = imread("images/fake_001.jpg") # image in RGB format
faces = face_detector(image)

>>> face_antispoofing([box for box, _, _ in faces], image) # [(is_real, score)]
[(False, 0.5154606513679028)]

drawing

  • CLI
> python3 service.py spoofing data/test/*{.jpg,.png}

  • Web API
> curl --location --request POST 'http://localhost:8000/spoofing' --form 'image=@"data/test/fake_001.jpg"'

Documents

Python API

Face Detection

  • class library.face_detector.FaceDetector:
    • __init__:
      • model_path: (str) Path of pre-trained model
      • detect_threshold (float): Threshold of confidence score of detector. Default: 0.975
        • scale_size (int): Scale size input image. Recommend in [240, 1080]. Default: 480
        • device: device model loaded in. Default: cpu
    • process: Detect faces in a image
      • image (numpy.ndarray): image source return: List[Tuple[List[int], float, List[List[int]]]] - [(box, score, land_mark)]

Face Anti Spoofing

  • class library.face_antspoofing.SpoofingDetector:
    • __init__:
      • model_path: (str) Path of pre-trained model
      • device: device model loaded in. Default: cpu
        • face_size (tuple(int, int)): model face input size. Default: (80, 80)
    • predict: Predict faces is spoof or not.
      • boxes: face's boxes
      • image (numpy.ndarray): image source return: Sequence[Tuple[bool, float]] - [(is_real, score)]

Command-line interface


Common options

>  python service.py --help
Usage: service.py [OPTIONS] COMMAND [ARGS]...

Options:
  --detector-model TEXT       Face detector model file path
  --detector-threshold FLOAT  Face detector model threshold
  --detector-scale INTEGER    Face detector model scale. >= 240
  --spoofing-model TEXT       Face anti-spoofing file path
  --device TEXT               Device to load model.
  --version                   Show the version and exit.
  --help                      Show this message and exit.

Commands:
  api       Run service as API
  detect    Detect face in images
  spoofing  Detect spoofing face in images

Face Detection


> python service.py detect --help
Usage: service.py detect [OPTIONS] IMAGES...

  Detect face in images

Options:
  -j, --json PATH  Export result to json file
  -q, --quiet      Turn off STD output
  -c, --count      Counting image during process
  -y, --overwrite  Force write json file.
  --help           Show this message and exit.
  • Input: image's path (support file globs)
    Example: python service.py detect ./*{.jpg,.png} - match with any file with extension is jpg and png.

  • Output option:

    • --json PATH: Export result to JSON file
    {
        "nums": "int",
        "boxes": "List[int]",
        "scores": "List[float]",
        "landmarks": "List[int]"
    }
    • --quiet Turn off STD output
    • --count Counting image during process
    • --overwrite Force write JSOn file.

Face Anti Spoofing


> python service.py spoofing --help
Usage: service.py spoofing [OPTIONS] IMAGES...

  Detect spoofing face in images

Options:
  -j, --json PATH  Export result to json file
  -q, --quiet      Turn off STD output
  -c, --count      Counting image during process
  -y, --overwrite  Force write json file.
  --help           Show this message and exit.
  • Input: image's path (support file globs)
    Example: python service.py spoofing ./*{.jpg,.png} - match with any file with extension is jpg and png.

  • Output option:

    • --json PATH: Export result to json file
    {
        "nums": "int",
        "is_reals": "List[bool]",
        "scores": "List[float]",
        "boxes": "List[int]"
    }
    • --quiet Turn off STD output
    • --count Counting image during process
    • --overwrite Force write json file.

Web API Hosting


> python service.py api --help
Usage: service.py api [OPTIONS]

  Run service as API

Options:
  --host TEXT     API host. Default: localhost
  --port INTEGER  API port. Default: 8000
  --version TEXT  API version.
  --help          Show this message and exit.

Run with default uvicorn setting:

> python service.py api

INFO:     Started server process [19802]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)

Support Docker with environment setting:

# edit API config in `.example` or container env
> cp .example .env
> docker-compose build && docker-compose up -d

Web API


Face Detection

  • Method: POST
  • URL: /detect
  • Form-data params:
    • images: File or URL of image

Face Anti Spoofing

  • Method: POST
  • Path: /spoofing
  • Form-data params:
    • images: File or URL of image

License

Licensed under the Apache License, Version 2.0

References

Contact

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.