DHEIVER commited on
Commit
73036df
1 Parent(s): e0ee69d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -37
app.py CHANGED
@@ -1,52 +1,40 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
- from PIL import Image
5
- from io import BytesIO
6
 
7
- # Define image dimensions
8
- img_height = 224 # Replace with the actual height of your images
9
- img_width = 224 # Replace with the actual width of your images
10
 
11
- # Load your trained model
12
- model = tf.keras.models.load_model("best_model_weights.h5") # Replace with the path to your saved model
13
-
14
- # Define the image classification function
15
  def classify_image(input_image):
16
- try:
17
- # Preprocess the input image
18
- input_image = Image.open(BytesIO(input_image))
19
- input_image = input_image.resize((img_width, img_height))
20
- input_image = np.array(input_image) / 255.0 # Normalize pixel values
21
-
22
- # Make a prediction using the model
23
- predictions = model.predict(np.expand_dims(input_image, axis=0))
24
-
25
- # Get the class label with the highest probability
26
- class_index = np.argmax(predictions)
27
- class_prob = predictions[0][class_index]
28
-
29
- # Define class labels (you can replace these with your actual class labels)
30
- class_labels = ["Normal", "Cataract"]
31
 
32
- # Get the class label
33
- class_label = class_labels[class_index]
34
 
35
- return f"Predicted Class: {class_label} (Probability: {class_prob:.2f})"
36
- except Exception as e:
37
- return "Error: Invalid image file"
 
38
 
39
- # ...
40
 
 
 
 
41
 
42
- # Define the Gradio interface
43
- iface = gr.Interface(
44
  fn=classify_image,
45
- inputs=gr.inputs.Image(shape=(img_height, img_width)),
46
- outputs="text",
47
  live=True,
48
- title="Image Classifier"
 
49
  )
50
 
51
- # Run the Gradio interface
52
- iface.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
 
 
4
 
5
+ # Load your trained TensorFlow model
6
+ model = tf.keras.models.load_model('best_model_weights.h5') # Load your saved model
 
7
 
8
+ # Define a function to make predictions
 
 
 
9
  def classify_image(input_image):
10
+ # Preprocess the input image (resize and normalize)
11
+ input_image = tf.image.resize(input_image, (224, 224)) # Make sure to match your model's input size
12
+ input_image = (input_image / 255.0) # Normalize to [0, 1]
13
+ input_image = np.expand_dims(input_image, axis=0) # Add batch dimension
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Make a prediction using your model
16
+ prediction = model.predict(input_image)
17
 
18
+ # Assuming your model outputs probabilities for two classes, you can return the class with the highest probability
19
+ class_index = np.argmax(prediction)
20
+ class_labels = ["Class 0", "Class 1"] # Replace with your actual class labels
21
+ predicted_class = class_labels[class_index]
22
 
23
+ return predicted_class
24
 
25
+ # Create a Gradio interface
26
+ input_interface = gr.inputs.Image() # Gradio input component for image
27
+ output_interface = gr.outputs.Text() # Gradio output component for text
28
 
29
+ # Create the Gradio app
30
+ app = gr.Interface(
31
  fn=classify_image,
32
+ inputs=input_interface,
33
+ outputs=output_interface,
34
  live=True,
35
+ title="Image Classifier",
36
+ description="Classify images using a trained model."
37
  )
38
 
39
+ # Start the Gradio app
40
+ app.launch()