mrvero commited on
Commit
2ef38c7
·
verified ·
1 Parent(s): 997141e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.layers import DepthwiseConv2D
4
+ from PIL import Image, ImageOps
5
+ import numpy as np
6
+
7
+ # Disable scientific notation for clarity
8
+ np.set_printoptions(suppress=True)
9
+
10
+ # Custom object for DepthwiseConv2D
11
+ custom_objects = {'DepthwiseConv2D': DepthwiseConv2D}
12
+
13
+ # Load the model with custom objects
14
+ model = load_model("model/pleasuredomes_image_model.h5", custom_objects=custom_objects, compile=False)
15
+
16
+ # Load the labels
17
+ class_names = open("model/labels.txt", "r").readlines()
18
+
19
+ def predict_image(image):
20
+ """
21
+ Function to process the image and make a prediction using the loaded model.
22
+ """
23
+ # Resize the image to be at least 224x224 and then crop from the center
24
+ size = (224, 224)
25
+ image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
26
+
27
+ # Turn the image into a numpy array
28
+ image_array = np.asarray(image)
29
+
30
+ # Normalize the image
31
+ normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
32
+
33
+ # Create the array of the right shape to feed into the keras model
34
+ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
35
+ data[0] = normalized_image_array
36
+
37
+ # Predict the model
38
+ prediction = model.predict(data)
39
+ index = np.argmax(prediction)
40
+ class_name = class_names[index].strip()
41
+ confidence_score = prediction[0][index]
42
+
43
+ return f"Class: {class_name}, Confidence Score: {confidence_score:.2f}"
44
+
45
+ # Create a Gradio interface
46
+ interface = gr.Interface(
47
+ fn=predict_image,
48
+ inputs=gr.inputs.Image(type="pil"),
49
+ outputs="text",
50
+ title="Image Classification",
51
+ description="Upload an image to classify it using the pre-trained model."
52
+ )
53
+
54
+ # Launch the interface
55
+ if __name__ == "__main__":
56
+ interface.launch()