Giter Club home page Giter Club logo

batch-face's Introduction

Batch Face for Modern Research

🚧Documentation under construction, check tests folder for more details. 🚧

This repo provides the out-of-box face detection and face alignment with batch input support and enables real-time application on CPU.

Features

  1. Batch input support for faster data processing.
  2. Smart API.
  3. Ultrafast with inference runtime acceleration.
  4. Automatically download pre-trained weights.
  5. Minimal dependencies.

Requirements

  • Linux, Windows or macOS
  • Python 3.5+ (it may work with other versions too)
  • opencv-python
  • PyTorch (>=1.0)
  • ONNX (optional)

While not required, for optimal performance it is highly recommended to run the code using a CUDA enabled GPU.

Install

The easiest way to install it is using pip:

pip install git+https://github.com/elliottzheng/batch-face.git@master

No extra setup needs, most of the pretrained weights will be downloaded automatically.

If you have trouble install from source, you can try install from PyPI:

pip install batch-face

the PyPI version is not guaranteed to be the latest version, but we will try to keep it up to date.

Usage

You can clone the repo and run tests like this

python -m tests.camera

Face Detection

Detect face and five landmarks on single image
import cv2
from batch_face import RetinaFace

detector = RetinaFace(gpu_id=0)
img = cv2.imread("examples/obama.jpg")
faces = detector(img, cv=True) # set cv to False for rgb input, the default value of cv is False
box, landmarks, score = faces[0]
Running on CPU/GPU

In order to specify the device (GPU or CPU) on which the code will run one can explicitly pass the device id.

from batch_face import RetinaFace
# 0 means using GPU with id 0 for inference
# default -1: means using cpu for inference
detector = RetinaFace(gpu_id=0) 
GPU(GTX 1080TI,batch size=1) GPU(GTX 1080TI,batch size=750) CPU(Intel(R) Core(TM) i7-7800X CPU @ 3.50GHz)
FPS 44.02405810720893 96.64058005582535 15.452635835550483
SPF 0.022714852809906007 0.010347620010375976 0.0647138786315918
Batch input for faster detection

Detector with CUDA process batch input faster than the same amount of single input.

import cv2
from batch_face import RetinaFace

detector = RetinaFace()
img= cv2.imread('examples/obama.jpg')[...,::-1]
all_faces = detector([img,img]) # return faces list of all images
box, landmarks, score = all_faces[0][0]

Note: All the input images must of the same size, for input images with different size, please use detector.pseudo_batch_detect.

Face Alignment

face alignment on single image
from batch_face import drawLandmark_multiple, LandmarkPredictor, RetinaFace

predictor = LandmarkPredictor(0)
detector = RetinaFace(0)

imgname = "examples/obama.jpg"
img = cv2.imread(imgname)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

faces = detector(img)

if len(faces) == 0:
    print("NO face is detected!")
    exit(-1)

# the first input for the predictor is a list of face boxes. [[x1,y1,x2,y2]]
results = predictor(faces, img, from_fd=True) # from_fd=True to convert results from our detection results to simple boxes

for face, landmarks in zip(faces, results):
    img = drawLandmark_multiple(img, face[0], landmarks)

References

batch-face's People

Contributors

agoniko avatar elliottzheng avatar simone1999 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

batch-face's Issues

Batch Inference with RetinaFace is slower than single image inference of Insightface

`
import numpy as np
from insightface.app import FaceAnalysis
import cv2
import matplotlib.pyplot as plt
import time
from batch_face import RetinaFace

img = cv2.imread('/home/zeeshan/Downloads/musk.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (640, 640))
plt.imshow(img)

model = FaceAnalysis(allowed_modules=['detection'],
providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
model.prepare(ctx_id=0)
detector = RetinaFace(gpu_id=0)

tik = time.time()
faces = model.get(img)
faces = model.get(img)
faces = model.get(img)
faces = model.get(img)
faces = model.get(img)
print(f"time taken: {time.time()-tik}")

tik = time.time()
faces = detector.detect([img, img, img, img, img])
print(f"time taken: {time.time()-tik}")
`
@elliottzheng could you please have a look at this code and help me to solve this issue?

Installing Error

When running command
pip install git+https://github.com/elliottzheng/batch-face.git@master
on Ubuntu 20.04.4 LTS x86_64
I get this error

ERROR: Command errored out with exit status 1: command: /home/alibustami/anaconda3/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-fk2_4kiw/setup.py'"'"'; __file__='"'"'/tmp/pip-req-build-fk2_4kiw/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-dylf4bz4 cwd: /tmp/pip-req-build-fk2_4kiw/ Complete output (9 lines): Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-req-build-fk2_4kiw/setup.py", line 5, in <module> import batch_face File "/tmp/pip-req-build-fk2_4kiw/batch_face/__init__.py", line 2, in <module> from .utils import drawLandmark_multiple, detection_adapter, bbox_from_pts, Aligner File "/tmp/pip-req-build-fk2_4kiw/batch_face/utils.py", line 1, in <module> import cv2 ModuleNotFoundError: No module named 'cv2' ---------------------------------------- WARNING: Discarding git+https://github.com/elliottzheng/batch-face.git@master. Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Problem loading model on CPU

Hi there, thanks for this fast face detection,
this is not actually an issue, it's just a small bug report,
there is a bug when loading the model on cpu, although I set the device id to -1 but is still gets me the cuda map location error,
cuple of code lines in alignment.py file can alleviate the problem:
def load_model(model, pretrained_path, load_to_cpu, network: str): if pretrained_path is None: url = pretrained_urls[network] if load_to_cpu: current_device = torch.device('cpu') else: current_device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') pretrained_dict = torch.utils.model_zoo.load_url(url, map_location = current_device)
Thanks again

face alignment

Hi,
I like to align the detected face based on the returned landmarks, but I could not find any example in the provided files or maybe I don't know the function name? could you give me a hint?

I like to do something like the one done in the following snippet similar to Insightface:
M, pose_index = estimate_norm(landmark, image_size, mode) warped = cv2.warpAffine(img, M, (image_size, image_size), borderValue=0.0)
TNX

Download URL for Resnet50 is Missing

Hello!

The load_net allow us to choose resnet50 but the download link is missing.
Can you share with this model?

pretrained_urls = {
    "mobilenet": "https://github.com/elliottzheng/face-detection/releases/download/0.0.1/mobilenet0.25_Final.pth"
}

Problem if image too big

The current model is encountering an issue when the image size is too large, as it returns multiple incorrect bounding boxes. One way to fix this is to resize the image after reading it.

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.