Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
-
import cv2
|
| 4 |
-
from tensorflow.keras.preprocessing import image
|
| 5 |
import tensorflow as tf
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load the saved model
|
| 8 |
model_path = "fahrnphi_exam_project.keras"
|
|
@@ -11,63 +12,58 @@ model = tf.keras.models.load_model(model_path)
|
|
| 11 |
# Set image dimensions
|
| 12 |
img_height, img_width = 150, 150 # Input size for the model
|
| 13 |
|
| 14 |
-
# Define
|
| 15 |
-
class_labels = {0: 'Bell Pepper', 1: 'Carrot', 2: 'Garlic', 3: 'Ginger', 4: 'Jalapeno', 5: 'Onion', 6: 'Potato', 7: 'Sweetpotato', 8: 'Tomato'}
|
| 16 |
-
|
| 17 |
-
# Define a function to predict labels for multiple regions in an image
|
| 18 |
def predict_labels_and_probabilities(image_path):
|
| 19 |
-
# Load the image
|
| 20 |
-
img =
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Find contours in the image
|
| 33 |
-
contours, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 34 |
-
|
| 35 |
predictions = []
|
| 36 |
-
|
| 37 |
-
for
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
return predictions
|
| 55 |
|
| 56 |
# Streamlit App
|
| 57 |
st.title("Intelligent Recipe Finder Classification")
|
| 58 |
|
| 59 |
-
uploaded_file = st.file_uploader("Choose an image
|
| 60 |
|
| 61 |
if uploaded_file is not None:
|
| 62 |
# Display the uploaded image
|
| 63 |
st.image(uploaded_file, caption='Uploaded Ingredient Image.', use_column_width=True)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
predictions = predict_labels_and_probabilities(uploaded_file)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
st.write("No significant ingredients detected.")
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.preprocessing import image
|
| 6 |
+
import cv2
|
| 7 |
|
| 8 |
# Load the saved model
|
| 9 |
model_path = "fahrnphi_exam_project.keras"
|
|
|
|
| 12 |
# Set image dimensions
|
| 13 |
img_height, img_width = 150, 150 # Input size for the model
|
| 14 |
|
| 15 |
+
# Define a function for prediction and returning labels and probabilities
|
|
|
|
|
|
|
|
|
|
| 16 |
def predict_labels_and_probabilities(image_path):
|
| 17 |
+
# Load the image using OpenCV
|
| 18 |
+
img = cv2.imdecode(np.frombuffer(image_path.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 19 |
+
|
| 20 |
+
# Assuming the input image might contain multiple ingredients, we will process it in patches.
|
| 21 |
+
# For simplicity, let's divide the image into 4 patches and classify each one
|
| 22 |
+
h, w, _ = img.shape
|
| 23 |
+
patches = [
|
| 24 |
+
img[0:h//2, 0:w//2], # Top-left
|
| 25 |
+
img[0:h//2, w//2:w], # Top-right
|
| 26 |
+
img[h//2:h, 0:w//2], # Bottom-left
|
| 27 |
+
img[h//2:h, w//2:w], # Bottom-right
|
| 28 |
+
]
|
| 29 |
+
|
|
|
|
|
|
|
|
|
|
| 30 |
predictions = []
|
| 31 |
+
|
| 32 |
+
for patch in patches:
|
| 33 |
+
patch_resized = cv2.resize(patch, (img_height, img_width))
|
| 34 |
+
patch_array = image.img_to_array(patch_resized)
|
| 35 |
+
patch_array = np.expand_dims(patch_array, axis=0)
|
| 36 |
+
patch_array /= 255. # Scale pixel values
|
| 37 |
+
|
| 38 |
+
preds = model.predict(patch_array)
|
| 39 |
+
class_idx = np.argmax(preds[0])
|
| 40 |
+
|
| 41 |
+
# Map class indices to class names
|
| 42 |
+
class_labels = {
|
| 43 |
+
0: 'Bell Pepper', 1: 'Carrot', 2: 'Garlic', 3: 'Ginger',
|
| 44 |
+
4: 'Jalapeno', 5: 'Onion', 6: 'Potato', 7: 'Sweetpotato', 8: 'Tomato'
|
| 45 |
+
}
|
| 46 |
+
predicted_class = class_labels[class_idx]
|
| 47 |
+
probability = preds[0][class_idx]
|
| 48 |
+
|
| 49 |
+
predictions.append((predicted_class, probability))
|
| 50 |
+
|
| 51 |
return predictions
|
| 52 |
|
| 53 |
# Streamlit App
|
| 54 |
st.title("Intelligent Recipe Finder Classification")
|
| 55 |
|
| 56 |
+
uploaded_file = st.file_uploader("Choose an ingredients image...", type="jpg")
|
| 57 |
|
| 58 |
if uploaded_file is not None:
|
| 59 |
# Display the uploaded image
|
| 60 |
st.image(uploaded_file, caption='Uploaded Ingredient Image.', use_column_width=True)
|
| 61 |
|
| 62 |
+
# Perform the prediction and display the results
|
| 63 |
predictions = predict_labels_and_probabilities(uploaded_file)
|
| 64 |
+
st.write("Predictions for different patches of the image:")
|
| 65 |
+
|
| 66 |
+
for i, (label, probability) in enumerate(predictions):
|
| 67 |
+
st.write(f"Patch {i+1}:")
|
| 68 |
+
st.write("Prediction:", label)
|
| 69 |
+
st.write("Probability:", probability)
|
|
|