rcodina commited on
Commit
c4e3d46
·
verified ·
1 Parent(s): 24190c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from keras.models import load_model # TensorFlow is required for Keras to work
2
+ from PIL import Image, ImageOps # Install pillow instead of PIL
3
+ import numpy as np
4
+ import gradio as gr
5
+
6
+ # Disable scientific notation for clarity
7
+ np.set_printoptions(suppress=True)
8
+
9
+ # Load the model
10
+ model = load_model("/content/keras_model.h5", compile=False)
11
+
12
+ # Load the labels
13
+ class_names = open("/content/labels.txt", "r").readlines()
14
+
15
+ # Create the array of the right shape to feed into the keras model
16
+ # The 'length' or number of images you can put into the array is
17
+ # determined by the first position in the shape tuple, in this case 1
18
+ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
19
+
20
+ def saluda(img):
21
+ # Replace this with the path to your image
22
+ image = img
23
+
24
+ # resizing the image to be at least 224x224 and then cropping from the center
25
+ size = (224, 224)
26
+ image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
27
+
28
+ # turn the image into a numpy array
29
+ image_array = np.asarray(image)
30
+
31
+ # Normalize the image
32
+ normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
33
+
34
+ # Load the image into the array
35
+ data[0] = normalized_image_array
36
+
37
+ # Predicts the model
38
+ prediction = model.predict(data)
39
+ index = np.argmax(prediction)
40
+ class_name = class_names[index]
41
+ confidence_score = prediction[0][index]
42
+
43
+ return class_name[2:], confidence_score
44
+
45
+ imatge_entrada = gr.Image(type='pil')
46
+ demo = gr.Interface(fn=saluda, inputs=imatge_entrada, outputs=["text", "text"])
47
+
48
+ demo.launch()