Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 21 |
-
#
|
| 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
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 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)
|
|
|
|
|
|