Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,15 @@
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.models import load_model
|
4 |
|
|
|
|
|
5 |
class ShelfClassifier:
|
6 |
def __init__(self, model_path):
|
7 |
self.model = load_model(model_path)
|
8 |
|
9 |
-
def classify_image(self,
|
10 |
-
image = cv2.imread(image_path)
|
11 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
12 |
resized_image = cv2.resize(image_rgb, (224, 224))
|
13 |
resized_image = resized_image.astype('float32') / 255
|
@@ -17,12 +19,32 @@ class ShelfClassifier:
|
|
17 |
class_label = "Disorganized or Empty" if class_index == 1 else "Organized"
|
18 |
return class_label
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
if __name__ ==
|
28 |
-
|
|
|
1 |
+
from flask import Flask, render_template, request
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.models import load_model
|
5 |
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
class ShelfClassifier:
|
9 |
def __init__(self, model_path):
|
10 |
self.model = load_model(model_path)
|
11 |
|
12 |
+
def classify_image(self, image):
|
|
|
13 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
14 |
resized_image = cv2.resize(image_rgb, (224, 224))
|
15 |
resized_image = resized_image.astype('float32') / 255
|
|
|
19 |
class_label = "Disorganized or Empty" if class_index == 1 else "Organized"
|
20 |
return class_label
|
21 |
|
22 |
+
@app.route('/', methods=['GET', 'POST'])
|
23 |
+
def upload_image():
|
24 |
+
if request.method == 'POST':
|
25 |
+
# Check if the post request has the file part
|
26 |
+
if 'file' not in request.files:
|
27 |
+
return render_template('index.html', message='No file part')
|
28 |
+
file = request.files['file']
|
29 |
+
# If user does not select file, browser also
|
30 |
+
# submit an empty part without filename
|
31 |
+
if file.filename == '':
|
32 |
+
return render_template('index.html', message='No selected file')
|
33 |
+
if file:
|
34 |
+
# Read uploaded image
|
35 |
+
image = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_COLOR)
|
36 |
+
# Initialize ShelfClassifier with the model
|
37 |
+
classifier = ShelfClassifier('saved_model.h5')
|
38 |
+
# Perform classification
|
39 |
+
class_label = classifier.classify_image(image)
|
40 |
+
# Draw bounding box if shelf is disorganized or empty
|
41 |
+
if class_label == "Disorganized or Empty":
|
42 |
+
# Draw red rectangle
|
43 |
+
cv2.rectangle(image, (0, 0), (image.shape[1], image.shape[0]), (255, 0, 0), 2)
|
44 |
+
# Save image with bounding box
|
45 |
+
cv2.imwrite('result.jpg', image)
|
46 |
+
return render_template('result.html', class_label=class_label)
|
47 |
+
return render_template('index.html')
|
48 |
|
49 |
+
if __name__ == '__main__':
|
50 |
+
app.run(debug=True)
|