Python calls Baidu API for animal and plant recognition
1. About the author
He Xin, female, School of Electronic Information, Xi'an Polytechnic University, 2021 graduate student
Research direction: Computer Vision
Email: [email protected]
Meng Liping, female, School of Electronic Information, Xi'an Polytechnic University, 2021 graduate student, Zhang Hongwei's artificial intelligence research group
Research direction: machine vision and artificial intelligence
Email: [email protected]
2. Baidu API animal and plant identification interface introduction
2.1 Baidu API
Baidu API is a series of application program interfaces opened by Baidu platform. Developers can call the API to directly interact with the search platform and develop their own applications based on the API. At present, Baidu's open APIs cover image technology, speech technology, text recognition, face and body recognition, AR and VR, natural language processing, knowledge maps and other interfaces, and some interfaces are free to use.
This experiment uses two interfaces of animal recognition and plant recognition in Baidu API.
2.2 Technologies used in animal and plant identification
2.2.1 Deep Learning
Deep learning is a very mainstream and popular algorithm in the image field at present. It is a type of machine learning. It is different from traditional shallow learning. The difference between deep learning is:
(1) It emphasizes the depth of the model structure;
(2) It clarifies the importance of feature learning
At present, the typical deep learning algorithms we are familiar with are Convolutional Neural Network (CNN), Recurrent Neural Network (RNN), Generative Adversarial Network (GAN) and Deep Reinforcement Learning (RL).
These networks have excellent learning ability, wide coverage, strong generalization ability, high up-line, and good portability.
Therefore, deep learning is very suitable for image feature extraction and matching, which is why most of the current image detection and recognition use deep learning networks.
3. Experimental procedure
3.1 Experimental environment
Python version: 3.6
IDLE: Pycharm
Integrated environment: Anaconda3
API: Baidu Animal/Plant Recognition API
Third-party library: requests library, Baidu api library
3.2 Dataset
Because the Baidu AI Open Platform provides a trained model, I only need to prepare the test set, but the currently available data set does not meet my test requirements, so I prepared a small set of data myself. There are 20 different kinds of animals and 20 different kinds of plants.
3.3 Experimental steps
1. Search Baidu AI Open Platform, log in to your account, and click the console
2. Search for the function you need to use
3. Click to use immediately after entering the function page
4. Click to create an application, and then select the interface you need to use. The image recognition interface has been selected here. If you cannot use it, you can click the free download below to receive the required interface resources.
5. Then we will get the AppID, API Key and Secret Key using this port
6. Next, open the pycharm with the configured environment, and obtain the token using the port according to the tutorial provided by the Baidu AI Open Platform (the tutorial is in the API reference document of the Baidu AI Open Platform)
#获取access token
#encoding:utf-8
import requests
#client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
response = requests.get(host)
if response:
print(response.json())
The result of running the above code is as follows, what we need here is the access token.
7. Finally, we can use this port for animal and plant identification. Since this port has tokens for animal identification and plant identification, we can use this token for animal identification and plant identification.
3.4 Complete experimental code
Here is the code for animal recognition, similar to plant recognition.
# encoding:utf-8
import requests
import base64
#动物识别
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
#二进制方式打开图片文件
f = open('图片路径', 'rb')
img = base64.b64encode(f.read())
params = {
"image":img}
access_token = '你的access token'
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())
3.5 Experimental results and analysis
It also has a good accuracy rate when recognizing similar animals and plants, but a misjudgment occurred when recognizing the picture of peach blossoms, and it was recognized as a beauty plum, which may be related to the clarity of my pictures.
4. Reference
Baidu AI Open Platform Technical Documentation
AI Knowledge Base: Understanding Deep Learning in One Article/ https://easyai.tech/ai-definition/deep-learning/