fahrnphi commited on
Commit
cc0fba2
·
verified ·
1 Parent(s): e1402db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -49
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 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.")
 
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)