Delete utils.py
Browse files
utils.py
DELETED
|
@@ -1,74 +0,0 @@
|
|
| 1 |
-
import tensorflow as tf
|
| 2 |
-
import numpy as np
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import json
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
def load_model_and_classes(model_path='dish_classifier_final.keras',
|
| 8 |
-
classes_path='class_names.json'):
|
| 9 |
-
"""Load the trained model and class names"""
|
| 10 |
-
|
| 11 |
-
# Load model
|
| 12 |
-
if not os.path.exists(model_path):
|
| 13 |
-
raise FileNotFoundError(f"Model file not found: {model_path}")
|
| 14 |
-
|
| 15 |
-
try:
|
| 16 |
-
model = tf.keras.models.load_model(model_path)
|
| 17 |
-
except Exception as e:
|
| 18 |
-
raise Exception(f"Error loading model: {e}")
|
| 19 |
-
|
| 20 |
-
# Load class names
|
| 21 |
-
if not os.path.exists(classes_path):
|
| 22 |
-
raise FileNotFoundError(f"Class names file not found: {classes_path}")
|
| 23 |
-
|
| 24 |
-
try:
|
| 25 |
-
with open(classes_path, 'r') as f:
|
| 26 |
-
class_names = json.load(f)
|
| 27 |
-
except Exception as e:
|
| 28 |
-
raise Exception(f"Error loading class names: {e}")
|
| 29 |
-
|
| 30 |
-
return model, class_names
|
| 31 |
-
|
| 32 |
-
def preprocess_image(image):
|
| 33 |
-
"""Preprocess image for EfficientNetV2S model"""
|
| 34 |
-
# Convert to RGB if needed
|
| 35 |
-
if image.mode != 'RGB':
|
| 36 |
-
image = image.convert('RGB')
|
| 37 |
-
|
| 38 |
-
# Resize to 224x224
|
| 39 |
-
image = image.resize((224, 224))
|
| 40 |
-
|
| 41 |
-
# Convert to array and normalize
|
| 42 |
-
img_array = np.array(image) / 255.0
|
| 43 |
-
|
| 44 |
-
# Add batch dimension
|
| 45 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 46 |
-
|
| 47 |
-
return img_array
|
| 48 |
-
|
| 49 |
-
def predict_image(model, image, class_names, top_k=5):
|
| 50 |
-
"""Predict top K classes for an image"""
|
| 51 |
-
|
| 52 |
-
# Preprocess
|
| 53 |
-
processed_image = preprocess_image(image)
|
| 54 |
-
|
| 55 |
-
# Make prediction
|
| 56 |
-
predictions = model.predict(processed_image, verbose=0)[0]
|
| 57 |
-
|
| 58 |
-
# Get top K predictions
|
| 59 |
-
top_k_idx = np.argsort(predictions)[-top_k:][::-1]
|
| 60 |
-
top_k_labels = [class_names[idx] for idx in top_k_idx]
|
| 61 |
-
top_k_confidences = [predictions[idx] * 100 for idx in top_k_idx]
|
| 62 |
-
|
| 63 |
-
return top_k_labels, top_k_confidences
|
| 64 |
-
|
| 65 |
-
def get_pakistan_food_info(dish_name):
|
| 66 |
-
"""Get information about a Pakistani dish (optional feature)"""
|
| 67 |
-
# Add more dishes as needed
|
| 68 |
-
pakistan_food_info = {
|
| 69 |
-
"biryani": "A mixed rice dish with meat, spices, and saffron. Popular throughout Pakistan.",
|
| 70 |
-
"nihari": "A slow-cooked stew with bone-in meat, originating from Old Delhi but loved in Pakistan.",
|
| 71 |
-
# Add more dish info here
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
return pakistan_food_info.get(dish_name.lower(), "A delicious Pakistani dish!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|