In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to identify flower species in Python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Since we are not very familiar with many flowers, we need to use software to identify what kind of flowers they are. There are many flower recognition software on the market, such as flower companion, shape and color, Baidu and so on. After my test, I found that Baidu's recognition effect is the best. So I had an idea, can you call Baidu's interface in batch to identify and classify flower photos?
Baidu image recognition
Baidu's image recognition interface can accurately identify more than 100, 000 objects and scenes, including more than 10 high-precision map recognition capabilities and provide corresponding API services.
Https://cloud.baidu.com/product/imagerecognition
We follow the steps to create a new application and get our own API Key and Secret Key, as shown in the following figure.
Image recognition provides a composite API interface to support the flexible combination of a variety of vertical recognition services, here only need to call plant recognition to meet the demand.
How to Python call Baidu image recognition API interface?
The first step is to call the authentication API to obtain 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 type of image
Enter the following command in an interactive environment:
Import requestsimport base64request_url ='*'# Open the picture file f = open (rudd:\ download\ QQ screenshot 20220407203203.pngphotos, 'rb') img = base64.b64encode (f.read ()) params = {"image": img} request_url = request_url + "? access_token=" + access_tokenheaders = {' content-type': 'application/x-www-form-urlencoded'} response = requests.post (request_url, data=params) in binary mode Headers=headers) if response: print (response.json () ['result'] [0] [' name']))
Output:
Cherry blossom
The return parameters of calling Baidu image recognition API are shown in the figure below. For us, only the name (plant name) parameter is needed.
Read photo file
I store the photos 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 an interactive environment:
Import ospath = "D:\ download\ Flower Collection" filenames = os.listdir (path) filenames
Output:
['QQ screenshot 20220405223301.png'
'Screenshot of QQ 20220405223320.png'
.
'Wechat Picture _ 20220405225020.jpg'
'Wechat Picture _ 20220405225023.jpg']
The listdir () substitution method in the os module receives two path parameters, path, and returns a list of the names of all the parts under that path. In this way, we get the file names of all the flower pictures under the path, as shown in the following figure.
Sort out the classified photos
Then, we can use the for loop statement to identify the images of the flower photos in turn, and sort them into the corresponding folder according to the identified names.
Enter the following command in an 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)
Among them, the get_fname () function is that we encapsulate the Baidu image recognition code in the previous article as a custom function, which can be called here to return the flower name flower_name corresponding to the photo.
The subsequent code is almost the same as the previously shared automatic sorting file, that is, if determines whether a folder corresponding to the flower name already exists, and if it does not, create it. Finally, call the shutil module to move the flower photos to the corresponding folder.
The specific implementation effect, as shown in the moving picture below.
These are all the contents of the article "how to identify flower species in Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.