Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,73 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
-
import tensorflow as tf
|
| 4 |
-
from tensorflow.keras.preprocessing import image
|
| 5 |
import cv2
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load the saved model
|
| 8 |
model_path = "fahrnphi_exam_project.keras"
|
| 9 |
model = tf.keras.models.load_model(model_path)
|
| 10 |
|
| 11 |
-
|
| 12 |
# Set image dimensions
|
| 13 |
img_height, img_width = 150, 150 # Input size for the model
|
| 14 |
|
| 15 |
-
# Define
|
|
|
|
|
|
|
|
|
|
| 16 |
def predict_labels_and_probabilities(image_path):
|
| 17 |
-
# Load the image
|
| 18 |
-
img =
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
predictions = []
|
| 32 |
-
|
| 33 |
-
for
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
predictions.append((predicted_class, probability))
|
| 51 |
-
|
| 52 |
return predictions
|
| 53 |
|
| 54 |
# Streamlit App
|
| 55 |
st.title("Intelligent Recipe Finder Classification")
|
| 56 |
|
| 57 |
-
uploaded_file = st.file_uploader("Choose an ingredients
|
| 58 |
|
| 59 |
if uploaded_file is not None:
|
| 60 |
# Display the uploaded image
|
| 61 |
st.image(uploaded_file, caption='Uploaded Ingredient Image.', use_column_width=True)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
predictions = predict_labels_and_probabilities(uploaded_file)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
| 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"
|
| 9 |
model = tf.keras.models.load_model(model_path)
|
| 10 |
|
|
|
|
| 11 |
# Set image dimensions
|
| 12 |
img_height, img_width = 150, 150 # Input size for the model
|
| 13 |
|
| 14 |
+
# Define class labels
|
| 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 = image.load_img(image_path)
|
| 21 |
+
img_array = image.img_to_array(img)
|
| 22 |
+
|
| 23 |
+
# Convert to grayscale
|
| 24 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
|
| 25 |
+
|
| 26 |
+
# Apply GaussianBlur to reduce noise and improve contour detection
|
| 27 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 28 |
+
|
| 29 |
+
# Find edges in the image using Canny edge detection
|
| 30 |
+
edged = cv2.Canny(blurred, 50, 150)
|
| 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 contour in contours:
|
| 38 |
+
# Create a bounding box around each contour
|
| 39 |
+
x, y, w, h = cv2.boundingRect(contour)
|
| 40 |
+
if w > 50 and h > 50: # Filter out small boxes
|
| 41 |
+
roi = img_array[y:y+h, x:x+w]
|
| 42 |
+
roi = cv2.resize(roi, (img_height, img_width))
|
| 43 |
+
roi = np.expand_dims(roi, axis=0)
|
| 44 |
+
roi = roi / 255.0 # Scale image pixels
|
| 45 |
+
|
| 46 |
+
# Predict with the model
|
| 47 |
+
preds = model.predict(roi)
|
| 48 |
+
class_idx = np.argmax(preds[0])
|
| 49 |
+
predicted_class = class_labels[class_idx]
|
| 50 |
+
probability = preds[0][class_idx]
|
| 51 |
+
|
| 52 |
+
predictions.append((predicted_class, probability))
|
| 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 with ingredients...", type="jpg")
|
| 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 |
+
# Predict labels and probabilities for multiple regions
|
| 66 |
predictions = predict_labels_and_probabilities(uploaded_file)
|
| 67 |
+
|
| 68 |
+
if predictions:
|
| 69 |
+
st.write("Predictions:")
|
| 70 |
+
for i, (label, probability) in enumerate(predictions):
|
| 71 |
+
st.write(f"{i+1}. **Prediction:** {label} | **Probability:** {probability:.2f}")
|
| 72 |
+
else:
|
| 73 |
+
st.write("No significant ingredients detected.")
|