preface

Recently there is a flower recognition project to write, but before writing, first call Baidu interface to try fresh.

Although it is convenient to directly call the other person’s interface, for those who write this thing for the first time, they have no clue at the beginning and can only explore slowly. Therefore, I want to sort out the whole process and provide some reference for those who have ideas in this aspect.

Reprint the article please indicate the source: juejin.cn/post/684490…

1. Platform login

First, baidu search “Baidu AI Open platform”, click the right console, enter the login interface, if you have used Baidu products (such as Baidu web disk), you can directly log in.

The next step is to select specific services. The flower recognition to be realized belongs to image recognition, so enter the control panel of image recognition. (In addition to flower recognition, image recognition also includes logo recognition, animal recognition, vehicle type recognition, etc.)

2. Create an application

To create an application, you need to apply for the interface permission for your project. The application can be invoked only after the application succeeds.

Tap Create Application to enter the application page.

Fill in the information and click Create to create the application.

After the application is created, click Application List to display the created application.

After the application is created, API keys and Secret keys are generated for subsequent operations.

3. Access token

In practice, interface calls are based on access_token instead of API Key and Secret Key

Access_token: indicates the authorization obtained from Baidu. The ACCESS_token parameter must be included in the URL when invoking API. Request data acquisition approach is to write code to the server, send the request to the authorized service address https://aip.baidubce.com/oauth/2.0/token (it is recommended to use POST), and in the URL with the following parameters:

  • Grant_type: mandatory parameter, which is fixed to client_credentials
  • Client_id: mandatory parameter, application API Key
  • Client_secret: mandatory parameter, Secret Key of the application

Example code for obtaining access_token:

# encoding:utf-8
import requests 

Client_id specifies the API Key obtained from the official website, and client_secret specifies the Secret Key obtained from the official website.
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= & website to obtain the AK 】 【 client_secret =' official website to obtain the SK 】 【
response = requests.get(host)
if response:
    print(response.json())
Copy the code

The information returned by the server is in JSON format. Important parameters include:

  • Access_token: Access Token to be obtained
  • Expires_in: Validity period of the Access Token (in seconds)

(Other parameters can be ignored)

If the returned information is too cumbersome, you can directly obtain the access_token parameter:

print(response.json()['access_token']) Modify the last line of the sample code
Copy the code

4. Flower identification

After the above work is completed, the interface can be used to realize flower recognition.

This interface is used to identify a picture, that is, output a flower identification result for an input flower picture.

Parameter Description:

  • Request URL:aip.baidubce.com/rest/2.0/im…
  • Access_token: The access_token requested in Part 3
  • Header:
parameter value
Content-Type application/x-www-form-urlencoded

Sample code:

import requests
import base64

"# Plant Recognition"

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"

path='[photo address]'
Open the image file in binary mode
f = open(path, 'rb')
img = base64.b64encode(f.read())

params = {"image":img}
access_token = 'access_token requested'
request_url = request_url + "? access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())
Copy the code

Give a sample picture:

Recognition results:

Score can be regarded as probability, and the probability of the image being recognized as sunflower is 0.872, heart-leaf sunflower is 0.001, and black-heart golden chrysanthemum is 0.0006. Therefore, the high probability is that it is sunflower, which is consistent with our own judgment. So the recognition results are pretty good.

reference

Baidu identification technical documents