sudip2003 commited on
Commit
80b963b
·
verified ·
1 Parent(s): 5cd1b71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -3,31 +3,42 @@ import tensorflow as tf
3
  from tensorflow.keras.preprocessing import image
4
  import numpy as np
5
 
6
- # Load the trained model
7
- model = tf.keras.models.load_model('Model_catsVSdogs.h5')
8
-
9
- # Define a function to make predictions
10
- def predict_image(img):
11
- # Preprocess the image
12
- img = img.resize((256,256)) # Resize the image to 224x224 pixels
13
- img_array = image.img_to_array(img) # Convert the image to an array
14
- img_array = np.expand_dims(img_array, axis=0) # Add a batch dimension
15
- img_array = img_array / 255.0 # Normalize the image
16
-
17
- # Make a prediction
 
 
 
 
 
 
 
 
 
 
 
 
18
  prediction = model.predict(img_array)
19
  if prediction[0] < 0.5:
20
  return "Cat"
21
  else:
22
  return "Dog"
23
 
24
- # Create the Gradio interface
25
 
26
  interface = gr.Interface(fn=predict_image,
27
- inputs=gr.Image(type="pil"),
28
  outputs="text",
29
  title="Cat and Dog Classifier",
30
- description="Upload an image of a cat or dog and the model will predict which one it is.")
 
31
 
32
- # Launch the interface
33
- interface.launch(share=True)
 
3
  from tensorflow.keras.preprocessing import image
4
  import numpy as np
5
 
6
+
7
+ def load_models():
8
+ models = {}
9
+ models['SimpleNN_model'] = tf.keras.models.load_model("SimpleNN_model.h5")
10
+ models['VGG16'] = tf.keras.models.load_model("vgg16.h5")
11
+ return models
12
+
13
+
14
+ models = load_models()
15
+
16
+
17
+ def predict_image(img, model_name):
18
+ model = models[model_name]
19
+
20
+ if model_name == 'SimpleNN_model':
21
+ img = img.resize((256, 256))
22
+ elif model_name == 'VGG16':
23
+ img = img.resize((224, 224))
24
+
25
+ img_array = image.img_to_array(img)
26
+ img_array = np.expand_dims(img_array, axis=0)
27
+ img_array = img_array / 255.0
28
+
29
+
30
  prediction = model.predict(img_array)
31
  if prediction[0] < 0.5:
32
  return "Cat"
33
  else:
34
  return "Dog"
35
 
 
36
 
37
  interface = gr.Interface(fn=predict_image,
38
+ inputs=[gr.Image(type="pil"), gr.Dropdown(["SimpleNN_model", "VGG16"], label="Select Model")],
39
  outputs="text",
40
  title="Cat and Dog Classifier",
41
+ description="Upload an Image")
42
+
43
 
44
+ interface.launch()