hb-setosys commited on
Commit
53c6708
·
verified ·
1 Parent(s): 0a2550a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -1,18 +1,41 @@
1
- import gradio as gr
 
2
  import tensorflow as tf
3
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
4
  from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
5
- import numpy as np
 
6
 
7
- # Load the trained model (replace with correct path to your model)
8
- model = tf.keras.models.load_model("setosys_dogs_model.h5")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Get class labels dynamically from model (from class_indices)
11
- class_labels = {v: k for k, v in model.class_indices.items()}
12
 
13
- # Preprocessing function for EfficientNetV2 model
14
- def preprocess_image(image_path):
15
- img = load_img(image_path, target_size=(224, 224)) # Resize image to 224x224
16
  img_array = img_to_array(img)
17
  img_array = preprocess_input(img_array) # EfficientNetV2 preprocessing
18
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
@@ -23,15 +46,15 @@ def predict_dog_breed(image):
23
  img_array = preprocess_image(image)
24
  predictions = model.predict(img_array)
25
  class_idx = np.argmax(predictions) # Get index of the class with the highest probability
26
- breed = class_labels[class_idx] # Get label using the index
27
- confidence = predictions[0][class_idx]
28
  return breed, confidence
29
 
30
- # Gradio interface definition
31
  iface = gr.Interface(fn=predict_dog_breed,
32
- inputs=gr.inputs.Image(type="filepath"),
33
  outputs=["text", "number"],
34
  live=True)
35
 
36
  # Launch the Gradio app
37
- iface.launch(share=True) # `share=True` gives you a public link
 
1
+ import os
2
+ import numpy as np
3
  import tensorflow as tf
4
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
5
  from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
6
+ from tensorflow.keras.models import load_model
7
+ import gradio as gr
8
 
9
+ # List of class labels from the Stanford Dogs dataset
10
+ class_labels = [
11
+ 'Affenpinscher', 'African_hunting_dog', 'Airedale', 'American_Staffordshire_terrier', 'American_water_spaniel',
12
+ 'Anatolian_shepherd_dog', 'Australian_terrier', 'Basenji', 'Basset_hound', 'Beagle', 'Bearded_collie',
13
+ 'Beauceron', 'Bedlington_terrier', 'Belgian_malinois', 'Belgian_sheepdog', 'Bernese_mountain_dog',
14
+ 'Biewer', 'Black-and-tan_coonhound', 'Black_russian_terrier', 'Border_collie', 'Border_terrier',
15
+ 'Borzoi', 'Boston_bull', 'Bouvier_des_Flandres', 'Boxer', 'Boykin_spaniel', 'Briard', 'Brittany',
16
+ 'Bull_mastiff', 'Cairn_terrier', 'Canaan_dog', 'Cavalier_king_charles_spaniel', 'Chihuahua',
17
+ 'Chinese_crested', 'Chow', 'Clumber_spaniel', 'Cocker_spaniel', 'Collie', 'Curly-coated_retriever',
18
+ 'Dachshund', 'Dalmatian', 'Dandie_Dinmont_terrier', 'Doberman', 'English_cocker_spaniel',
19
+ 'English_setter', 'English_springer_spaniel', 'Entlebucher_mountain_dog', 'Field_spaniel', 'Finnish_spitz',
20
+ 'Flat-coated_retriever', 'French_bulldog', 'German_pinscher', 'German_shepherd', 'German_short-haired_pointer',
21
+ 'Giant_schnauzer', 'Glen_of_imaal_terrier', 'Golden_retriever', 'Goldendoodle', 'Great_dane', 'Great_pyrenees',
22
+ 'Greater_swiss_mountain_dog', 'Havanese', 'Irish_setter', 'Irish_terrier', 'Irish_water_spaniel',
23
+ 'Italian_greyhound', 'Japanese_chin', 'Keeshond', 'Kerry_blue_terrier', 'King_charles_spaniel',
24
+ 'Klee_kai', 'Labrador_retriever', 'Lakeland_terrier', 'Lhasa', 'Maltese_dog', 'Manchester_terrier',
25
+ 'Mastiff', 'Miniature_pinscher', 'Miniature_schnauzer', 'Newfoundland', 'Norfolk_terrier', 'Norwegian_elkhound',
26
+ 'Norwich_terrier', 'Old_english_sheepdog', 'Otterhound', 'Papillon', 'Pekingese', 'Pembroke', 'Pharaoh_hound',
27
+ 'Plott', 'Pointer', 'Pomeranian', 'Poodle', 'Portuguese_water_dog', 'Rottweiler', 'Saint_bernard',
28
+ 'Saluki', 'Samoyed', 'Schipperke', 'Scotch_terrier', 'Shiba_inu', 'Shih-tzu', 'Siberian_husky', 'Silky_terrier',
29
+ 'Staffordshire_bullterrier', 'Standard_schnauzer', 'Tibetan_mastiff', 'Tibetan_terrier', 'Weimaraner',
30
+ 'Welsh_springer_spaniel', 'West_highland_white_terrier', 'Whippet', 'Yorkshire_terrier'
31
+ ]
32
 
33
+ # Load the trained model
34
+ model = load_model("setosys_dogs_model.h5")
35
 
36
+ # Preprocessing function
37
+ def preprocess_image(image):
38
+ img = load_img(image, target_size=(224, 224)) # Resize image to 224x224
39
  img_array = img_to_array(img)
40
  img_array = preprocess_input(img_array) # EfficientNetV2 preprocessing
41
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
 
46
  img_array = preprocess_image(image)
47
  predictions = model.predict(img_array)
48
  class_idx = np.argmax(predictions) # Get index of the class with the highest probability
49
+ breed = class_labels[class_idx] # Map the index to the breed name
50
+ confidence = predictions[0][class_idx] # Confidence score
51
  return breed, confidence
52
 
53
+ # Create a Gradio interface
54
  iface = gr.Interface(fn=predict_dog_breed,
55
+ inputs=gr.inputs.Image(type="pil"),
56
  outputs=["text", "number"],
57
  live=True)
58
 
59
  # Launch the Gradio app
60
+ iface.launch()