fahrnphi commited on
Commit
e0d3bec
·
verified ·
1 Parent(s): 900ca9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -16,37 +16,33 @@ img_height, img_width = 150, 150 # Input size for the model
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
 
@@ -58,12 +54,19 @@ uploaded_file = st.file_uploader("Choose an ingredients image...", type="jpg")
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)
 
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
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
20
 
21
+ # Scan the image in a grid-like fashion
22
+ step_size = 100 # Step size for the grid
 
 
 
 
 
 
 
 
23
  predictions = []
24
 
25
+ for y in range(0, img.shape[0] - img_height, step_size):
26
+ for x in range(0, img.shape[1] - img_width, step_size):
27
+ patch = img_rgb[y:y+img_height, x:x+img_width]
28
+ patch_resized = cv2.resize(patch, (img_height, img_width))
29
+ patch_array = image.img_to_array(patch_resized)
30
+ patch_array = np.expand_dims(patch_array, axis=0)
31
+ patch_array /= 255. # Scale pixel values
32
+
33
+ preds = model.predict(patch_array)
34
+ class_idx = np.argmax(preds[0])
35
+
36
+ # Map class indices to class names
37
+ class_labels = {
38
+ 0: 'Bell Pepper', 1: 'Carrot', 2: 'Garlic', 3: 'Ginger',
39
+ 4: 'Jalapeno', 5: 'Onion', 6: 'Potato', 7: 'Sweetpotato', 8: 'Tomato'
40
+ }
41
+ predicted_class = class_labels[class_idx]
42
+ probability = preds[0][class_idx]
43
+
44
+ if probability > 0.5: # Threshold to filter out low confidence predictions
45
+ predictions.append((predicted_class, probability, x, y, x+img_width, y+img_height))
46
 
47
  return predictions
48
 
 
54
  if uploaded_file is not None:
55
  # Display the uploaded image
56
  st.image(uploaded_file, caption='Uploaded Ingredient Image.', use_column_width=True)
57
+
58
  # Perform the prediction and display the results
59
  predictions = predict_labels_and_probabilities(uploaded_file)
60
+ st.write("Predictions for detected ingredients in the image:")
61
+
62
+ for i, (label, probability, x1, y1, x2, y2) in enumerate(predictions):
63
+ st.write(f"Ingredient {i+1}:")
64
+ st.write(f"Prediction: {label}")
65
+ st.write(f"Probability: {probability}")
66
+ st.write(f"Location: ({x1}, {y1}) to ({x2}, {y2})")
67
+
68
+ # Draw rectangle around detected ingredients
69
+ cv2.rectangle(img_rgb, (x1, y1), (x2, y2), (255, 0, 0), 2)
70
 
71
+ # Display the image with rectangles
72
+ st.image(img_rgb, caption='Detected Ingredients', use_column_width=True)