NodeJS face recognition (3)

The first two articles on the use of NodeJS provided by the official SDK for rapid development, but the SDK , after all, is packaged omitted authentication request to initiate a series of operations, this article does not use the SDK to develop, but direct use of API development. We can verify permission from scratch, and then initiate a request to call API to achieve all the features of the previous two articles achieved.

First, we look at the official document calling for API requirements:

17781779-f2aec5bb0321821b

First, we need API key  and Secret  Key to generate the access_token . Document has provided ready-made Node.js critical code, but this article adopt its own code logic. access_token valid for 30 days, after which the need to re-acquire. First, let's get the access_token :

First, based on our own to encapsulate a request of the http request:

17781779-43e3084be69b60ed

我们封装的第一个http请求方法采用Content-typeapplication/x-www-form-urlencoded,请求方式为POST请求。然后我们看下文档获取access_token的文档要求:

17781779-2b1d09cf09c7eed5

获取access_token很简单,就是一个post请求,将三个参数传过去,接下来我们代码实现下,调用我们封装好的http请求:

17781779-e220bb54f2fc0a3b

我们来调下接口看看是否有效果:

17781779-bea34e98783d2232

可以看到我们成功获取到了access_token。获取到了access_token其实我们已经完成了鉴权操作,http请求我们也已经进行封装,下一步其实就可以开始调用API实现上一篇文章的人脸识别功能了。本篇文章就不一一测试所有接口,会着重挑几个接口来自己封装代码调取API获取数据。首先我们一样先调取人脸检测接口试试水:

人脸检测接口:

我们先来看看文档对该接口的介绍:

17781779-1b168b6266ba7328

可以看到接口要求Content-type要求为application/json,所以说我们需要封装一个以json格式提交请求的http请求:

17781779-d746d588e2ec3818

然后我们需要看看本接口需要提交的请求参数:

17781779-329a9bc8fe29f6d8

有了请求参数,我们就可以开始着手写接口:

17781779-4054b4011e9729e6

首先调用API每个接口都需要携带access_token进行鉴权,所以请求接口地址为:

CONFIG.detectURL ='https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=24.a86eea7696e1b0d370be73eaf9daf02d.2592000.1562144929.282335-16399628';

我先只提交必需参数imageimage_type。我们可以调取接口看看是否能得到图片检测结果:

17781779-e64ffeb0605bec5c

可以看到我们成功得到人脸检测API的响应了,因为默认只返回人脸框、概率和旋转角度,我们添加选传参数返回更多信息:

17781779-5632bef2bfae1e4d

我设置返回所有参数,图片检测人脸数最多为5,照片类型为生活照。我们可以看下返回参数(返回参数过多我就简单截图下)

17781779-0936c6600bfefb3b

可以看到接口完全按照我们的需要将全部参数进行返回,我们就可以在客户端根据需要对人脸进行聚焦等特效的控制。就比如可以针对接口返回的72个特征点或者150个特征点的具体坐标进行具体操作:

17781779-282b6861a0db0347
17781779-70f288bc94d5ac84

(图片来源于官方文档)

其他的接口调用方式和本接口完全一致的,这里就不一一讲解了。具体流程:

通过封装的第一个http请求:

'Content-Type':'application/x-www-form-urlencoded'

可以获取到access_token,有效期为30天,这里可以选择30天后access失效再重新获取新的access_token,或者每次调用接口获取新的access_token

然后通过封装的第二个http请求:

"content-type":'application/json'

将接口请求参数以json方式提交请求对应API,即可以获得对应API的正确响应。

使用SDK与调用API的区别:

1.鉴权SDK封装好了,我们只需要传入API KEY以及secret KEY就可以完成鉴权。直接调取API我们需要先通过http请求获取access_token

2.SDK封装好http请求,我们可以直接调用,而无需关心如何实现。直接调取API我们需要封装一个基于x-www-form-urlencoded以及一个基于jsonhttp请求。

3.实现人脸识别功能API使用SDK可以直接直接封装好的方法,而调取API我们则需要手动封装方法调用已封装好的http请求发起请求。

总结:

In fact, the use of artificial intelligence carried out a very simple basis of previous-based. Wu Kun We use existing SDK or own package calls the API , in fact, the difficulty factor is not high, because the most difficult to realize the face recognition logic operations existing API has been achieved, we do not need to care about face recognition to identify how it , and simply call the API can be identified. Face to Benpian series is over, there is the principle interested can study on their own to carry out research. If you like my articles, please pay attention to my personal public number: Chow plots

17781779-caa2291259233090

Guess you like

Origin blog.csdn.net/weixin_34072857/article/details/90808475