-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
528 lines (445 loc) · 19.8 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
from flask import Flask, request, render_template, url_for
from keras.layers import LeakyReLU
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from PIL import Image
import numpy as np
import os
import base64
from io import BytesIO
import requests
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
app = Flask(__name__)
from flask import Flask
app = Flask(__name__)
# Load the saved model
model = load_model("pokemon.keras", compile=False, custom_objects={'LeakyReLU': LeakyReLU})
# Compile the model manually
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Define image dimensions
img_width, img_height = 150, 150
import requests
import logging
# Global cache to store different types of Pokémon data
global_cache = {
'pokemon_data': {},
'species_data': {},
'base_stats': {},
'api_data': {} # For caching individual API responses if needed
}
special_cases = {
"deoxys": "deoxys-normal",
"wormadam": "wormadam-plant",
"giratina": "giratina-altered",
"shaymin": "shaymin-land",
"basculin": "basculin-red-striped",
"basculegion": "basculegion-male",
"darmanitan": "darmanitan-standard",
"tornadus": "tornadus-incarnate",
"thundurus": "thundurus-incarnate",
"landorus": "landorus-incarnate",
"enamorus": "enamorus-incarnate",
"keldeo": "keldeo-ordinary",
"meloetta": "meloetta-aria",
"meowstic": "meowstic-male",
"aegislash": "aegislash-shield",
"pumpkaboo": "pumpkaboo-average",
"gourgeist": "gourgeist-average",
"zygarde": "zygarde-50",
"oricorio": "oricorio-baile",
"lycanroc": "lycanroc-midday",
"wishiwashi": "wishiwashi-solo",
"minior": "minior-red-meteor",
"mimikyu": "mimikyu-disguised",
"toxtricity": "toxtricity-amped",
"eiscue": "eiscue-ice",
"indeedee": "indeedee-male",
"urshifu": "urshifu-single-strike",
"oinkologne": "oinkologne-male",
"maushold": "maushold-family-of-four",
"squawkabilly": "squawkabilly-green-plumage",
"palafin": "palafin-zero",
"tatsugiri": "tatsugiri-curly",
"dudunsparce": "dudunsparce-two-segment",
"farfetch'd": "farfetchd",
"sirfetch'd": "sirfetchd",
"mime jr": "mime-jr",
"mr. mime": "mr-mime",
"mr. rime": "mr-rime",
"nidoran♀": "nidoran-f",
"nidoran♂": "nidoran-m",
"flabébé": "flabebe"
}
special_pokemon = [
"type-null",
"jangmo-o",
"hakamo-o",
"kommo-o",
"tapu lele",
"tapu bulu",
"tapu fini",
"tapu koko",
"chi-yu",
"chien-pao",
"ting-lu",
"wo-chien",
"great tusk",
"scream tail",
"brute bonnet",
"flutter mane",
"slither wing",
"sandy shocks",
"roaring moon",
"walking wake",
"gouging fire",
"raging bolt",
"iron treads",
"iron bundle",
"iron hands",
"iron jugulis",
"iron moth",
"iron thorns",
"iron valiant",
"iron leaves",
"iron boulder",
"iron crown"
]
def format_pokemon_name(name):
formatted_name = special_cases.get(name.lower(), name.replace(' ', '-').lower())
return formatted_name
# Function to fetch data with caching
def fetch_data_with_cache(url):
if url in global_cache['api_data']:
return global_cache['api_data'][url] # Return from cache if available
response = requests.get(url)
if response.status_code == 200:
data = response.json()
global_cache['api_data'][url] = data # Cache the result
return data
return None
def get_pokemon_data(pokemon_name):
base_name = pokemon_name.lower()
pokemon_data = {}
if base_name in global_cache['pokemon_data']:
return global_cache['pokemon_data'][base_name]
# Fetch base form data
base_url = f'https://pokeapi.co/api/v2/pokemon/{base_name}'
base_data = fetch_data_with_cache(base_url)
if base_data:
pokemon_data['dex_number'] = base_data['id']
pokemon_data['types'] = [t['type']['name'] for t in base_data['types']] # Base types
pokemon_data['base_stats'] = {stat['stat']['name']: stat['base_stat'] for stat in base_data['stats']}
pokemon_data['sprite_url'] = base_data['sprites']['other']['official-artwork']['front_default']
pokemon_data['forms'] = []
# Add base form as the first form
pokemon_data['forms'].append({
'form_name': base_name,
'sprite_url': base_data['sprites']['other']['official-artwork']['front_default'],
'types': pokemon_data['types'] # Storing types for the base form
})
# Fetch species data to get form information
species_url = f'https://pokeapi.co/api/v2/pokemon-species/{base_name}'
species_data = fetch_data_with_cache(species_url)
if species_data:
forms = species_data['varieties']
for form in forms:
form_name = form['pokemon']['name']
if form_name != base_name:
form_url = form['pokemon']['url']
form_data = fetch_data_with_cache(form_url)
if form_data:
form_sprite_url = form_data['sprites']['other']['official-artwork']['front_default']
form_types = [t['type']['name'] for t in form_data['types']] # Form-specific types
# Append form if not already added
pokemon_data['forms'].append({
'form_name': form_name,
'sprite_url': form_sprite_url,
'types': form_types # Store types for the specific form
})
# Store the result in the global cache
global_cache['pokemon_data'][base_name] = pokemon_data
return pokemon_data
def get_pokemon_species_data(pokemon_name):
# Convert the name to lowercase and replace spaces with hyphens
formatted_name = pokemon_name.lower().replace(' ', '-')
if '-' in formatted_name and formatted_name.split('-')[0] in special_cases:
base_name = formatted_name.split('-')[0]
elif formatted_name in special_pokemon:
base_name = formatted_name
else:
base_name = formatted_name
# Debug: Log the base name
logging.debug(f"Base name used for species data: {base_name}")
# Check cache for species data using the base name
if base_name in global_cache['species_data']:
return global_cache['species_data'][base_name]
url = f'https://pokeapi.co/api/v2/pokemon-species/{base_name}'
data = fetch_data_with_cache(url)
if data:
# Extract necessary fields from the fetched data
evolution_chain_url = data['evolution_chain']['url']
forms = [variety['pokemon']['name'] for variety in data['varieties']]
gender_rate = data['gender_rate']
# Structure species data
species_data = {
'forms': forms,
'evolution_chain_url': evolution_chain_url,
'gender_rate': gender_rate
}
# Cache the fetched species data for future use
global_cache['species_data'][base_name] = species_data
return species_data
else:
# Log error if data fetching fails
logging.error(f"Error fetching species data for {base_name}")
return None
def get_pokemon_base_stats(pokemon_name):
base_name = pokemon_name.lower()
base_stats_by_form = []
# Initialize cache for the Pokémon if not present
if base_name not in global_cache:
global_cache[base_name] = {}
# Check if base stats are already cached
if 'base_stats' in global_cache[base_name]:
return global_cache[base_name]['base_stats']
fetched_forms = set() # Track forms that have been fetched to avoid duplication
# Fetch default Pokémon data
url = f'https://pokeapi.co/api/v2/pokemon/{base_name}'
response = requests.get(url)
if response.status_code == 200:
base_data = response.json()
default_base_stats = {stat['stat']['name']: stat['base_stat'] for stat in base_data['stats']}
default_types = [t['type']['name'] for t in base_data['types']]
default_sprite_url = base_data['sprites']['other']['official-artwork']['front_default']
# Append only if the form has not been fetched already
if base_name not in fetched_forms:
base_stats_by_form.append({
'form_name': base_name,
'base_stats': default_base_stats,
'types': default_types, # Fetch the types for the base form
'sprite_url': default_sprite_url
})
fetched_forms.add(base_name)
# Fetch species data for additional forms
species_url = f'https://pokeapi.co/api/v2/pokemon-species/{base_name}'
species_response = requests.get(species_url)
if species_response.status_code == 200:
species_data = species_response.json()
for variety in species_data['varieties']:
form_name = variety['pokemon']['name']
if form_name not in fetched_forms: # Fetch form data only if not already fetched
form_response = requests.get(variety['pokemon']['url'])
if form_response.status_code == 200:
form_data = form_response.json()
form_base_stats = {stat['stat']['name']: stat['base_stat'] for stat in form_data['stats']}
form_types = [t['type']['name'] for t in form_data['types']] # Fetch form-specific types
form_sprite_url = form_data['sprites']['other']['official-artwork']['front_default']
# Append the form to the list
base_stats_by_form.append({
'form_name': form_name,
'base_stats': form_base_stats,
'types': form_types, # Include form-specific types
'sprite_url': form_sprite_url
})
fetched_forms.add(form_name)
# Store the fetched base stats in cache
global_cache[base_name]['base_stats'] = base_stats_by_form
return base_stats_by_form
def get_evolution_chain(evolution_chain_url):
evolution_data = fetch_data_with_cache(evolution_chain_url)
if evolution_data:
evolution_chain = evolution_data['chain']
evolutions = []
def parse_evolution(evolution_chain, parent=None):
pokemon_name = evolution_chain['species']['name']
species_url = f'https://pokeapi.co/api/v2/pokemon-species/{pokemon_name}'
species_data = fetch_data_with_cache(species_url)
if species_data:
base_form_name = species_data['varieties'][0]['pokemon']['name']
pokemon_url = f'https://pokeapi.co/api/v2/pokemon/{base_form_name}'
pokemon_data = fetch_data_with_cache(pokemon_url)
if pokemon_data:
sprite_url = pokemon_data['sprites']['other']['official-artwork']['front_default']
evolution_conditions = []
if parent is None:
evolution_conditions.append("Unevolved")
if 'evolution_details' in evolution_chain:
for detail in evolution_chain['evolution_details']:
if detail.get('gender') is not None:
gender = 'Male' if detail['gender'] == 2 else 'Female'
evolution_conditions.append(f'Evolves if {gender}')
if detail.get('trigger'):
trigger = detail['trigger']['name']
evolution_conditions.append(f'Evolution trigger: {trigger}')
if detail.get('item'):
item = detail['item']['name']
evolution_conditions.append(f'Item required: {item}')
if detail.get('held_item'):
held_item = detail['held_item']['name']
evolution_conditions.append(f'Held item: {held_item}')
if detail.get('min_level'):
evolution_conditions.append(f'Min level: {detail["min_level"]}')
if detail.get('time_of_day'):
evolution_conditions.append(f'Time of day: {detail["time_of_day"]}')
if detail.get('known_move'):
move = detail['known_move']['name']
evolution_conditions.append(f'Must know move: {move}')
if detail.get('known_move_type'):
move_type = detail['known_move_type']['name']
evolution_conditions.append(f'Move type required: {move_type}')
evolutions.append({
'name': pokemon_name,
'sprite_url': sprite_url,
'conditions': evolution_conditions,
'parent': parent
})
for evo in evolution_chain['evolves_to']:
parse_evolution(evo, parent=pokemon_name)
parse_evolution(evolution_chain)
return evolutions
return []
# Load the train generator to get the class indices
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
brightness_range=[0.8, 1.2],
validation_split=0.2
)
train_generator = train_datagen.flow_from_directory(
"Training",
target_size=(img_width, img_height),
batch_size=32,
class_mode='categorical',
subset='training'
)
# Create 'static' directory if not exists
if not os.path.exists('static'):
os.makedirs('static')
# Function to predict Pokémon from an image file
def predict_pokemon(img_array):
img_array = img_array / 255.0
prediction = model.predict(img_array)
predicted_class = np.argmax(prediction)
return predicted_class, np.max(prediction) * 100
# Function to get the label (Pokémon name) of the predicted class
def get_prediction_details(predicted_class):
predicted_pokemon = train_generator.class_indices
predicted_pokemon = dict((v, k) for k, v in predicted_pokemon.items())
predicted_pokemon = predicted_pokemon[predicted_class]
return predicted_pokemon
# Function to get a representative image file from the predicted Pokémon folder
def get_representative_image(pokemon_folder):
folder_path = os.path.join('Training', pokemon_folder)
try:
files = os.listdir(folder_path)
# Filter out non-image files and get the first image
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
if image_files:
return os.path.join(folder_path, image_files[0])
except FileNotFoundError:
return None
return None
# Function to convert image to base64
def image_to_base64(image):
if image.mode == 'RGBA':
image = image.convert('RGB') # Convert RGBA to RGB if necessary
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
return img_base64
import logging
# Configure logging
logging.basicConfig(level=logging.DEBUG)
@app.route('/predict', methods=['POST'])
def predict():
file = request.files['file']
img = Image.open(file.stream)
img = img.resize((img_width, img_height))
if img.mode == 'RGBA':
img = img.convert('RGB')
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
uploaded_image_path = os.path.join('static', 'uploaded_image.jpg')
img.save(uploaded_image_path)
predicted_class, similarity_percentage = predict_pokemon(img_array)
predicted_pokemon = get_prediction_details(predicted_class)
predicted_pokemon_formatted = format_pokemon_name(predicted_pokemon)
# Log the predicted Pokémon and formatted name
logging.debug(f"Predicted Pokémon: {predicted_pokemon}, Formatted: {predicted_pokemon_formatted}")
# Try to fetch Pokémon species data
try:
species_data = get_pokemon_species_data(predicted_pokemon_formatted)
forms = species_data.get('forms', []) if species_data else []
evolution_chain = get_evolution_chain(species_data.get('evolution_chain_url', '')) if species_data else []
logging.debug(f"Species Data: {species_data}, Forms: {forms}, Evolution Chain: {evolution_chain}")
except Exception as e:
logging.error(f"Error fetching species data: {e}")
species_data, forms, evolution_chain = None, [], []
# Try to fetch Pokémon data from /pokemon/
try:
pokemon_data = get_pokemon_data(predicted_pokemon_formatted)
logging.debug(f"Pokémon Data: {pokemon_data}")
except KeyError as e:
logging.error(f"Error fetching Pokémon data: {e}")
pokemon_data = None
# Handle if pokemon_data is missing (i.e., special cases)
if not pokemon_data:
base_stats_by_form = get_pokemon_base_stats(predicted_pokemon_formatted)
if species_data and base_stats_by_form:
pokemon_data = {
'dex_number': species_data.get('id', 'N/A'),
'types': [],
'base_stats': base_stats_by_form,
'sprite_url': base_stats_by_form[0].get('sprite_url', None) if base_stats_by_form else None
}
logging.debug(f"Handling special case Pokémon data: {pokemon_data}")
else:
logging.warning(f"Failed to handle special case for {predicted_pokemon_formatted}")
else:
base_stats_by_form = get_pokemon_base_stats(predicted_pokemon_formatted)
if base_stats_by_form is None:
base_stats_by_form = []
# Ensure there are no duplicate forms
forms = list(dict.fromkeys(forms)) # Removes duplicates
# Fetch base stats for each unique form
base_stats_by_form = []
fetched_stats = set() # Set to track fetched stats and avoid duplicates
for form in forms:
form_stats = get_pokemon_base_stats(form)
for stats in form_stats:
if stats['form_name'] not in fetched_stats: # Check if form's stats were already added
base_stats_by_form.append(stats)
fetched_stats.add(stats['form_name'])
# Fetch representative image (if available)
representative_image_path = get_representative_image(predicted_pokemon)
if representative_image_path:
predicted_image = Image.open(representative_image_path)
max_width = 150
max_height = int(predicted_image.height * (max_width / predicted_image.width))
predicted_image = predicted_image.resize((max_width, max_height))
predicted_image_base64 = image_to_base64(predicted_image)
else:
predicted_image_base64 = None
return render_template(
'index.html',
predicted_pokemon=predicted_pokemon,
similarity_percentage=similarity_percentage,
image_url=url_for('static', filename='uploaded_image.jpg'),
predicted_image_base64=predicted_image_base64,
dex_number=pokemon_data.get('dex_number', 'N/A') if pokemon_data else 'N/A',
types=pokemon_data.get('types', []) if pokemon_data else [],
base_stats_by_form=base_stats_by_form, # Ensure all forms' stats are passed
forms=forms,
evolution_chain=evolution_chain
)
@app.route("/")
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)