In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use API to achieve face recognition in NodeJS, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
First, let's take a look at the official documentation's requirements for calling API:
First of all, we need to generate access_token through API key and Secret key. The documentation provides off-the-shelf Node.js key code, but this article uses its own code logic. The access_token is valid for 30 days. If it expires, you need to get it again. First, let's get the access_token:
First, let's encapsulate a request-based http request:
The first http request method we encapsulated uses Content-type as application/x-www-form-urlencoded and the request method as POST request. Then let's take a look at the document requirements for getting access_token:
It is very simple to get access_token, which is a post request, passing three parameters. Next, we implement the code and call our encapsulated http request:
Let's adjust the API to see if it works:
You can see that we have successfully obtained the access_token. After obtaining the access_token, we have already completed the authentication operation, and we have encapsulated the http request. In fact, the next step is to call API to implement the face recognition feature of the previous article. This article will not test all interfaces one by one, but will focus on several interfaces to encapsulate their own code to call API to get data. First of all, let's call the face detection API to try the water:
Face detection API:
Let's first take a look at the document's introduction to this interface:
You can see that the API requires Content-type to be application/json, so we need to encapsulate a http request that submits the request in json format:
Then we need to take a look at the request parameters to be submitted for this API:
With the request parameters, we can start writing the interface:
To call API, each API needs to carry access_token for authentication, so the request API address is:
CONFIG.detectURL = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=24.a86eea7696e1b0d370be73eaf9daf02d.2592000.1562144929.282335-16399628';
I will only submit the required parameter image,image_type first. We can call the API to see if we can get the image detection result:
You can see that we have successfully got the response of the face detection API, because only the face frame, probability and rotation angle are returned by default. We add optional parameters to return more information:
I set to return all the parameters, the maximum number of faces detected in the picture is 5, and the photo type is life photo. We can take a look at the returned parameters (if there are too many returned parameters, I will simply take a screenshot)
We can see that the interface returns all the parameters exactly according to our needs, and we can control the special effects such as focusing on the face on the client side as needed. For example, you can perform specific operations on the specific coordinates of 72 feature points or 150 feature points returned by the API:
(the picture comes from the official document)
Other APIs are called in the same way as this API, so I won't explain them all here. Specific process:
Through the first encapsulated http request:
'Content-Type': 'application/x-www-form-urlencoded'
You can obtain the access_token, which is valid for 30 days. Here, you can choose to retrieve the new access_token after the access expires, or to obtain the new access_token each time the API is called.
Then through the encapsulated second http request:
"content-type": 'application/json'
If the API request parameters are submitted to the corresponding API in json mode, the correct response of the corresponding API can be obtained.
The difference between using SDK and calling API:
1. The authentication SDK is encapsulated. We only need to input API KEY and secret KEY to complete the authentication. To directly call API, we need to obtain access_token through http request first.
2.SDK encapsulates the http request, and we can call it directly without worrying about how to implement it. To retrieve API directly, we need to encapsulate a x-www-form-urlencoded-based and a json-based http request.
3. To implement the face recognition function, API can be directly encapsulated by SDK, while to retrieve API, we need to manually encapsulate the method to call the encapsulated http request to initiate the request.
Thank you for reading this article carefully. I hope the article "how to use API to achieve face recognition in NodeJS" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.