[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace

Overview

Create an environment (Docker image) that can run OpenFace with Python3 + Ubuntu16.04, and try running the sample code of OpenFace face recognition (comparison of the distance between two faces) on the container.

What is OpenFace

Created by Brandon Amos of Carnegie Mellon University. It is open source specialized for face recognition using deep neural networks. It is a Python and Torch implementation and is based on CVPR 2015. Below is a summary of the algorithm and release notes. https://cmusatyalab.github.io/openface/

OpenFace algorithm, (1) Face detection with pre-trained model with dlib or OpenCV (2) Convert so that the positions of the eyes and lower lip in the face are the same using the affine transformation of OpenCV and the real-time pose estimation of dlib. (3) Represent (or embed) faces on a 128-dimensional unit hypersphere using a deep neural network, and perform face recognition by clustering, similarity detection, and classification tasks. ... apparently ...

Premise

-Docker is installed on the PC

procedure

1. Creating a Dockerfile

Create the following Dockerfile in the working directory.

I created it for the time being, so there may be extra commands.

Dockerfile


FROM ubuntu:16.04

#apt package update
#Install the required apt packages
RUN apt-get update && apt-get upgrade -y && apt-get install -y wget sudo python-setuptools python-pip liblapack-dev libatlas-base-dev gfortran g++ build-essential libgtk2.0-dev libjpeg-dev libtiff5-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev unzip cmake git python-dev python-numpy libboost-dev libboost-python-dev libboost-system-dev
#Install the required pip packages
RUN sudo pip install -U pip && sudo pip install numpy==1.12.0 scipy matplotlib cython scikit-image dlib pandas txaio

#Install Torch
WORKDIR /root
RUN git clone https://github.com/torch/distro.git ~/torch --recursive
WORKDIR /root/torch
RUN sudo dpkg --configure -a
RUN bash install-deps
RUN ./install.sh -y
RUN . /root/.bashrc
#PATH of th command
RUN export PATH=/root/torch/bin:$PATH;
RUN export LD_LIBRARY_PATH=/root/torch/lib:$LD_LIBRARY_PATH;

#Install LUA package for Torch
#lua to build luarocks**Install in advance as you will need.
RUN sudo apt-get install lua5.3
RUN sudo apt-get install -y lua5.3-dev

#Install luarocks
RUN mkdir luarocks
WORKDIR /root/torch/luarocks
RUN wget https://luarocks.org/releases/luarocks-3.3.1.tar.gz
RUN tar -xf luarocks-3.3.1.tar.gz
WORKDIR /root/torch/luarocks/luarocks-3.3.1
RUN ./configure
RUN make
RUN make install

#LUA package installation
RUN for NAME in dpnn nn optim optnet csvigo cutorch cunn fblualib torchx tds image nngraph; do sudo /root/torch/install/bin/luarocks install $NAME; done

#Install OpenFace
RUN git clone https://github.com/cmusatyalab/openface ~/openface --recursive
WORKDIR /root/openface
RUN sudo python setup.py install
RUN sudo python3 setup.py install

#Install additional required items
RUN ./models/get-models.sh
RUN ./demos/web/install-deps.sh
RUN sudo pip install -r demos/web/requirements.txt

#Install what you need to run OpenFace on python3
WORKDIR /root
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
RUN sudo python3 get-pip.py && python3 -m pip install -U pip
RUN sudo apt-get install -y python3-dev
# OpenCV,dlib installation
RUN python3 -m pip install opencv_python dlib

terminal


usermac:~ user$ ls {Working directory}
Dockerfile #Created Dockerfile

2. Build a Docker image (docker build)

terminal


docker build -t openface_ubuntu16_python3:1.0 {Relative or absolute path to the working directory where Docker files are located}

3. Run the container (docker run)

terminal


docker run -it -v {Absolute path of working directory}:/home/ openface_ubuntu16_python3:1.0 /bin/bash .

The working directory of the host and the home directory of the container are shared (volume).

Absolute path confirmation command

terminal


usermac:work user$ pwd
/Users/{username}/openface_sample/work

4. Run the OpenFace demo code

Check the OpenFace source and demo code in ~ / openface.

container


root@bb9464b46285:~# ls openface/
CONTRIBUTING.md  LICENSE    api-docs         build    data   docs        images      models                        openface          run-tests.sh  tests     util
Dockerfile       README.md  batch-represent  cloc.sh  demos  evaluation  mkdocs.yml  opencv-dlib-torch.Dockerfile  requirements.txt  setup.py      training

This time, run the demo code compare.py to determine if the faces in the two photos are the same person.

container


root@bb9464b46285:~# python3 openface/demos/compare.py openface/images/examples/lennon-1.jpg openface/images/examples/lennon-2.jpg 
Comparing images/examples/lennon-1.jpg with images/examples/lennon-2.jpg.
  + Squared l2 distance between representations: 0.782  #Execution result

I want to compare ʻopenface / images / examples / lennon-1.jpg and ʻopenface / images / examples / lennon-2.jpg specified after python3 openface / demos / compare.py 2 Specify the number of images. This time, I used the sample image placed under openface. If the face cannot be detected from the specified image, an error will occur.

The distance between the two faces compared by 0.782. The closer it is to 0, the more likely it is that they are the same person. It seems that the standard value needs to be judged by verifying it with the photos actually used.

Recommended Posts

[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Try face recognition with Python
Try face recognition with python + OpenCV
Try face recognition with Generated Photos
Face recognition with camera with opencv3 + python2.7
[python, openCV] base64 Face recognition with images
Serverless face recognition API made with Python
Try scraping with Python.
Face recognition with Edison
Use python with docker
Now, let's try face recognition with Chainer (prediction phase)
Easy face recognition try with Jetson Nano and webcam
Now, let's try face recognition with Chainer (learning phase)
Face recognition with Python's OpenCV
Try Python output with Haxe 3.2
Face recognition with Amazon Rekognition
Face detection with Python + dlib
Build python3 environment with ubuntu 16.04
Prepare python3 environment with Docker
Face detection with Python + OpenCV
Try running Python with Try Jupyter
Face recognition / cutting with OpenCV
Try Selenium Grid with Docker
Try building JupyterHub with Docker
Number recognition in images with Python
Try scraping with Python + Beautiful Soup
Face detection with YOLO Face (Windows10, Python3.6)
Try to operate Facebook with Python
Face detection with Lambda (Python) + Rekognition
Try singular value decomposition with Python
Build Mysql + Python environment with docker
python x tensoflow x image face recognition
First Anime Face Recognition with Chainer
Cut out face with Python + OpenCV
Try frequency control simulation with Python
Application development with Docker + Python + Flask
I tried face recognition with OpenCV
[Pyenv] Building a python environment with ubuntu 16.04
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Face detection with Python + OpenCV (rotation invariant)
Try to reproduce color film with Python
Try logging in to qiita with Python
English speech recognition with python [speech to text]
Build Jupyter Lab (Python) environment with Docker
Try mathematical formulas using Σ with python
Behind the flyer: Using Docker with Python
Try working with binary data in Python
Try python
Let's try gRPC with Go and Docker
Easy introduction of speech recognition with Python
Try using Python with Google Cloud Functions
Face recognition of anime characters with Keras
Use cryptography library cryptography with Docker Python image
Create Python + uWSGI + Nginx environment with Docker
Created Ubuntu, Python, OpenCV environment on Docker
Quickly try Microsoft's Face API in Python
Try HTML scraping with a Python library
Try calling Python from Ruby with thrift
Launch environment with LineBot + Heroku + Docker + Python
Run a Python web application with Docker
Try drawing a map with python + cartopy 0.18.0