"Wuxiaoliang's Popular Science Daily" often releases videos identifying popular online organisms, which not only popularizes biological knowledge, but also satisfies the audience's curiosity. Today we will also identify popular plants on the Internet! Many flowers have bloomed in spring recently. I happened to take advantage of the Qingming holiday to go outdoors and take a lot of photos of flowers.
#Since we are not particularly familiar with many flowers, we need to use software to identify what type of flower it is. There are many flower recognition software on the market, such as Huamate, Xingse, Baidu, etc. After testing, I found that Baidu has the best recognition effect. So I had an idea. Can I batch call Baidu's interface to identify and classify flower photos? (See the end of the article for the complete code)
Baidu’s image recognition interface can accurately identify more than 100,000 objects and scenes, including more than 10 It has high-precision image recognition capabilities and provides corresponding API services.
//m.sbmmt.com/link/17d187eaf6157b4e219552d6a187290a
We follow the steps to create a new application , and get your own API Key and Secret Key, as shown in the figure below.
Image recognition provides a combination API interface that supports flexible combination calls of multiple vertical recognition services. Here, only plant recognition needs to be called to meet the needs.
How to call Baidu image recognition API interface in Python?
The first step is to call the authentication interface to obtain the token.
API_Key = '**********' Secret_Key = '**********' def get_access_token(API_Key,Secret_Key): host = '**********' response = requests.get(host) return response.json()['access_token'] access_token = get_access_token(API_Key,Secret_Key)
The second step is to identify the image type
Enter the following command in the interactive environment:
import requests import base64 request_url = '**********' # 二进制方式打开图片文件 f = open(r'D:下载QQ截图20220407203203.png', 'rb') img = base64.b64encode(f.read()) params = {"image":img} 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()['result'][0]['name'])
Output:
Sakura
The return parameters of calling Baidu image recognition interface are shown in the figure below. For us, we only need the name (plant name) parameter.
I store the photos I took in the D: download flower collection path, so I need to use the os module to read the file list , to facilitate subsequent batch operations.
Enter the following command in the interactive environment:
import os path = "D:下载花卉合集" filenames = os.listdir(path) filenames
Output:
['QQ截图20220405223301.png', 'QQ截图20220405223320.png', ...... '微信图片_20220405225020.jpg', '微信图片_20220405225023.jpg']
The listdir() method in the os module receives a The path parameter path returns a list of the file names of all files under the path. In this way, we have obtained the file names of all flower pictures under this path, as shown in the figure below.
Then, we can use the for loop statement to perform image recognition on the flower photos in sequence, and classify them according to the recognized names Organize into corresponding folders.
Enter the following command in the interactive environment:
for i in filenames: flower_name = get_fname(i) file_path = os.path.join(path,i) folder_path = os.path.join(path,flower_name) if not os.path.exists(folder_path): os.mkdir(folder_path) shutil.move(file_path,folder_path)
The get_fname() function is where we encapsulate the Baidu image recognition code in the previous article into a custom function, which can be returned when called here Get the flower_name corresponding to the photo.
The subsequent code is almost the same as the automatic classification and sorting file shared before, that is, if determines whether the folder corresponding to the flower name already exists, and if it does not exist, create it. Finally, call the shutil module to move the flower photos to the corresponding folder.
The specific execution effect is shown in the animation below.
The above is the detailed content of This is fun! Use Python to identify flower types and automatically organize and classify them!. For more information, please follow other related articles on the PHP Chinese website!