Repository navigation
Expand file tree
/
Copy pathlocal_prototype.py
More file actions
72 lines (55 loc) · 2.17 KB
/
Copy pathlocal_prototype.py
File metadata and controls
72 lines (55 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 25 12:29:07 2020
@author: ms101
"""
# bird classifier app
#imports
import streamlit as st
import numpy as np
import torch
from PIL import Image
from fastai.vision.all import load_learner, Path
#app
st.title("Bird Classifier")
st.write("""
You can use this app to classify birds from images.
Did you take a picture of a bird, but aren't sure which species it is?
No Problem! Just upload your image and let the classifier tell you!
*Note: This first version only knows birds from Europe (100 Species) and North America (224 Species), classifiers for other continents will be implemented soon*
""")
uploaded_file = st.file_uploader("Choose an image...", type="jpg") #file upload
continents = {"Europe": "EU",
"North America":"NA",
"South America":"SA",
"Africa":"AF",
"Asia":"AS",
"Australia Oceania":"AU",
"Antarctica":"AN"}
habitat = st.sidebar.selectbox("On which continent did you spot this bird?", list(continents.keys()))
#load model
try:
learn_inf = load_learner(Path(f"C:/Users/ms101/OneDrive/DataScience_ML/projects/birds_classifier/App/classifiers/{continents.get(habitat)}_first_34.pkl"))#load trained model
learn_inf.model.cpu()
except FileNotFoundError:
st.title("There seems to be no classifier for this habitat, please choose another one")
#classify
if uploaded_file is not None:
img = Image.open(uploaded_file)
st.image(img, caption='Your Image.', use_column_width=True)
st.write("")
st.write("Classifying...")
image = np.asarray(img)
label,prob_idx,prob = learn_inf.predict(image)
prob_perc = round(float(prob[prob_idx])*100,2)
if label[0] in "AEIOU":
st.write("## This looks like an")
else:
st.write("## This looks like a")
st.title(label.replace("_"," "))
st.write(f"The classifier is {prob_perc}% sure")
st.write("""*Note: if the classifier is not at least 90 percent sure,
there is a good chance that it either does not know the species
or the picture is not of a high enough quality*""")
#check whether model selection works
st.write(f"C:/Users/ms101/OneDrive/DataScience_ML/projects/birds_classifier/{continents.get(habitat)}_first_34.pkl")