Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tkinter as tk
|
2 |
+
from tkinter import filedialog
|
3 |
+
import cv2
|
4 |
+
from PIL import Image, ImageTk
|
5 |
+
import numpy as np
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
|
8 |
+
class ShelfClassifierApp:
|
9 |
+
def __init__(self, master):
|
10 |
+
self.master = master
|
11 |
+
self.master.title("Shelf Classifier")
|
12 |
+
|
13 |
+
self.model = load_model('your_model.h5') # Load your model
|
14 |
+
|
15 |
+
self.canvas = tk.Canvas(master, width=300, height=300)
|
16 |
+
self.canvas.pack()
|
17 |
+
|
18 |
+
self.load_button = tk.Button(master, text="Load Image", command=self.load_image)
|
19 |
+
self.load_button.pack()
|
20 |
+
|
21 |
+
self.classify_button = tk.Button(master, text="Classify", command=self.classify_image)
|
22 |
+
self.classify_button.pack()
|
23 |
+
|
24 |
+
self.result_label = tk.Label(master, text="")
|
25 |
+
self.result_label.pack()
|
26 |
+
|
27 |
+
self.image = None
|
28 |
+
|
29 |
+
def load_image(self):
|
30 |
+
file_path = filedialog.askopenfilename()
|
31 |
+
if file_path:
|
32 |
+
self.image = cv2.imread(file_path)
|
33 |
+
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
|
34 |
+
self.display_image(self.image)
|
35 |
+
|
36 |
+
def display_image(self, image):
|
37 |
+
image = Image.fromarray(image)
|
38 |
+
image = ImageTk.PhotoImage(image)
|
39 |
+
self.canvas.create_image(0, 0, anchor=tk.NW, image=image)
|
40 |
+
self.canvas.image = image
|
41 |
+
|
42 |
+
def classify_image(self):
|
43 |
+
if self.image is not None:
|
44 |
+
# Preprocess the image
|
45 |
+
resized_image = cv2.resize(self.image, (224, 224))
|
46 |
+
resized_image = resized_image.astype('float32') / 255
|
47 |
+
resized_image = np.expand_dims(resized_image, axis=0)
|
48 |
+
|
49 |
+
# Make prediction
|
50 |
+
prediction = self.model.predict(resized_image)
|
51 |
+
|
52 |
+
# Postprocess the prediction
|
53 |
+
class_index = np.argmax(prediction)
|
54 |
+
class_label = "Disorganized or Empty" if class_index == 1 else "Organized"
|
55 |
+
|
56 |
+
# Draw bounding box if shelf is disorganized or empty
|
57 |
+
if class_index == 1:
|
58 |
+
# Draw red rectangle
|
59 |
+
image_with_box = cv2.rectangle(self.image, (0, 0), (self.image.shape[1], self.image.shape[0]), (255, 0, 0), 2)
|
60 |
+
self.display_image(image_with_box)
|
61 |
+
else:
|
62 |
+
self.display_image(self.image)
|
63 |
+
|
64 |
+
self.result_label.config(text=class_label)
|
65 |
+
else:
|
66 |
+
self.result_label.config(text="Please load an image first")
|
67 |
+
|
68 |
+
def main():
|
69 |
+
root = tk.Tk()
|
70 |
+
app = ShelfClassifierApp(root)
|
71 |
+
root.mainloop()
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
main()
|