sowbaranika13 commited on
Commit
e91bcc6
1 Parent(s): 3be889a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -48
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import tensorflow as tf
3
  from tensorflow.keras.models import load_model
4
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
5
- from tensorflow.keras.preprocessing.image import img_to_array,load_img
6
  import numpy as np
7
  from PIL import Image
8
  import os
@@ -16,64 +16,31 @@ labels = {
16
  'amphibia': ['American Bullfrog', 'American Toad', 'Green Frog', 'Northern Leopard Frog']
17
  }
18
 
 
19
  def preprocess_image(image):
20
  img = image.resize((224, 224)) # MobileNet requires 224x224 input size
21
  img_array = img_to_array(img)
22
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
23
  img_array = preprocess_input(img_array) # Preprocess the image
24
-
25
- return img_array
26
 
27
  # Function to perform inference
28
- def predict(img_path):
29
- img = preprocess_image(img_path)
30
  model = load_model(r"inceptionv3_classs.h5")
31
- preds = model.predict(img) # Get the model predictions
32
- decoded_preds = np.argmax(preds)
33
- print(decoded_preds, labels['class'][decoded_preds])
34
- return labels['class'][decoded_preds]
35
-
36
-
37
 
38
-
39
- # results = {}
40
- # image_array = load_and_preprocess_image(image)
41
- # print(image_array)
42
- # # Predict class level
43
- # class_preds = hierarchical_models['class'].predict(image_array)
44
- # print(class_preds)
45
- # class_idx = np.argmax(class_preds)
46
- # print(class_idx)
47
- # class_label = labels['class'][class_idx]
48
- # class_confidence = class_preds[0][class_idx]
49
- # class_level = f"{class_label} ({class_confidence*100:.2f}%)"
50
-
51
- # # Predict species level
52
- # hierarchical_models[class_label] = load_model(f"inceptionv3_{class_label}.h5")
53
- # species_preds = hierarchical_models[class_label].predict(image_array)
54
- # species_idx = np.argmax(species_preds)
55
- # species_label = labels[class_label][species_idx]
56
- # species_confidence = species_preds[0][species_idx]
57
- # species_level = f"{species_label} ({species_confidence*100:.2f}%)"
58
- # return class_level,species_level
59
-
60
- # Sample images (you can add paths to images here)
61
- # sample_images = [
62
- # ("Sample Amphibia", "path/to/amphibia.jpg"),
63
- # ("Sample Aves", "path/to/aves.jpg"),
64
- # ("Sample Mammalia", "path/to/mammalia.jpg"),
65
- # # Add more sample images as needed
66
- # ]
67
 
68
- # Create Gradio interface
69
  iface = gr.Interface(
70
- fn=predict,
71
- inputs=gr.Image(type="pil"),
72
- outputs=[gr.Label(label="class_label")],
73
- # examples=sample_images,
74
- title="Image Classification",
75
- description="Upload an image to classify it into species and class level.",
76
  )
77
 
78
- # Launch the interface
79
  iface.launch()
 
2
  import tensorflow as tf
3
  from tensorflow.keras.models import load_model
4
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
5
+ from tensorflow.keras.preprocessing.image import img_to_array
6
  import numpy as np
7
  from PIL import Image
8
  import os
 
16
  'amphibia': ['American Bullfrog', 'American Toad', 'Green Frog', 'Northern Leopard Frog']
17
  }
18
 
19
+ # Preprocess the image
20
  def preprocess_image(image):
21
  img = image.resize((224, 224)) # MobileNet requires 224x224 input size
22
  img_array = img_to_array(img)
23
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
24
  img_array = preprocess_input(img_array) # Preprocess the image
25
+ return img_array, image # Return both the preprocessed array and resized image for display
 
26
 
27
  # Function to perform inference
28
+ def predict(img):
29
+ img_array, resized_img = preprocess_image(img)
30
  model = load_model(r"inceptionv3_classs.h5")
31
+ preds = model.predict(img_array) # Get the model predictions
32
+ decoded_preds = np.argmax(preds)
 
 
 
 
33
 
34
+ # Return the prediction and the resized image for display
35
+ return resized_img, labels['class'][decoded_preds]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ # Create the Gradio interface
38
  iface = gr.Interface(
39
+ fn=predict, # The prediction function
40
+ inputs=gr.inputs.Image(type="pil"), # Input as a PIL image
41
+ outputs=[gr.outputs.Image(type="pil"), gr.outputs.Textbox()], # Output the resized image and prediction
42
+ title="Animal Classifier"
 
 
43
  )
44
 
45
+ # Launch the Gradio interface
46
  iface.launch()