体验京东云 Serverless+AI 人脸属性识别

云原生计算基金会CNCF(Cloud Native Computing Foundation, CNCF)Serverless Whitepaper v1.0对无服务器计算作了如下定义:

Serverless computing refers to the concept of building and running applications that do not require server management. It describes a finer-grained deployment model where applications, bundled as one or more functions, are uploaded to a platform and then executed, scaled, and billed in response to the exact demand needed at the moment.

“RightScale 2018 State of the Cloud Report” 中指出, Serverless 的年增长率达到了 75%,已经远超过 Container-as-a service 的 36%,位列第一,是增长速度最快的扩展云服务。

顺应当下容器技术、IoT、AI、5G、区块链等新技术的快速发展,技术上对去中心化、轻量虚拟化、细粒度计算等需求愈发强烈,在此趋势之上,Serverless 必将快速发展并得到应用。

对于开发者而言, Serverless 极大简化了编写代码和应用部署的整个过程,无需关心 DevOps,无需关心代码运行服务器及环境,更无需关心负载均衡,中间件和存储服务可以随时调用

Q

Serverless 的优势有哪些?

  • 无需运维 用户不需要再关心服务器资源及复杂运行环境;

  • 弹性伸缩 根据请求并发量实时自动扩缩资源;

  • 按需付费 为实际消耗资源付费,无占用不收费,运行成本更低;

云上提供一整套serverless faaS+BaaS服务,帮助用户快速构建应用与服务。

今天,我们会通过 快速组合京东云 Serverless + AI,实现一个简单的 API 接口 ,进行人脸属性识别和简单数据分析。带领大家一起来感受一下Serverless的轻量虚拟化。

关键词

Serverless Service: 函数服务+API 网关

人工智能产品:

AI 人脸识别

Step1:创建函数

创建 runtime 为 python2.7 的函数,将依赖库 jdcloud_apim_sdk、ai_sdk(京东云AI)和入口文件 index.py 打包上传,index.py 代码如下:

#coding=utf-8

import json

import base64

import os

from jdcloud_apim_sdk.core.credential  import Credential

from jdcloud_apim_sdk.core.config  import Config

from jdcloud_apim_sdk.core.const  import SCHEME_HTTPS, SCHEME_HTTP

from ai_sdk.apis.ai_face_detect_attr_request  import *

from ai_sdk.client.ai_Face_Detect_Attr_client  import *

”’

AI

def handler(event,context):

if not bool(event):

result = {

‘statusCode’200 ,

‘headers’ : {},

‘body’“” ,

}

return result

# get request data

print( ‘AI Request’ )

# get request data

body = event[ ‘detail’ ][ ‘body’ ]

body = json.loads(body)

if type(body) == unicode:

body = json.loads(body)

# request

access_key =  ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’

secret_key =  ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’

credential = Credential(access_key, secret_key)

config = Config( ‘aiapi.jdcloud.com’ , scheme=SCHEME_HTTPS)

client = AiFaceDetectAttrClient(credential, config)

header = dict()

parameters = {

‘return_landmark’ : 0 ,

‘return_attr’ : 1 ,

‘face_detect_num’ : 0

}

data = body.get( ‘data’“” )

imageBase64Data =  ‘imageBase64=’ + data.decode( “utf-8” )

s = imageBase64Data.encode( ‘utf-8’ )

ai_face_detect_attr_request = AiFaceDetectAttrRequest(parameters, s, header)

AiFaceDetectAttr_response = client.send(ai_face_detect_attr_request)

respBody = { ‘code’0‘data’ : AiFaceDetectAttr_response.json()}

resp = json.dumps(respBody)

result = {

‘statusCode’200 ,

‘headers’ : {},

‘body’ : resp,

}

# statistics

resp = AiFaceDetectAttr_response.json()

if resp[ ‘result’ ][ ‘status’ ] ==  0 :

attrs = json.loads(resp[ ‘result’ ][ ‘result’ ])

if len(attrs[ ‘faces’ ]) >  0 :

if attrs[ ‘faces’ ][ 0 ][ ‘faceAttribute’ ][ ‘gender’ ] ==  0 :

print( “lookup ai attr success, this is a woman” )

else :

print( “lookup ai attr success, this is a man” )

return result

Step2:测试函数

将任意人脸图片 base64 以 json 格式放入测试事件的 body 中,即可进行在线测试,返回结果如下:

Step3:对接 API 网关

为上面的函数创建一个 API 网关触发器,将 API 发布至测试环境。 https://www.jdcloud.com/cn/products/face-detect-and-analysis ?utm_source=PMM_Wechat&utm_medium=ReadMore_179&utm_campaign=ReadMore&utm_term=NA

Step4:测试验证上线

接口验证无误后,即可与前端页面开发联调,测试完成后,可通过函数版本、别名功能管理线上函数迭代发布。

现在就点击小程序,看看 AI 眼中的你吧!

Serverless 联合京东云 AI,还可打包集成人体识别、语音技术、图像理解、OCR 文字识别等一系列 AI 场景,通过函数服务还可进一步进行数据的存储分析处理、灵活应用,只等大家来探索实践啦!