Update food_identification.py
Browse files- food_identification.py +5 -4
food_identification.py
CHANGED
|
@@ -1,16 +1,17 @@
|
|
| 1 |
-
import
|
| 2 |
import tensorflow_hub as hub
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
# Load the Food101 model from TensorFlow Hub
|
| 6 |
MODEL_URL = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/5"
|
| 7 |
model = hub.load(MODEL_URL)
|
| 8 |
|
| 9 |
-
# Download and load
|
| 10 |
LABELS_URL = "https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json"
|
| 11 |
labels_path = tf.keras.utils.get_file("imagenet_labels.json", LABELS_URL)
|
| 12 |
|
| 13 |
-
# Properly parse the JSON file
|
| 14 |
with open(labels_path, "r") as f:
|
| 15 |
food_labels = json.load(f)
|
| 16 |
|
|
@@ -27,6 +28,6 @@ def identify_food(image):
|
|
| 27 |
predictions = model(image_array)
|
| 28 |
predicted_class = np.argmax(predictions[0])
|
| 29 |
|
| 30 |
-
# Get the top prediction
|
| 31 |
food_item = food_labels.get(str(predicted_class), ["Unknown"])[1]
|
| 32 |
return [food_item]
|
|
|
|
| 1 |
+
import tensorflow as tf # Import TensorFlow
|
| 2 |
import tensorflow_hub as hub
|
| 3 |
+
import json
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Load the Food101 model from TensorFlow Hub
|
| 7 |
MODEL_URL = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/5"
|
| 8 |
model = hub.load(MODEL_URL)
|
| 9 |
|
| 10 |
+
# Download and load ImageNet labels
|
| 11 |
LABELS_URL = "https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json"
|
| 12 |
labels_path = tf.keras.utils.get_file("imagenet_labels.json", LABELS_URL)
|
| 13 |
|
| 14 |
+
# Properly parse the JSON file to load labels
|
| 15 |
with open(labels_path, "r") as f:
|
| 16 |
food_labels = json.load(f)
|
| 17 |
|
|
|
|
| 28 |
predictions = model(image_array)
|
| 29 |
predicted_class = np.argmax(predictions[0])
|
| 30 |
|
| 31 |
+
# Get the top prediction label
|
| 32 |
food_item = food_labels.get(str(predicted_class), ["Unknown"])[1]
|
| 33 |
return [food_item]
|