bauerfel commited on
Commit
2c8ee55
·
verified ·
1 Parent(s): e643366

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -31
app.py CHANGED
@@ -2,34 +2,34 @@ import gradio as gr
2
  import tensorflow as tf
3
  from PIL import Image
4
  import numpy as np
5
-
6
- labels = ['barrel_jellyfish','blue_jellyfish','compass_jellyfish','lions_mane_jellyfish','mauve_stinger_jellyfish','Moon_jellyfish']
7
-
8
- def predict_jellyfish_type(uploaded_file):
9
-
10
- if uploaded_file is None:
11
- return "No file uploaded."
12
-
13
- model = tf.keras.models.load_model('Jellyfish_InceptionResNetV2_transferlearning.keras')
14
- # Load the image from the file path
15
- with Image.open(uploaded_file) as img:
16
- img = img.resize((150, 150)).convert('RGB') # Convert image to RGB
17
- img_array = np.array(img)
18
-
19
- prediction = model.predict(np.expand_dims(img_array, axis=0))
20
- confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
21
-
22
- return confidences
23
-
24
-
25
- # Define the Gradio interface
26
- iface = gr.Interface(
27
- fn=predict_jellyfish_type, # Function to process the input
28
- inputs=gr.File(label="Upload File"), # File upload widget
29
- outputs="text", # Output type
30
- title="Jellyfish Classifier", # Title of the interface
31
- description="Upload a picture of a Jellyfish (preferably barrel, blue, compass, lions mane, mauve stinger, Moon)" # Description of the interface
32
- )
33
-
34
- # Launch the interface
35
- iface.launch()
 
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 = "Jellyfish_InceptionResNetV2_transferlearning.weights.h5"
8
+ model_path = "Jellyfish_InceptionResNetV2_transferlearning.keras"
9
+
10
+ #model.load_weights(model_path)
11
+ model = tf.keras.models.load_model(model_path)
12
+
13
+ labels = ['barrel_jellyfish', 'blue_jellyfish', 'compass_jellyfish', 'lions_mane_jellyfish', 'mauve_stinger_jellyfish', 'Moon_jellyfish']
14
+
15
+ # Define regression function
16
+ def predict_regression(image):
17
+ # Preprocess image
18
+ image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
19
+ image = image.resize((28, 28)).convert('L') #resize the image to 28x28 and converts it to gray scale
20
+ image = np.array(image)
21
+ print(image.shape)
22
+ # Predict
23
+ prediction = model.predict(image[None, ...]) # Assuming single regression value
24
+ confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
25
+ return confidences
26
+
27
+ # Create Gradio interface
28
+ input_image = gr.Image()
29
+ output_text = gr.Textbox(label="Predicted Value")
30
+ interface = gr.Interface(fn=predict_regression,
31
+ inputs=input_image,
32
+ outputs=gr.Label(),
33
+ examples=["images/barrel_jellyfish.jpg", "images/blue_jellyfish.jpg", "images/compass_jellyfish.jpg", "images/lions_mane_jellyfish.jpg"],
34
+ description="A simple mlp classification model for image classification using the mnist dataset.")
35
+ interface.launch()