bauckluc commited on
Commit
e44d4e7
·
verified ·
1 Parent(s): bbb7b1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -14
app.py CHANGED
@@ -3,11 +3,26 @@ import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
 
6
-
7
  model_path = "DogClassifier2.1.keras"
8
  model = tf.keras.models.load_model(model_path)
9
 
10
- labels = ['Afghan', 'African Wild Dog', 'Airedale', 'American Hairless', 'American Spaniel', 'Basenji', 'Basset', 'Beagle',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  'Bearded Collie', 'Bermaise', 'Bichon Frise', 'Blenheim', 'Bloodhound', 'Bluetick', 'Border Collie', 'Borzoi',
12
  'Boston Terrier', 'Boxer', 'Bull Mastiff', 'Bull Terrier', 'Bulldog', 'Cairn', 'Chihuahua', 'Chinese Crested',
13
  'Chow', 'Clumber','Cockapoo', 'Cocker', 'Collie', 'Corgi', 'Coyote', 'Dalmation', 'Dhole', 'Dingo', 'Doberman',
@@ -16,22 +31,19 @@ labels = ['Afghan', 'African Wild Dog', 'Airedale', 'American Hairless', 'Americ
16
  'Lhasa', 'Malinois', 'Maltese', 'Maltese', 'Mex Hairless', 'Newfoundland', 'Pekinese', 'Pit Bull', 'Pomeranian',
17
  'Poodle', 'Pug', 'Rhodesian', 'Rottweiler', 'Saint Bernard', 'Schnauzer', 'Scotch Terrier', 'Shar_Pei',
18
  'Shiba Inu', 'Shih-Tzu', 'Siberian Husky', 'Vizsla', 'Yorkie']
19
-
20
- def predict_regression(image):
21
- # Preprocess image
22
- image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
23
- image = image.resize((150, 150)).convert('L') #resize the image to 150x150 and converts it to gray scale
24
- image = np.array(image)
25
- print(image.shape)
26
- # Predict
27
- prediction = model.predict(image[None, ...]) # Assuming single regression value
28
- confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
29
- return confidences
30
 
31
 
32
  input_image = gr.Image()
33
  iface = gr.Interface(
34
- fn=predict_regression,
35
  inputs=input_image,
36
  outputs=gr.Label(),
37
  description="A simple MLP classification model for image classification using the MNIST dataset.")
 
3
  import numpy as np
4
  from PIL import Image
5
 
 
6
  model_path = "DogClassifier2.1.keras"
7
  model = tf.keras.models.load_model(model_path)
8
 
9
+ # Define the core prediction function
10
+ def predict_bmwX(image):
11
+ # Preprocess image
12
+ image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
13
+ image = image.convert("RGB") # Ensure the image is in RGB format
14
+ image = image.resize((150, 150)) # Resize the image to 150x150
15
+ image = np.array(image)
16
+ image = np.expand_dims(image, axis=0) # Add batch dimension
17
+
18
+ # Predict
19
+ prediction = model.predict(image)
20
+
21
+ # Apply softmax to get probabilities for each class
22
+ prediction = tf.nn.softmax(prediction)
23
+
24
+ # Define class names
25
+ class_names = ['Afghan', 'African Wild Dog', 'Airedale', 'American Hairless', 'American Spaniel', 'Basenji', 'Basset', 'Beagle',
26
  'Bearded Collie', 'Bermaise', 'Bichon Frise', 'Blenheim', 'Bloodhound', 'Bluetick', 'Border Collie', 'Borzoi',
27
  'Boston Terrier', 'Boxer', 'Bull Mastiff', 'Bull Terrier', 'Bulldog', 'Cairn', 'Chihuahua', 'Chinese Crested',
28
  'Chow', 'Clumber','Cockapoo', 'Cocker', 'Collie', 'Corgi', 'Coyote', 'Dalmation', 'Dhole', 'Dingo', 'Doberman',
 
31
  'Lhasa', 'Malinois', 'Maltese', 'Maltese', 'Mex Hairless', 'Newfoundland', 'Pekinese', 'Pit Bull', 'Pomeranian',
32
  'Poodle', 'Pug', 'Rhodesian', 'Rottweiler', 'Saint Bernard', 'Schnauzer', 'Scotch Terrier', 'Shar_Pei',
33
  'Shiba Inu', 'Shih-Tzu', 'Siberian Husky', 'Vizsla', 'Yorkie']
34
+
35
+ # Create a dictionary with the probabilities for each dog breed
36
+ prediction_dict = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
37
+
38
+ # Sort the dictionary by value in descending order and get the top 3 classes
39
+ sorted_predictions = dict(sorted(prediction_dict.items(), key=lambda item: item[1], reverse=True))
40
+
41
+ return sorted_predictions
 
 
 
42
 
43
 
44
  input_image = gr.Image()
45
  iface = gr.Interface(
46
+ fn=predict_bmwX,
47
  inputs=input_image,
48
  outputs=gr.Label(),
49
  description="A simple MLP classification model for image classification using the MNIST dataset.")