This article describes how to use Python to quickly implement a plant recognition app. It only takes about 20 lines of code to implement such an app. Let’s take a look at the effect:

In addition, I also developed the micro channel small program version, you can experience.

The realization of the APP mainly consists of two steps: front-end interface development and back-end plant identification service. The following are respectively introduced.

There are many ways to implement the front end, just said small program is one, but for Python developers, we still want to be able to develop the interface through Python. Streamlit is a tool for data scientists and machine learning engineers to quickly build beautiful apps without having to worry about web layout, styling, service deployment, etc., just like normal Python programs. Streamlit installation is also simple by executing the PIP install Streamlit command. Those who want to get started with Streamlit can check out the next article.

Next, we developed the front-end interface. As can be seen from the above GIF, the core logic is to receive the pictures input by users and display them.

import streamlit as st

# Set the site title
st.title('Plant Recognition')

# Picture selection box uploaded_file = st.file_uploader('Select a picture', type=['jpg'.'png']) if uploaded_file is not None:  # show the selected file  st.image(uploaded_file, caption='Selected files', use_column_width=True) Copy the code

After the interface is completed, we will consider the recognition service of plant pictures. Here I use baidu AI service


To install the Python SDK for Baidu AI, run the PIP install Bidu-aip command. Then, go to Baidu AI open platform to register an account to get the corresponding APP_KEY and SCRET_KEY. Write code to invoke the plant identification service

from aip import AipImageClassify

APP_ID = 'xxx'  # change your APP_ID
API_KEY = 'xxx' # replace your own API_KEY
SECRET_KEY = 'xxx' # Change your own SECRET_KEY
client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)  "" plant identification Results "" res = client.plantDetect(image) Call Baidu API to identify plants Copy the code

Finally, the returned results can be displayed on the APP. The complete code is as follows

import streamlit as st
from aip import AipImageClassify

APP_ID = 'xxx'
API_KEY = 'xxx'
SECRET_KEY = 'xxx' client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)  # Set the site title st.title('Plant Recognition')  # Picture selection box uploaded_file = st.file_uploader('Select a picture', type=['jpg'.'png']) if uploaded_file is not None:  # show the selected file  st.image(uploaded_file, caption='Selected files', use_column_width=True)  bs = uploaded_file.read()   "" plant identification Results ""  res = client.plantDetect(bs) Call Baidu API to identify plants  res['result'] Display the output   """ The plant most likely is """, res['result'] [0] ['name'] # Take the result with the highest probability of prediction Copy the code

Run the streamlit run plant_detect.py command to start the app. The following output is displayed

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
The Network URL: http://192.168.1.3:8501Copy the code

Access the specified address in the browser.

I hope you found this article useful, and I’ll be sharing this AI widget on a regular basis. In addition, the complete code (including wechat mini program) has been opened, the public account background reply keyword plant recognition can obtain complete information.

Welcome the public account “du Code” to export the dry goods you can’t see elsewhere.