File size: 2,953 Bytes
4ba9e12
7e51b59
 
 
 
4ba9e12
 
58dbd0a
 
 
 
4ba9e12
58dbd0a
 
 
 
 
 
 
 
7e51b59
5cc192b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ba9e12
 
 
 
 
5cc192b
4ba9e12
 
 
 
5cc192b
4ba9e12
 
 
 
 
 
 
 
 
 
 
 
 
5cc192b
 
7e51b59
5cc192b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from flask import Flask, render_template, request
import cv2
import numpy as np
from tensorflow.keras.models import load_model

app = Flask(__name__)

class ShelfClassifier:
    def __init__(self, model_path):
        self.model = load_model(model_path)

    def classify_image(self, image):
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        resized_image = cv2.resize(image_rgb, (224, 224))
        resized_image = resized_image.astype('float32') / 255
        resized_image = np.expand_dims(resized_image, axis=0)
        prediction = self.model.predict(resized_image)
        class_index = np.argmax(prediction)
        class_label = "Disorganized or Empty" if class_index == 1 else "Organized"
        return class_label

index_html = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shelf Classifier</title>
</head>
<body>
    <h1>Shelf Classifier</h1>
    <form method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="Upload">
    </form>
    {% if message %}
    <p>{{ message }}</p>
    {% endif %}
</body>
</html>
"""

result_html = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Classification Result</title>
</head>
<body>
    <h1>Classification Result</h1>
    <p>{{ class_label }}</p>
    <img src="result.jpg" alt="Result Image">
</body>
</html>
"""

@app.route('/', methods=['GET', 'POST'])
def upload_image():
    if request.method == 'POST':
        # Check if the post request has the file part
        if 'file' not in request.files:
            return render_template_string(index_html, message='No file part')
        file = request.files['file']
        # If user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            return render_template_string(index_html, message='No selected file')
        if file:
            # Read uploaded image
            image = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_COLOR)
            # Initialize ShelfClassifier with the model
            classifier = ShelfClassifier('saved_model.h5')
            # Perform classification
            class_label = classifier.classify_image(image)
            # Draw bounding box if shelf is disorganized or empty
            if class_label == "Disorganized or Empty":
                # Draw red rectangle
                cv2.rectangle(image, (0, 0), (image.shape[1], image.shape[0]), (255, 0, 0), 2)
            # Save image with bounding box
            cv2.imwrite('result.jpg', image)
            return render_template_string(result_html, class_label=class_label)
    return render_template_string(index_html)

if __name__ == '__main__':
    app.run(debug=True)