Conner commited on
Commit
7a29178
1 Parent(s): c5f9302

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
app.py CHANGED
@@ -4,30 +4,37 @@ import numpy as np
4
  import gradio as gr
5
 
6
 
 
 
 
 
 
7
  def greet(name):
8
  return "Hello " + name + "!!"
9
 
10
- def predict(img):
11
- # Load the model
12
- model = load_model('keras_model.h5')
13
- # Create the array of the right shape to feed into the keras model
14
- # The 'length' or number of images you can put into the array is
15
- # determined by the first position in the shape tuple, in this case 1.
16
- data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
17
- # Replace this with the path to your image
18
- image = img
19
- # image = Image.open('<IMAGE_PATH>')
20
- #resize the image to a 224x224 with the same strategy as in TM2:
21
- #resizing the image to be at least 224x224 and then cropping from the center
22
- size = (224, 224)
23
- image = ImageOps.fit(image, size)
24
- #turn the image into a numpy array
25
-
26
-
27
- data[0] = np.asarray(image)
28
- # run the inference
29
- prediction = model.predict(data)
30
- gr.outputs.Label = open(labels.txt)
 
 
31
  return prediction
32
 
33
 
 
4
  import gradio as gr
5
 
6
 
7
+ # Load the model
8
+ model = load_model('keras_model.h5')
9
+
10
+
11
+
12
  def greet(name):
13
  return "Hello " + name + "!!"
14
 
15
+ def predict(img):
16
+
17
+ # Create the array of the right shape to feed into the keras model
18
+ # The 'length' or number of images you can put into the array is
19
+ # determined by the first position in the shape tuple, in this case 1.
20
+ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
21
+ # Replace this with the path to your image
22
+ image = Image.open('<IMAGE_PATH>')
23
+ #resize the image to a 224x224 with the same strategy as in TM2:
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.ANTIALIAS)
27
+
28
+ #turn the image into a numpy array
29
+ image_array = np.asarray(image)
30
+ # Normalize the image
31
+ normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
32
+ # Load the image into the array
33
+ data[0] = normalized_image_array
34
+
35
+ # run the inference
36
+ prediction = model.predict(data)
37
+ print(prediction)
38
  return prediction
39
 
40