CoolFace
Apppublic

LuftLiu/southeast_asian_flags_classification

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py87 linesDownload Raw Back to root
1from fastai.vision.all import *2from io import BytesIO3import requests4import streamlit as st5 6"""7# Southeast Asian Flags Classifier8This is a classifier for flags of Southeast Asian countries. It will attempt to identify the country to which the flag image belongs.9"""10 11def predict(img):12    st.image(img, caption="Your image", use_column_width=True)13    pred, key, probs = learn_inf.predict(img)14    probability = probs[key].item()*10015    # st.write(learn_inf.predict(img))16    if probability > 70:17        if not pred == 'Unknown':18            f"""19            ### Prediction result: {pred}20            ### Probability of {pred}: {probability: .2f}%21            """22        else:23            f"""24            ### Prediction result: Not the flag of a Southeast Asian country25            ### Probability: {probability: .2f}%26            """27    else:28        f"""29        ### Prediction result: Classification failed (low probability value)30        ### Probability: {probability: .2f}%31        """32 33 34path = "./"35# learn_inf = load_learner(path + "south_east_asia_flags_stage-3.pkl")36learn_inf = load_learner(path + "south_east_asia_flags2_2.pkl")37 38option = st.radio("", ["Upload Image", "Image URL", "See Examples"])39 40if option == "Upload Image":41    uploaded_file = st.file_uploader("Please upload an image.")42 43    if uploaded_file is not None:44        img = PILImage.create(uploaded_file)45        predict(img)46 47elif option == "Image URL":48    url = st.text_input("Please input a url.")49 50    if url != "":51        try:52            response = requests.get(url)53            pil_img = PILImage.create(BytesIO(response.content))54            predict(pil_img)55 56        except:57            st.text("Problem reading image from", url)58 59else:60    item = st.radio(61        "Choose an example of Southeast Asian flag image",62        ["Brunei Darussalam", "Cambodia", "East Timor", "Indonesia", "Laos", "Malaysia", "Myanmar", "Philippines", "Singapore", "Thailand", "Vietnam"],63        horizontal=True)64    65    if item == 'Brunei Darussalam':66        predict(img = 'brunei.png')67    elif item == 'Cambodia':68        predict(img = 'cambodia.png')69    elif item == 'East Timor':70        predict(img = 'east_timor.png')71    elif item == 'Indonesia':72        predict(img = 'indonesia.png')73    elif item == 'Laos':74        predict(img = 'laos.png')75    elif item == 'Malaysia':76        predict(img = 'malaysia.png')77    elif item == 'Myanmar':78        predict(img = 'myanmar.png')79    elif item == 'Philippines':80        predict(img = 'philippines.png')81    elif item == 'Singapore':82        predict(img = 'singapore.png')83    elif item == 'Thailand':84        predict(img = 'thailand.png')85    elif item == 'Vietnam':86        predict(img = 'vietnam.png')87