DeepFace【部署 03】轻量级人脸识别和面部属性分析框架deepface在Linux环境下服务部署(conda虚拟环境+docker)

简介: DeepFace【部署 03】轻量级人脸识别和面部属性分析框架deepface在Linux环境下服务部署(conda虚拟环境+docker)

1.使用虚拟环境[810ms]

1.1 环境部署

Anaconda的安装步骤这里不再介绍,直接开始使用。

# 1.创建虚拟环境
conda create -n deepface python=3.9.18
# 2.激活虚拟环境
conda activate deepface
# 3.安装deepface
pip install deepface -i https://pypi.tuna.tsinghua.edu.cn/simple

以下操作在虚拟环境deepface下执行:

# 1.安装mesa-libGL.x86_64
yum install mesa-libGL.x86_64
# 防止报错
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
# 2.安装deprecated
pip install deprecated==1.2.13
# 防止报错
ModuleNotFoundError: No module named 'deprecated'

使用yum install mesa-libGL.x86_64命令会在Linux系统中安装mesa-libGL包。这个包包含了Mesa 3D图形库的运行时库和DRI驱动。安装mesa-libGL包后,系统将能够支持OpenGL,这是一种用于渲染2D和3D矢量图形的跨语言、跨平台的应用程序编程接口(API)。

1.2 服务启动

DeepFace serves an API as well. You can clone [/api](https://github.com/serengil/deepface/tree/master/api) folder and run the api via gunicorn server. This will get a rest service up. In this way, you can call deepface from an external system such as mobile app or web.

cd scripts
./service.sh

Linux系统使用这个命令是前台启动,实际的启动用的是shell脚本,内容如下:

#!/bin/bash
nohup python -u ./api/api.py > ./deepfacelog.out 2>&1 &

Face recognition, facial attribute analysis and vector representation functions are covered in the API. You are expected to call these functions as http post methods. Default service endpoints will be http://localhost:5000/verify for face recognition, http://localhost:detector_backend for facial attribute analysis, and http://localhost:5000/represent for vector representation. You can pass input images as exact image paths on your environment, base64 encoded strings or images on web. Here, you can find a postman project to find out how these methods should be called.

这里仅贴出如何传递base64进行接口调用:

{
    "img_path": "data:image/,image_base64_str"
}

仅看一下base64相关源码:

def load_image(img):
    # The image is a base64 string
    if img.startswith("data:image/"):
        return loadBase64Img(img)
def loadBase64Img(uri):
    encoded_data = uri.split(",")[1]
    nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return img

2.使用Docker[680ms]

You can deploy the deepface api on a kubernetes cluster with docker. The following shell script will serve deepface on localhost:5000. You need to re-configure the Dockerfile if you want to change the port. Then, even if you do not have a development environment, you will be able to consume deepface services such as verify and analyze. You can also access the inside of the docker image to run deepface related commands. Please follow the instructions in the shell script.

修改Dockerfile,调整镜像库:

# base image
FROM python:3.8
LABEL org.opencontainers.image.source https://github.com/serengil/deepface
# -----------------------------------
# create required folder
RUN mkdir /app
RUN mkdir /app/deepface
# -----------------------------------
# Copy required files from repo into image
COPY ./deepface /app/deepface
COPY ./api/app.py /app/
COPY ./api/routes.py /app/
COPY ./api/service.py /app/
COPY ./requirements.txt /app/
COPY ./setup.py /app/
COPY ./README.md /app/
# -----------------------------------
# switch to application directory
WORKDIR /app
# -----------------------------------
# update image os
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
# -----------------------------------
# if you will use gpu, then you should install tensorflow-gpu package
# RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org tensorflow-gpu
# -----------------------------------
# install deepface from pypi release (might be out-of-the-date)
RUN pip install deepface -i https://pypi.tuna.tsinghua.edu.cn/simple
# -----------------------------------
# environment variables
ENV PYTHONUNBUFFERED=1
# -----------------------------------
# run the app (re-configure port if necessary)
EXPOSE 5000
CMD ["gunicorn", "--workers=1", "--timeout=3600", "--bind=0.0.0.0:5000", "app:create_app()"]

官网启动命令:

cd scripts
./dockerize.sh

报错:

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/deepface/scripts/Dockerfile: no such file or directory
Unable to find image 'deepface:latest' locally
docker: Error response from daemon: pull access denied for deepface, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.

解决【不要 cd scripts】原因是执行脚本的文件夹要跟构建镜像使用的Dockerfile同级:

./scripts/dockerize.sh
# 这个过程一共有两个步骤:1是构建镜像;2是启动容器。构建镜像的速度取决于网速【时间可能会比较久】

分解步骤:

# 构建镜像
docker build -t deepface_image .
# 创建模型文件夹【并将下载好的模型文件上传】
mkdir -p /root/.deepface/weights/
# 启动容器
docker run --name deepface --privileged=true --restart=always --net="host" -v /root/.deepface/weights/:/root/.deepface/weights/ -d deepface_image
目录
相关文章
|
15天前
|
消息中间件 监控 RocketMQ
Docker部署RocketMQ5.2.0集群
本文详细介绍了如何使用Docker和Docker Compose部署RocketMQ 5.2.0集群。通过创建配置文件、启动集群和验证容器状态,您可以快速搭建起一个RocketMQ集群环境。希望本文能够帮助您更好地理解和应用RocketMQ,提高消息中间件的部署和管理效率。
186 91
|
16天前
|
存储 NoSQL Redis
Docker 部署 Redis
在使用 Docker 部署 Redis 时,为实现数据持久化,需正确挂载容器内的数据目录到宿主机。推荐命令如下: ``` docker run -d --name redis -v /mnt/data/redis:/data -p 6379:6379 redis ``` 该命令将宿主机的 `/mnt/data/redis` 目录挂载到容器的 `/data` 目录,确保 Redis 数据持久化。此路径更通用,适合大多数场景。避免使用不匹配的挂载路径,如 `/var/lib/redis` 或 `/mnt/data/redis` 到非默认目录,以防止数据无法正确持久化。
|
6天前
|
人工智能 文字识别 安全
Stirling-PDF:51.4K Star!用Docker部署私有PDF工作站,支持50多种PDF操作,从此告别在线工具
Stirling-PDF 是一款基于 Docker 的本地化 PDF 编辑工具,支持 50 多种 PDF 操作,包括合并、拆分、转换、压缩等,同时提供多语言支持和企业级功能,满足个人和企业用户的多样化需求。
54 6
Stirling-PDF:51.4K Star!用Docker部署私有PDF工作站,支持50多种PDF操作,从此告别在线工具
|
23天前
|
JavaScript 前端开发 Docker
如何通过pm2以cluster模式多进程部署next.js(包括docker下的部署)
通过这些步骤,可以确保您的Next.js应用在多核服务器上高效运行,并且在Docker环境中实现高效的容器化管理。
99 44
|
3天前
|
中间件 关系型数据库 数据库
docker快速部署OS web中间件 数据库 编程应用
通过Docker,可以轻松地部署操作系统、Web中间件、数据库和编程应用。本文详细介绍了使用Docker部署这些组件的基本步骤和命令,展示了如何通过Docker Compose编排多容器应用。希望本文能帮助开发者更高效地使用Docker进行应用部署和管理。
33 19
|
9月前
|
弹性计算 Java PHP
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
对于初次购买阿里云产品的用户来说,第一步要做的是注册账号并完成实名认证,然后才是购买阿里云服务器或者其他云产品,本文为大家以图文形式展示一下新手用户从注册阿里云账号、实名认证到购买云服务器完整详细教程,以供参考。
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
|
8月前
|
文字识别 算法 API
视觉智能开放平台产品使用合集之uniapp框架如何使用阿里云金融级人脸识别
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
184 0
|
机器学习/深度学习 搜索推荐 计算机视觉
【阿里云OpenVI-人脸感知理解系列之人脸识别】基于Transformer的人脸识别新框架TransFace ICCV-2023论文深入解读
本文介绍 阿里云开放视觉智能团队 被计算机视觉顶级国际会议ICCV 2023接收的论文 "TransFace: Calibrating Transformer Training for Face Recognition from a Data-Centric Perspective"。TransFace旨在探索ViT在人脸识别任务上表现不佳的原因,并从data-centric的角度去提升ViT在人脸识别任务上的性能。
2330 341
|
9月前
对于阿里云OpenAPI的域名实名认证
【1月更文挑战第5天】【1月更文挑战第22篇】对于阿里云OpenAPI的域名实名认证
101 1