今天要整的是免费使用ams 提供的serverless 实现 lambda ,通过lambda 实现人脸识别(依赖face_recognition实现),我们来看看,以下四个人,他们的相识度会怎么样呢?
两种实现方式:
1. 本地运行测试没问题,通过serverless 部署到 ams lambda ,通过http 触发调用( 这篇讲实现这个简单的FaaS)。
2. 本地打好依赖层包,lambda 控制台把依赖加上,然后控制台边写代码,边调整验证。(下期)
直接来,预选准备:
1. aws账号,注册免费用。(https://aws.amazon.com/cn/)
2. 安装serverless 框架。(https://cloud.tencent.com/document/product/1154/42990)
3. 本地python 环境,3.8
第一种实现: 本地就绪,直接上传。
1. 本地安装python 库face_recognition
#我本地已安装,所以会提示已安装,如果你安装有问题,可以留言我
>pip3.8 install cmake dlib face_recognition
Requirement already satisfied: cmake in /usr/local/lib/python3.8/site-packages (3.18.2.post1)
Requirement already satisfied: dlib in /usr/local/lib/python3.8/site-packages (19.21.0)
Requirement already satisfied: face_recognition in /usr/local/lib/python3.8/site-packages (1.3.0)
Requirement already satisfied: face-recognition-models>=0.3.0 in /usr/local/lib/python3.8/site-packages (from face_recognition) (0.3.0)
Requirement already satisfied: Click>=6.0 in /usr/local/lib/python3.8/site-packages (from face_recognition) (7.1.2)
Requirement already satisfied: numpy in /usr/local/lib/python3.8/site-packages (from face_recognition) (1.19.2)
Requirement already satisfied: Pillow in /usr/local/lib/python3.8/site-packages (from face_recognition) (7.2.0)
2. 利益face_recognition API,比较程序如下:
# coding=utf-8
import face_recognition
def match_handler(event, context):
# 通过face_distance 函数来判断两张脸是否相识,相识度是多少
# 此模型训练在0.6 以内的距离值,就认为是同一个人,如果想要更严格,可以把距离值设置的更新,比如设置值0.55
jay = face_recognition.load_image_file("jay.png")
jay1 = face_recognition.load_image_file("jay1.png")
GZ = face_recognition.load_image_file("GZ.png")
CXK = face_recognition.load_image_file("CXK.png")
# Get the face encodings for the known images
jay_face_encoding = face_recognition.face_encodings(jay)[0]
jay1_face_encoding = face_recognition.face_encodings(jay1)[0]
GZ_face_encoding = face_recognition.face_encodings(GZ)[0]
CXK_face_encoding = face_recognition.face_encodings(CXK)[0]
known_encodings = [
jay_face_encoding,
jay1_face_encoding,
GZ_face_encoding,
CXK_face_encoding
]
dictTag = {"0": "jay", "1": "jay1", "2": "gz", "3": "cxk"}
result = "=====================CXK跟JAY,GZ比========================\r\n"
print("=====================CXK跟JAY,GZ比========================")
image_to_test = face_recognition.load_image_file("CXK.png")
image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
# See how far apart the test image is from the known faces
face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
for i, face_distance in enumerate(face_distances):
print("CXK 与{},相识度差距 {:.2} #{}".format(dictTag[str(i)], face_distance, i))
print("-差距值为普通的 0.6, 他们为同一人的对比结果是: {}".format(face_distance < 0.6))
print("- 更严格的差距设置为0.5 , 他们为同一人的对比结果是: {}".format(face_distance < 0.5))
result+= "CXK 与{},相识度差距 {:.2} #{}".format(dictTag[str(i)], face_distance, i) + "\r\n"
result+="-差距值为普通的 0.6, 他们为同一人的对比结果是: {}".format(face_distance < 0.6)+"\r\n"
result+="- 更严格的差距设置为0.5 , 他们为同一人的对比结果是: {}".format(face_distance < 0.5)+"\r\n"
print("=====================GZ跟JAY 相似度========================")
result+="=====================GZ跟JAY 相似度========================\r\n"
GZ_to_test = face_recognition.load_image_file("GZ.png")
GZ_to_test_encoding = face_recognition.face_encodings(GZ_to_test)[0]
# See how far apart the test image is from the known faces
face_distances = face_recognition.face_distance(known_encodings, GZ_to_test_encoding)
for i, face_distance in enumerate(face_distances):
print("GZ 与{},相识度差距 {:.2} #{}".format(dictTag[str(i)], face_distance, i))
print("-差距值为普通的 0.6, 他们为同一人的对比结果是: {}".format(face_distance < 0.6))
print("- 更严格的差距设置为0.5 , 他们为同一人的对比结果是: {}".format(face_distance < 0.5))
result +="GZ 与{},相识度差距 {:.2} #{}".format(dictTag[str(i)], face_distance, i)+"\r\n"
result +="-差距值为普通的 0.6, 他们为同一人的对比结果是: {}".format(face_distance < 0.6)+"\r\n"
result += "- 更严格的差距设置为0.5 , 他们为同一人的对比结果是: {}".format(face_distance < 0.5)+"\r\n"
response = {
"statusCode": 200,
"body": result
}
return response
if __name__ == "__main__":
print(match_handler('', ''))
3. 本地跑一跑
#python3.8 为你本地python3 的版本,可能你的是3.x
>python3.8 handler.py
运行结果:
4. 上面的步骤都比较简单,怎么把他部署到amazon上去呢?前面讲到需要借助serverless框架。
#安装python serverless依赖
>npm install --save serverless-python-requirements
npm WARN face-demo No description
npm WARN face-demo No repository field.
npm WARN face-demo No license field.
+ [email protected]
updated 1 package in 5.656s
在项目中增加依赖文件requirements.txt,内容为你跑的python依赖库。
face_recognition==1.3.0
serverless.yml ,中声明你的运行环境,函数,API
service: face-demo
frameworkVersion: '2'
provider:
name: aws
runtime: python3.8
timeout: 1000
memorySize: 1024
functions:
match_handler:
handler: handler.match_handler
events:
- http:
path: face_demo
method: get
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
provider为你的服务厂商,运行时环境时python3.8 ,运行超时时间为1000,内存限制为1024M。
函数名为match_handler, handler 为handler.match_handler。
event为定义的事件,http trigger,通过get 方法的API ,可以触发函数的调用。
部署到云:serverless deploy 命令
5. 部署成功,试试验证下
Serverless: Stack update finished...
Service Information
service: face-demo
stage: dev
region: us-east-1
stack: face-demo-dev
resources: 11
api keys:
None
endpoints:
GET - https://s9gj3grvig.execute-api.us-east-1.amazonaws.com/dev/face_demo
functions:
match_handler: face-demo-dev-match_handler
layers:
None
Serverless: Removing old service artifacts from S3...
**************************************************************************************************************************************
Serverless: Announcing Metrics, CI/CD, Secrets and more built into Serverless Framework. Run "serverless login" to activate for free..
**************************************************************************************************************************************
https://s9gj3grvig.execute-api.us-east-1.amazonaws.com/dev/face_demo 这个地址为刚才serverless.yml 定义的http trigger
访问下:
服务器日志:(GZ跟杰伦小于比较严格的0.5,系统认为是同一人,CXK当然跟杰伦,广智都不像,脱口秀大会说他像杰伦,站在程序员角度,那还真是)
看看ams的控制台: