Filipp106 commited on
Commit
e581129
·
verified ·
1 Parent(s): b21dcae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -1,34 +1,38 @@
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
- from PIL import Image
4
  import numpy as np
5
-
6
- # Load your custom regression model
 
7
  model_path = "pokemon_classifier_xception.keras"
8
-
9
- #model.load_weights(model_path)
10
  model = tf.keras.models.load_model(model_path)
11
-
12
- labels = ['Abra', 'Aerodactyl', 'Arcanine']
13
-
14
- # Define regression function
15
- def predict_regression(image):
16
  # Preprocess image
 
17
  image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
18
- image = image.resize((28, 28)).convert('L') #resize the image to 28x28 and converts it to gray scale
19
  image = np.array(image)
20
- print(image.shape)
21
  # Predict
22
- prediction = model.predict(image[None, ...]) # Assuming single regression value
23
- confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
24
- return confidences
25
-
26
- # Create Gradio interface
 
 
 
 
 
27
  input_image = gr.Image()
28
- output_text = gr.Textbox(label="Predicted Value")
29
- interface = gr.Interface(fn=predict_regression,
30
- inputs=input_image,
31
- outputs=gr.Label(),
32
- examples=[],
33
- description="A simple mlp classification model for image classification using the mnist dataset.")
34
- interface.launch()
 
1
+
2
+
3
  import gradio as gr
4
  import tensorflow as tf
 
5
  import numpy as np
6
+ from PIL import Image
7
+
8
+
9
  model_path = "pokemon_classifier_xception.keras"
 
 
10
  model = tf.keras.models.load_model(model_path)
11
+
12
+ # Define the core prediction function
13
+ def predict_pokemon(image):
 
 
14
  # Preprocess image
15
+ print(type(image))
16
  image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
17
+ image = image.resize((150, 150)) #resize the image to 150x150
18
  image = np.array(image)
19
+ image = np.expand_dims(image, axis=0) # same as image[None, ...]
20
  # Predict
21
+ prediction = model.predict(image)
22
+ # Apply softmax to get probabilities for each class
23
+ prediction = tf.nn.softmax(prediction)
24
+ # Create a dictionary with the probabilities for each Pokemon
25
+ abra = np.round(float(prediction[0][0]), 2)
26
+ aerodactyl = np.round(float(prediction[0][1]), 2)
27
+ arcanine = np.round(float(prediction[0][2]), 2)
28
+ return {'abra': abra, 'aerodactyl': aerodactyl, 'arcanine': arcanine}
29
+
30
+
31
  input_image = gr.Image()
32
+ iface = gr.Interface(
33
+ fn=predict_pokemon,
34
+ inputs=input_image,
35
+ outputs=gr.Label(),
36
+ examples=[],
37
+ description="A simple mlp classification model for image classification using the mnist dataset.")
38
+ iface.launch(share=True)