-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
305 lines (284 loc) · 9.42 KB
/
app.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import streamlit as st
from PIL import Image
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras import preprocessing
import time
import requests #test commit
st.set_page_config(
page_title='''WHO'S THAT POKEMON''',
page_icon='827ed6bc234ff07054502083a9a2eb7c.jpg'
)
fig = plt.figure()
st.image('Whos-that-Pokemon.png', width=400)
class_names = ['Abra',
'Aerodactyl',
'Alakazam',
'Alolan Sandslash',
'Arbok',
'Arcanine',
'Articuno',
'Beedrill',
'Bellsprout',
'Blastoise',
'Bulbasaur',
'Butterfree',
'Caterpie',
'Chansey',
'Charizard',
'Charmander',
'Charmeleon',
'Clefable',
'Clefairy',
'Cloyster',
'Cubone',
'Dewgong',
'Diglett',
'Ditto',
'Dodrio',
'Doduo',
'Dragonair',
'Dragonite',
'Dratini',
'Drowzee',
'Dugtrio',
'Eevee',
'Ekans',
'Electabuzz',
'Electrode',
'Exeggcute',
'Exeggutor',
'Farfetchd',
'Fearow',
'Flareon',
'Gastly',
'Gengar',
'Geodude',
'Gloom',
'Golbat',
'Goldeen',
'Golduck',
'Golem',
'Graveler',
'Grimer',
'Growlithe',
'Gyarados',
'Haunter',
'Hitmonchan',
'Hitmonlee',
'Horsea',
'Hypno',
'Ivysaur',
'Jigglypuff',
'Jolteon',
'Jynx',
'Kabuto',
'Kabutops',
'Kadabra',
'Kakuna',
'Kangaskhan',
'Kingler',
'Koffing',
'Krabby',
'Lapras',
'Lickitung',
'Machamp',
'Machoke',
'Machop',
'Magikarp',
'Magmar',
'Magnemite',
'Magneton',
'Mankey',
'Marowak',
'Meowth',
'Metapod',
'Mew',
'Mewtwo',
'Moltres',
'MrMime',
'Muk',
'Nidoking',
'Nidoqueen',
'Nidorina',
'Nidorino',
'Ninetales',
'Oddish',
'Omanyte',
'Omastar',
'Onix',
'Paras',
'Parasect',
'Persian',
'Pidgeot',
'Pidgeotto',
'Pidgey',
'Pikachu',
'Pinsir',
'Poliwag',
'Poliwhirl',
'Poliwrath',
'Ponyta',
'Porygon',
'Primeape',
'Psyduck',
'Raichu',
'Rapidash',
'Raticate',
'Rattata',
'Rhydon',
'Rhyhorn',
'Sandshrew',
'Sandslash',
'Scyther',
'Seadra',
'Seaking',
'Seel',
'Shellder',
'Slowbro',
'Slowpoke',
'Snorlax',
'Spearow',
'Squirtle',
'Starmie',
'Staryu',
'Tangela',
'Tauros',
'Tentacool',
'Tentacruel',
'Vaporeon',
'Venomoth',
'Venonat',
'Venusaur',
'Victreebel',
'Vileplume',
'Voltorb',
'Vulpix',
'Wartortle',
'Weedle',
'Weepinbell',
'Weezing',
'Wigglytuff',
'Zapdos',
'Zubat']
st.title('''Gotta catch 'em all..!!''')
st.markdown(
'''- This tool will help you identify :the Pokemon you encounter in your way in the **Kanto Region** *(GEN 1).*
- Just upload an image of a GEN 1 pokemon to see which Pokemon it is.'''
)
def main():
file_uploaded = st.file_uploader(
"Choose File", type=["png", "jpg", "jpeg"])
class_btn = st.button("CLASSIFY!!")
if file_uploaded is not None:
image = Image.open(file_uploaded)
st.image(image, caption='Uploaded Image', use_column_width=True)
if class_btn:
if file_uploaded is None:
st.write("Invalid command, please upload an image")
else:
with st.spinner('Wait For a sec.....'):
predictions = predict(image)
time.sleep(1)
st.success('Gotcha your pokemon was found')
print_data1(predictions)
def path_to_image_html(path):
return '<img src="'+ path + '" width="60" >'
def predict(image):
IMAGE_SHAPE = (128, 128, 3)
model = load_model("model_pokemon.h5")
test_image = image.convert("RGB").resize((128, 128))
test_image = np.array(test_image)
test_image = test_image / 255.0
img_array = tf.expand_dims(test_image, 0)
predictions = model.predict(img_array)
scores = tf.nn.softmax(predictions[0])
scores = scores.numpy()
highest = scores.argsort()[-5:][::-1]
result = [class_names[highest[0]]]
# for i in range(1):
# result.append(class_names[highest[i]])
# i += 1
return result
def print_data1(pokelist):
url = 'https://pokeapi.co/api/v2/pokemon/'
df = pd.DataFrame(data=np.zeros((1, 4)),
columns=['Name', 'Type', 'Description', 'Image'],
index=np.linspace(1, 1, 1, dtype=int)
)
sprites_path = 'https://github.com/iushdoescode/WhosThatPokemon-Deployed/blob/master/Sprites/'
sprites = []
i = 0
for poke in pokelist:
response = requests.get(url+poke.lower())
if(response.status_code != 200):
df.iloc[i, 0] = poke
df.iloc[i, 1] = 'Error fetching data from API'
df.iloc[i, 2] = 'Error fetching data from API'
sprites.append(sprites_path+'0.png?raw=true')
else:
jresponse = response.json()
type = jresponse['types'][0]['type']['name']
id = jresponse['id']
species_url = jresponse['species']['url']
species_response = requests.get(species_url)
species_response = species_response.json()
description = ''
for d in species_response['flavor_text_entries']:
if d['language']['name'] == 'en':
description = d['flavor_text']
break
df.iloc[i, 0] = poke.capitalize()
df.iloc[i, 1] = type.capitalize()
description = description.replace('\n', ' ')
description = description.replace('', ' ')
df.iloc[i, 2] = description
sprites.append(sprites_path+str(id)+'.png?raw=true')
i += 1
df['Image'] = sprites
st.title("Your Pokemon is Most Likely")
st.write(df.to_html(escape=False, formatters=dict(Image=path_to_image_html)), unsafe_allow_html=True)
# def print_data2(pokelist):
# url = 'https://pokeapi.co/api/v2/pokemon/'
# df2=pd.DataFrame(data=np.zeros((4, 4)),
# columns=['Name', 'Type', 'Description', 'Image'],
# index=np.linspace(1, 4, 4, dtype=int)
# )
# sprites_path = 'https://github.com/iushdoescode/WhosThatPokemon-Deployed/blob/master/Sprites/'
# sprites = []
# j=0
# for poke in pokelist:
# response = requests.get(url+poke.lower())
# if(response.status_code != 200):
# df2.iloc[j, 0] = poke
# df2.iloc[j, 1] = 'Error fetching data from API'
# df2.iloc[j, 2] = 'Error fetching data from API'
# sprites.append(sprites_path+'0.png?raw=true')
# else:
# jresponse = response.json()
# type = jresponse['types'][0]['type']['name']
# id = jresponse['id']
# species_url = jresponse['species']['url']
# species_response = requests.get(species_url)
# species_response = species_response.json()
# description = ''
# for d in species_response['flavor_text_entries']:
# if d['language']['name'] == 'en':
# description = d['flavor_text']
# break
# df2.iloc[j, 0] = poke.capitalize()
# df2.iloc[j, 1] = type.capitalize()
# description = description.replace('\n', ' ')
# description = description.replace('', ' ')
# df2.iloc[j, 2] = description
# sprites.append(sprites_path+str(id)+'.png?raw=true')
# j += 1
# df2['Image'] = sprites
# st.title("But Your Pokemon can Also Be...")
# st.write(df2.to_html(escape=False, formatters=dict(Image=path_to_image_html)), unsafe_allow_html=True)
if __name__ == "__main__":
main()