chiraant commited on
Commit
e426d59
·
verified ·
1 Parent(s): 0c7f4b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -26
app.py CHANGED
@@ -1,35 +1,51 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
  from PIL import Image
4
- import numpy as np
5
 
 
 
6
 
7
- # Modell laden
8
- model = tf.keras.models.load_model('pokemon_classifier.keras')
9
 
10
- def classify_image(image):
11
- # Bild vorverarbeiten
12
- image = Image.fromarray(image.astype('uint8')).convert('RGB')
13
- image = image.resize((150, 150)) # Anpassung der Größe an das Modell
14
- image = np.array(image) / 255.0 # Normalisieren
15
- image = np.expand_dims(image, axis=0) # Hinzufügen der Batch-Dimension
16
 
17
- # Vorhersage machen
18
- prediction = model.predict(image).flatten()
19
- classes = ['Abra', 'Ditto', 'Gengar'] # Namen der Klassen
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Wahrscheinlichkeiten mit Klassen verbinden und formatieren
22
- return {classes[i]: float(prediction[i]) for i in range(len(classes))}
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Gradio-Interface erstellen
25
  input_image = gr.Image()
26
- iface = gr.Interface(
27
- fn=classify_image,
28
- inputs=input_image,
29
- outputs=gr.Label(num_top_classes=3),
30
- examples=["pokemon/Abra/00000000.png", "pokemon/Ditto/00000000.jpg", "pokemon/Gengar/00000000.png"], # Beispiele hinzufügen
31
- description="Upload an image of a Pokémon to classify it as Pikachu, Charmander, or Bulbasaur."
32
- )
33
-
34
- # Interface starten
35
- iface.launch()
 
1
  import gradio as gr
 
2
  from PIL import Image
 
3
 
4
+ # Load your custom regression model
5
+ model_path = "transferlearning_pokemon.keras"
6
 
7
+ #model.load_weights(model_path)
8
+ model = tf.keras.models.load_model(model_path)
9
 
10
+ labels = ['Abra', 'Ditto', 'Gengar']
 
 
 
 
 
11
 
12
+ def predict_pokemons(image):
13
+ # Preprocess the image
14
+ print(type(image))
15
+ image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
16
+ image = image.resize((150, 150)) # Resize the image as per the model's input requirement
17
+ image = np.array(image)
18
+ image = np.expand_dims(image, axis=0) # Add batch dimension
19
+
20
+ # Predict
21
+ predictions = model.predict(image)
22
+
23
+ # Convert the logits to probabilities using softmax
24
+ probabilities = tf.nn.softmax(predictions[0]).numpy()
25
+
26
+ # Create a dictionary to hold the probabilities for each class
27
+ results = {class_names[i]: float(np.round(probabilities[i], 2)) for i in range(len(class_names))}
28
+ return results
29
 
30
+ # Define regression function
31
+ def predict_regression(image):
32
+ # Preprocess image
33
+ image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
34
+ image = image.resize((150, 150))
35
+ image = np.array(image)
36
+ print(image.shape)
37
+ # Predict
38
+ prediction = model.predict(image[None, ...]) # Assuming single regression value
39
+ print(prediction)
40
+ confidences = str(prediction)
41
+ return confidences
42
 
43
+ # Create Gradio interface
44
  input_image = gr.Image()
45
+ output_text = gr.Textbox(label="Predicted Value")
46
+ interface = gr.Interface(fn=predict_pokemons,
47
+ inputs=input_image,
48
+ outputs=gr.Label(),
49
+ examples=["images/images_0.png", "images/images_1.png", "images/images_2.png", "images/images_3.png"],
50
+ description="A simple pokemon classification model based on Xception and Pokemon Images (https://www.kaggle.com/datasets/mikoajkolman/pokemon-images-first-generation17000-files).")
51
+ interface.launch()