Spaces:
Runtime error
Runtime error
Apply fixes to preprocess for scaling and single batch pred index
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import from_pretrained_keras
|
| 3 |
import tensorflow as tf
|
|
|
|
| 4 |
|
| 5 |
from labels import CLASS_LABELS
|
| 6 |
|
|
@@ -12,15 +13,14 @@ def _preprocess(image_path: str):
|
|
| 12 |
image = tf.keras.utils.load_img(image_path, target_size=(224, 224))
|
| 13 |
input_arr = tf.keras.utils.img_to_array(image)
|
| 14 |
input_arr = np.array([input_arr]) # Convert single image to a batch.
|
| 15 |
-
return input_arr
|
| 16 |
|
| 17 |
|
| 18 |
def predict(image):
|
| 19 |
image_array = _preprocess(image)
|
| 20 |
-
predictions = model.predict(image_array)
|
| 21 |
return {k:v for k, v in zip(CLASS_LABELS, predictions)}
|
| 22 |
|
| 23 |
-
|
| 24 |
gr.Interface(
|
| 25 |
predict,
|
| 26 |
inputs=gr.inputs.Image(label="Upload food image", type="filepath"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import from_pretrained_keras
|
| 3 |
import tensorflow as tf
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
from labels import CLASS_LABELS
|
| 7 |
|
|
|
|
| 13 |
image = tf.keras.utils.load_img(image_path, target_size=(224, 224))
|
| 14 |
input_arr = tf.keras.utils.img_to_array(image)
|
| 15 |
input_arr = np.array([input_arr]) # Convert single image to a batch.
|
| 16 |
+
return input_arr / 255.
|
| 17 |
|
| 18 |
|
| 19 |
def predict(image):
|
| 20 |
image_array = _preprocess(image)
|
| 21 |
+
predictions = model.predict(image_array)[0] # single pred batch
|
| 22 |
return {k:v for k, v in zip(CLASS_LABELS, predictions)}
|
| 23 |
|
|
|
|
| 24 |
gr.Interface(
|
| 25 |
predict,
|
| 26 |
inputs=gr.inputs.Image(label="Upload food image", type="filepath"),
|