Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,28 @@
|
|
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
|
9 |
-
def __init__(self,
|
10 |
-
self.
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
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 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
|
73 |
if __name__ == "__main__":
|
74 |
main()
|
|
|
|
|
|
|
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, image_path):
|
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
|
14 |
+
resized_image = np.expand_dims(resized_image, axis=0)
|
15 |
+
prediction = self.model.predict(resized_image)
|
16 |
+
class_index = np.argmax(prediction)
|
17 |
+
class_label = "Disorganized or Empty" if class_index == 1 else "Organized"
|
18 |
+
return class_label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def main():
|
21 |
+
model_path = 'your_model.h5'
|
22 |
+
classifier = ShelfClassifier(model_path)
|
23 |
+
image_path = 'your_image.jpg' # Change to your image path
|
24 |
+
result = classifier.classify_image(image_path)
|
25 |
+
print("Classification Result:", result)
|
26 |
|
27 |
if __name__ == "__main__":
|
28 |
main()
|