Skip to content

chnnnnng/face_recognition

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Face recognition

前言

本项目人脸识别核心采用 ageitgey/face_recognition,一个强大、简单、易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,特别是兼容树莓派系统。

因此在Clone前,你要先安装face_recognition环境(雾)

此外,该项目还使用到了Redis作为数据库,因此你要提前 service redis-server start

如果不想配置环境的话,还有一个Docker版

开始

python3 face.py

这会打开一个flask的webserver

调试

# face.py

​'''
#异常捕捉
@app.errorhandler(404)
def error404(error):
    return response().error().message("未知错误").make()
@app.errorhandler(500)
def error500(error):
    return response().error().message("未知错误").make()
@app.errorhandler(400)
def error400(error):
    return response().error().message("未知错误").make()   
​'''
  
if __name__ == "__main__":
    redis = redis(app.config['REDIS_HOST'],app.config['REDIS_PORT'],app.config['REDIS_PASSWORD'],app.config['REDIS_PREFIX'])
    app.run(host='0.0.0.0', port=80, debug=True)
    #app.run(host='0.0.0.0', port=80, debug=False)

配置

# face.py

#是否使用SECRET_KEY进行安全检测
app.config['USE_SECRET_KEY'] = False
#SECRET_KEY
app.config['SECRET_KEY'] = 'this_is_a_secret_key'
#照片上传目录
app.config['UPLOAD_FOLDER'] = 'photos/'
#每个人脸最多上传照片数量
app.config['MAX_UPLOAD_EACH_FACE'] = 5
#redis host
app.config['REDIS_HOST'] = 'localhost'
#redis port
app.config['REDIS_PORT'] = 6379
#redis password
app.config['REDIS_PASSWORD'] = ''
#redis key 前缀
app.config['REDIS_PREFIX'] = 'FRS:'
#允许上传的类型
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'}

uWSGI

uwsgi --ini uwsgi.ini
;uwsgi.ini

[uwsgi]
socket = /face/face.sock
;nginx通过socket与uwsgi通信
chdir = /face
;项目根目录,根据实际修改
wsgi-file = /face/face.py
;face.py文件的位置
callable = app
processes = 2
threads = 8
buffer-size = 65525
master = true
vacuum = true
#	/etc/nginx/nginx.conf

  server {
	  listen 80; #监听端口
	  location / {
	    try_files $uri @face;
	  }
	  location @face {
	    include uwsgi_params;
	    uwsgi_pass unix:/face/face.sock; #nginx通过socket与uwsgi通信
	  }
	}	

DEMO页

注册人脸Demo:http://localhost:80/demo/register

人脸识别Demo:http://localhost:80/demo

添加照片Demo:http://localhost:80/demo/upload

API

#注册人脸
#POST
#参数:faceid 必须 要保证唯一性 作为每一个人脸的唯一编号
#     photos  可选 要上传的照片
@app.route('/register', methods=['POST'])
#添加人脸
#POST
#参数:faceid 必须
#     photos  必须
@app.route('/upload', methods=['POST'])

注意:注册人脸和添加人脸时,图片的文件名不能出现非ascii字符(当然,你可以用secure_filename来过滤非ascii字符,但是这样的话对于一些名字全部是中文/日文的图片,会引发异常。因此建议在调用api上传之前,提前修改文件名,如使用 secure_filename过滤 再与 随机字符串拼接)

#查询人脸照片
#GET
#参数:faceid 必须
@app.route('/photos', methods=['GET'])

如果上传的照片检测不到人脸,会带来502错误,但是我懒得改了,可以自己在异常捕捉里加一条,或者在predict前判断是否是人脸!

#查看某一张照片
#GET
#参数:faceid 必须
#     filename 必须
@app.route('/photo', methods=['GET'])
#删除指定照片
#POST
#参数:faceid 必须
#     filename list 必须
@app.route('/del_photos', methods=['POST'])
#删除指定Face
#POST
#参数:faceid 必须
@app.route('/del_face', methods=['POST'])
#训练模型
#GET
#参数:无
@app.route('/train', methods=['GET'])
#识别
#POST
#参数:photo 必须
@app.route('/recognize', methods=['POST'])

使用docker

只有amd64!

放在阿里云容器镜像服务

#拉取镜像
sudo docker pull registry.cn-hangzhou.aliyuncs.com/ch-ng/face_recognition_server:v1
#运行镜像
sudo docker run --name FRS -it -v /wherever/you/like:/face registry.cn-hangzhou.aliyuncs.com/ch-ng/face_recognition_server:v1

/wherever/you/like 你可以挂载到任何你喜欢的目录,如/home/chng/face

:后面的/face 是不可改变的

容器内包含了redis-server,不需要使用外部的服务

你可以修改/wherever/you/like 中的代码,记得docker restart FRS

/etc/nginx/nginx.conf中的uwsgi_passunix:/wherever/you/like/uwsgi.sock;

当然uwsgi你也可以选择httphttp-socket,根据情况修改nginx.conf

About

简单易用的人脸识别服务器

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published