sowbaranika13 commited on
Commit
9e4f4db
1 Parent(s): 1c07ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -29
app.py CHANGED
@@ -6,9 +6,7 @@ from tensorflow.keras.preprocessing.image import img_to_array
6
  import numpy as np
7
  from PIL import Image
8
  import os
9
- import requests
10
 
11
- # Load your model and tokenizer
12
  labels = {
13
  'class': ['amphibia', 'aves', 'invertebrates', 'lacertilia', 'mammalia', 'serpentes', 'testudines'],
14
  'serpentes': ["Butler's Gartersnake", "Dekay's Brownsnake", 'Eastern Gartersnake', 'Eastern Hog-nosed snake', 'Eastern Massasauga', 'Eastern Milksnake', 'Eastern Racer Snake', 'Eastern Ribbonsnake', 'Gray Ratsnake', "Kirtland's Snake", 'Northern Watersnake', 'Plains Gartersnake', 'Red-bellied Snake', 'Smooth Greensnake'],
@@ -17,44 +15,26 @@ labels = {
17
  'amphibia': ['American Bullfrog', 'American Toad', 'Green Frog', 'Northern Leopard Frog']
18
  }
19
 
20
- model_url = "https://github.com/sowbaranika1302/ohio_classifier/raw/main/Models/inceptionv3_best_withoutblanks.h5"
21
- model_path = "inceptionv3_best_withoutblanks.h5"
22
 
23
- # Download the model if it doesn't exist locally
24
- if not os.path.exists(model_path):
25
- response = requests.get(model_url)
26
- with open(model_path, 'wb') as f:
27
- f.write(response.content)
28
-
29
- # Load the model
30
- model = load_model(model_path)
31
-
32
- # Preprocess the image
33
  def preprocess_image(image):
34
- img = image.resize((224, 224)) # MobileNet requires 224x224 input size
35
  img_array = img_to_array(img)
36
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
37
- img_array = preprocess_input(img_array) # Preprocess the image
38
- return img_array, img # Return both the preprocessed array and resized image for display
39
 
40
- # Function to perform inference
41
  def predict(img):
42
  img_array, resized_img = preprocess_image(img)
43
- #model = load_model(r"inceptionv3_classs.h5")
44
- preds = model.predict(img_array) # Get the model predictions
45
  decoded_preds = np.argmax(preds)
46
-
47
- # Return the prediction and the resized image for display
48
  return resized_img, labels['class'][decoded_preds]
49
 
50
- # Create the Gradio interface
51
  iface = gr.Interface(
52
- fn=predict, # The prediction function
53
-
54
- inputs=gr.Image(type="pil"), # Input as a PIL image
55
- outputs=[gr.Image(type="pil"), gr.Label(label="species_label")], # Output the resized image and prediction
56
  title="Animal Classifier"
57
  )
58
 
59
- # Launch the Gradio interface
60
  iface.launch()
 
6
  import numpy as np
7
  from PIL import Image
8
  import os
 
9
 
 
10
  labels = {
11
  'class': ['amphibia', 'aves', 'invertebrates', 'lacertilia', 'mammalia', 'serpentes', 'testudines'],
12
  'serpentes': ["Butler's Gartersnake", "Dekay's Brownsnake", 'Eastern Gartersnake', 'Eastern Hog-nosed snake', 'Eastern Massasauga', 'Eastern Milksnake', 'Eastern Racer Snake', 'Eastern Ribbonsnake', 'Gray Ratsnake', "Kirtland's Snake", 'Northern Watersnake', 'Plains Gartersnake', 'Red-bellied Snake', 'Smooth Greensnake'],
 
15
  'amphibia': ['American Bullfrog', 'American Toad', 'Green Frog', 'Northern Leopard Frog']
16
  }
17
 
 
 
18
 
 
 
 
 
 
 
 
 
 
 
19
  def preprocess_image(image):
20
+ img = image.resize((224, 224))
21
  img_array = img_to_array(img)
22
+ img_array = np.expand_dims(img_array, axis=0)
23
+ img_array = preprocess_input(img_array)
24
+ return img_array, img
25
 
 
26
  def predict(img):
27
  img_array, resized_img = preprocess_image(img)
28
+ model = load_model("inceptionv3_class.h5")
29
+ preds = model.predict(img_array)
30
  decoded_preds = np.argmax(preds)
 
 
31
  return resized_img, labels['class'][decoded_preds]
32
 
 
33
  iface = gr.Interface(
34
+ fn=predict,
35
+ inputs=gr.Image(type="pil"),
36
+ outputs=[gr.Image(type="pil"), gr.Label(label="species_label")],
 
37
  title="Animal Classifier"
38
  )
39
 
 
40
  iface.launch()