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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -46
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 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), 1)
19
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
20
-
21
- # Assuming the input image might contain multiple ingredients, we will process it in patches.
22
- # For simplicity, let's divide the image into 4 patches and classify each one
23
- h, w, _ = img.shape
24
- patches = [
25
- img_rgb[0:h//2, 0:w//2], # Top-left
26
- img_rgb[0:h//2, w//2:w], # Top-right
27
- img_rgb[h//2:h, 0:w//2], # Bottom-left
28
- img_rgb[h//2:h, w//2:w], # Bottom-right
29
- ]
30
-
 
 
31
  predictions = []
32
-
33
- for patch in patches:
34
- patch_resized = cv2.resize(patch, (img_height, img_width))
35
- patch_array = image.img_to_array(patch_resized)
36
- patch_array = np.expand_dims(patch_array, axis=0)
37
- patch_array /= 255. # Scale pixel values
38
-
39
- preds = model.predict(patch_array)
40
- class_idx = np.argmax(preds[0])
41
-
42
- # Map class indices to class names
43
- class_labels = {
44
- 0: 'Bell Pepper', 1: 'Carrot', 2: 'Garlic', 3: 'Ginger',
45
- 4: 'Jalapeno', 5: 'Onion', 6: 'Potato', 7: 'Sweetpotato', 8: 'Tomato'
46
- }
47
- predicted_class = class_labels[class_idx]
48
- probability = preds[0][class_idx]
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 image...", type="jpg")
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
- # Perform the prediction and display the results
64
  predictions = predict_labels_and_probabilities(uploaded_file)
65
- st.write("Predictions for different patches of the image:")
66
-
67
- for i, (label, probability) in enumerate(predictions):
68
- st.write(f"Patch {i+1}:")
69
- st.write("Prediction:", label)
70
- st.write("Probability:", probability)
 
 
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.")