基本上三种方式安装 

1,安装dlib   pip安装  依赖较多 过程稍微复杂

2,docker 快速安装   省事

3,使用官方推荐的虚拟机镜像  服务器不方便

 

--------------

ps。本文是制作镜像的步骤

如果不想自己制作,直接下载别人做好的镜像即可

pull下来

run起来就能用了

https://hub.docker.com/search?q=face_recognition&type=image&page=2

---------------

 

项目地址

https://github.com/ageitgey/face_recognition

建议看英文readme  中文docker讲的少了信息

Dockerfile路径

https://github.com/ageitgey/face_recognition/blob/master/Dockerfile

Docker菜鸟教程

https://www.runoob.com/docker/docker-tutorial.html

 

 

1,准备服务器Ubuntu18.04

2,安装docker

等待安装完成

 

测试docker是否正常

 

# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:4fe721ccc2e8dc73622dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

常用操作

查找镜像 

docker search hello-world

获取镜像

docker pull hello-world

删除镜像

docker rmi hello-world

查看镜像

docker images

什么是 Dockerfile?

https://www.runoob.com/docker/docker-dockerfile.html

 

 

3,安装face_recognition

 

前提

需要以下依赖包: python3 pip3 python-dev,libffi-dev,openssl-dev,gcc,libc-dev,和。

apt-get update -y
apt-get upgrade -y


sudo apt-get install python3
sudo apt-get install python3-pip

sudo apt-get install aptitude

sudo apt-get install aptitude build-essential
apt-get install build-essential

 

扩展swap存储   我的server内存1G太小

:~/faceDocker# mkdir /var/temp
:~/faceDocker# dd if=/dev/zero of=/var/temp/swap bs=1024 count=1024000 
1024000+0 records in
1024000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 4.69173 s, 223 MB/s
:~/faceDocker#  mkswap /var/temp/swap 
mkswap: /var/temp/swap: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 1000 MiB (1048571904 bytes)
no label, UUID=9c02658f-821a-4622-b228-b451f7b4c091
:~/faceDocker# swapon /var/temp/swap 
swapon: /var/temp/swap: insecure permissions 0644, 0600 suggested.
:~/faceDocker# free -m
              total        used        free      shared  buff/cache   available
Mem:            985         152          68           0         763         689
Swap:           999           0         999



 

 

安装git  apt-get install git

复制地址准备clone

https://github.com/ageitgey/face_recognition.git

 

:~# apt-get install git
Reading package lists... Done
Building dependency tree       
Reading state information... Done
git is already the newest version (1:2.17.1-1ubuntu0.4).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

:~# git clone https://github.com/ageitgey/face_recognition.git
Cloning into 'face_recognition'...
remote: Enumerating objects: 814, done.
remote: Total 814 (delta 0), reused 0 (delta 0), pack-reused 814
Receiving objects: 100% (814/814), 101.42 MiB | 25.27 MiB/s, done.
Resolving deltas: 100% (470/470), done.

进入docker存放目录

cd face_recognition

查看是否服务器有compose

# docker-compose --version

Command 'docker-compose' not found, but can be installed with:

snap install docker          # version 18.09.9, or
apt  install docker-compose

See 'snap info docker' for additional versions.

没有 那么需要安装

项目地址https://github.com/docker/compose/releases

curl -L https://github.com/docker/compose/releases/download/1.25.1-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
#  docker-compose --version
docker-compose version 1.25.1-rc1, build d92e9bee
构建docker并运行代码
docker-compose up --build

等待

 

 

OK

查看docker

:~/faceDocker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
face_recognition    latest              d621baff82d6        9 minutes ago       1.84GB
python              3.6-slim-stretch    c507cae2609e        2 days ago          151MB
hello-world         latest              fce289e99eb9        11 months ago       1.84kB
 

4,进入docker使用face_recognition

运行docker

root@vultr:~/faceDocker# docker run -i -t face_recognition /bin/bash
root@1b1a7a030fa3:/# ls
bin  boot  dev    etc  home  lib    lib64  media  mnt  opt    proc  root  run  sbin  srv  sys  tmp  usr  var
root@1b1a7a030fa3:/#  cat /proc/version
Linux version 4.15.0-66-generic (buildd@lgw01-amd64-044) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019
root@1b1a7a030fa3:/# 

 

复制文件到docker

实例
将主机/root/face_recognition 目录拷贝到容器dadb75280ef3的/home 目录下。
docker cp /root/face_recognition  dadb75280ef3:/home/ 

 

执行命令

直接在终端敲命令就会在docker中运行

前面符号root@dadb75280ef3 就是docker的ID

 

root@dadb75280ef3:/home/face_recognition# cd examples/

root@dadb75280ef3:/home/face_recognition/examples# ls
alex-lacamoire.png	 face_recognition_svm.py			find_faces_in_batches.py	     knn_examples	     obama2.jpg
benchmark.py		 facerec_from_video_file.py			find_faces_in_picture.py	     lin-manuel-miranda.png  obama_small.jpg
biden.jpg		 facerec_from_webcam.py				find_faces_in_picture_cnn.py	     obama-1080p.jpg	     recognize_faces_in_pictures.py
blur_faces_on_webcam.py  facerec_from_webcam_faster.py			find_facial_features_in_picture.py   obama-240p.jpg	     short_hamilton_clip.mp4
digital_makeup.py	 facerec_from_webcam_multiprocessing.py		hamilton_clip.mp4		     obama-480p.jpg	     two_people.jpg
face_distance.py	 facerec_on_raspberry_pi.py			identify_and_draw_boxes_on_faces.py  obama-720p.jpg	     web_service_example.py
face_recognition_knn.py  facerec_on_raspberry_pi_Simplified_Chinese.py	ipynb_examples			     obama.jpg		     web_service_example_Simplified_Chinese.py


root@dadb75280ef3:/home/face_recognition/examples# face_detection  .
./obama_small.jpg,65,215,169,112
./obama-240p.jpg,29,261,101,189
./biden.jpg,233,749,542,439
./obama2.jpg,302,474,611,164
./lin-manuel-miranda.png,164,577,474,267
./obama-480p.jpg,65,507,189,383
./alex-lacamoire.png,134,353,313,174
./obama-720p.jpg,94,751,273,572
./two_people.jpg,62,394,211,244
./two_people.jpg,95,941,244,792
./obama-1080p.jpg,136,1140,394,882
./obama.jpg,136,624,394,366
root@dadb75280ef3:/home/face_recognition/examples# 

初步测试OK!

 

 

参考:

docker-compose.yml

    
version: '2.3'

services:
  face_recognition:
    image: face_recognition
    container_name: face_recognition
    working_dir: /face_recognition/examples
    build:
      context: .
      #Uncomment this line to run the example on the GPU (requires Nvidia-Docker)
      # dockerfile: Dockerfile.gpu 
    command: python3 -u find_faces_in_picture_cnn.py
    volumes:
      - ./:/face_recognition
    #Uncomment this line to run the example on the GPU (requires Nvidia-Docker)
    # runtime: nvidia

参考2 Dockerfile

# This is a sample Dockerfile you can modify to deploy your own app based on face_recognition

FROM python:3.6-slim-stretch

RUN apt-get -y update
RUN apt-get install -y --fix-missing \
    build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-dev \
    libavcodec-dev \
    libavformat-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    liblapack-dev \
    libswscale-dev \
    pkg-config \
    python3-dev \
    python3-numpy \
    software-properties-common \
    zip \
    && apt-get clean && rm -rf /tmp/* /var/tmp/*

RUN cd ~ && \
    mkdir -p dlib && \
    git clone -b 'v19.9' --single-branch https://github.com/davisking/dlib.git dlib/ && \
    cd  dlib/ && \
    python3 setup.py install --yes USE_AVX_INSTRUCTIONS


# The rest of this file just runs an example script.

# If you wanted to use this Dockerfile to run your own app instead, maybe you would do this:
# COPY . /root/your_app_or_whatever
# RUN cd /root/your_app_or_whatever && \
#     pip3 install -r requirements.txt
# RUN whatever_command_you_run_to_start_your_app

COPY . /root/face_recognition
RUN cd /root/face_recognition && \
    pip3 install -r requirements.txt && \
    python3 setup.py install

CMD cd /root/face_recognition/examples && \
    python3 recognize_faces_in_pictures.py

参考3 docker 默认运行的程序find_faces_in_picture_cnn.py

from PIL import Image
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("biden.jpg")

# Find all the faces in the image using a pre-trained convolutional neural network.
# This method is more accurate than the default HOG model, but it's slower
# unless you have an nvidia GPU and dlib compiled with CUDA extensions. But if you do,
# this will use GPU acceleration and perform well.
# See also: find_faces_in_picture.py
face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

print("I found {} face(s) in this photograph.".format(len(face_locations)))

for face_location in face_locations:

    # Print the location of each face in this image
    top, right, bottom, left = face_location
    print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    # You can access the actual face itself like this:
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.show()

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐