riyas247/butterfly-classifier
0
1from PIL import Image
2import tensorflow as tf
3import streamlit as st
4import numpy as np
5import wikipedia as wiki
6import random as rd
7
8
9# Load the butterfly classifier model
10model = tf.keras.models.load_model('butterfly-classifier-model.keras')
11classes = ['AN 88', 'BECKERS WHITE', 'CABBAGE WHITE', 'CHESTNUT TIGER', 'CLOUDED SULPHUR',
12 'JULIA', 'PERIANDER METALMARK', 'MONARCH', 'MORNING CLOAK', 'PAINTED LADY',
13 'PAPER KITE', 'PINE WHITE', 'PURPLE HAIRSTREAK', 'RED-UNDERWING SKIPPER']
14
15
16# Some names are not available in wikipedia, so using their real name from WIKIPEDIA
17real_names = {
18 'AN 88': 'Diaethria anna',
19 'JULIA': 'DRYAS IULIA',
20 'PERIANDER METALMARK' : 'RHETUS PERIANDER',
21 'PAINTED LADY': 'VANESSA CARDUI',
22 'CHESTNUT TIGER': 'PARANTICA SITA'
23}
24
25
26st.set_page_config(page_title="ButterFly Classifier.")
27
28# About the App
29st.sidebar.write(f"""This is a **BUTTTERFLY CLASSIFYING APP**, which uses a
30 ML model to predict the Genus of butterflies.
31 This Model can predict 14 different butterfly Genus.
32 They are :""")
33
34# Write each class names in the side bar
35for i in classes:
36 st.sidebar.write(f'- {i}')
37
38# About the model
39st.sidebar.write(f''':red[It will classify your input even it is not belonged to the classes mentioned above.
40 Also there is no butterfly detector is using. Even if you input an image which has
41 no butterfly, it will classify it.]''')
42
43
44st.header(":blue[Know the ButterFly.]")
45
46# Image uploader and display frame of the image
47image_frame, fileuploader = st.columns(2)
48
49uploaded_image = fileuploader.file_uploader('Upload an image of ButterFly...', type=['jpg', 'jpeg', 'png'])
50
51# showing the image
52if uploaded_image is not None:
53 image_frame.image(uploaded_image, width=350)
54
55# Make the submit button under the image uploaded
56_, submit_button, _, _, _ = st.columns(5)
57
58submit = submit_button.button("Identify")
59
60
61# Code block will preprocess the image, make the classification using the predefined model.
62# Grab data about the Genus from WikiPedia. Shows 4 images of the Genus
63
64if submit:
65
66 if uploaded_image is not None:
67
68 # Image preprocessing
69 image = Image.open(uploaded_image)
70 resized = image.resize((256, 256))
71 arr_img = np.array(resized)
72 scaled = arr_img / 255
73 arr_img = np.expand_dims(scaled, axis=0)
74
75 # Model predicting
76 preds = model.predict(arr_img)
77 pred_ind = np.argmax(preds)
78 cls = classes[pred_ind]
79
80 try:
81 summary = wiki.summary(cls, sentences=5)
82 except Exception as e: # wiki.DisambiguationError
83 real_name = real_names.get(cls)
84 summary = wiki.summary(real_name, sentences=5)
85
86 st.subheader(f"The Butterfly in the shown image is {cls}.")
87 st.header(f'{cls} : ')
88 st.write(summary)
89
90 st.write('Related images :')
91 nums = []
92 for i in range(4):
93 nums.append(rd.randint(0, 70)) # Because the min length of images is over 70
94
95 image_list = st.columns(4)
96
97 for i in range(1, 5):
98
99 try:
100 image = Image.open(f'Images_to_display/{cls.lower()}({i}).jpg')
101
102 except :
103 image = Image.open(f'Images_to_display/{cls.lower()}({i}).jpeg')
104
105 finally:
106 image_list[i-1].image(image, width=150)
107
108 else :
109 st.error('SORRY, NO IMAGE FOUND!')