thomen commited on
Commit
ce36f5c
·
verified ·
1 Parent(s): 93d46aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -32
app.py CHANGED
@@ -1,38 +1,39 @@
1
-
2
  import gradio as gr
3
  import tensorflow as tf
4
- from PIL import Image
5
  import numpy as np
 
6
 
7
- labels = ['Haunter', 'Gengar', 'Ditto', 'Vulpix']
8
-
9
- def predict_pokemon_type(uploaded_file):
10
- if uploaded_file is None:
11
- return "No file uploaded.", None, "No prediction"
12
-
13
- model = tf.keras.models.load_model('pokemon-model_2_transferlearning.keras')
14
-
15
- # Load the image from the file path
16
- with Image.open(uploaded_file) as img:
17
- img = img.resize((150, 150))
18
- img_array = np.array(img)
19
-
20
- prediction = model.predict(np.expand_dims(img_array, axis=0))
21
- confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
22
-
23
- # Identify the most confident prediction
24
- confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
25
-
26
- return img, confidences
27
 
28
- # Define the Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  iface = gr.Interface(
30
- fn=predict_pokemon_type, # Function to process the input
31
- inputs=gr.File(label="Upload File"), # File upload widget
32
- outputs=["image", "text"], # Output types for image and text
33
- title="Pokemon Classifier", # Title of the interface
34
- description="Upload a picture of a Pokemon (preferably Ditto, Gengar, Haunting, or Vulpix) to see its type and confidence level."
35
- )
36
-
37
-
38
- iface.launch()
 
 
1
  import gradio as gr
2
  import tensorflow as tf
 
3
  import numpy as np
4
+ from PIL import Image
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ model_path = "pokemon-model_2_transferlearning.keras"
8
+ model = tf.keras.models.load_model(model_path)
9
+
10
+ def predict_pokemon(image):
11
+ # Preprocess image
12
+ print(type(image))
13
+ image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
14
+ image = image.resize((150, 150)) #resize the image to 150x150
15
+ image = np.array(image)
16
+ image = np.expand_dims(image, axis=0) # same as image[None, ...]
17
+
18
+
19
+ prediction = model.predict(image)
20
+
21
+ # Apply softmax to get probabilities for each class
22
+ prediction = tf.nn.softmax(prediction)
23
+
24
+ # Create a dictionary with the probabilities for each Pokemon
25
+ evee = np.round(float(prediction[0][0]), 2)
26
+ farfetched = np.round(float(prediction[0][1]), 2)
27
+ graveler = np.round(float(prediction[0][2]), 2)
28
+ venonta = np.round(float(prediction[0][3]), 2)
29
+
30
+ return {'Evee': evee, 'Farfetched': farfetched, 'Graveler': graveler, 'Venonta': venonta}
31
+
32
+
33
+ input_image = gr.Image()
34
  iface = gr.Interface(
35
+ fn=predict_pokemon,
36
+ inputs=input_image,
37
+ outputs=gr.Label(),
38
+ description="A simple mlp classification model for image classification using the mnist dataset.")
39
+ iface.launch(share=True)