Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog
- from keras.models import load_model
- from PIL import Image, ImageOps, ImageTk
- import numpy as np
- # Load the model
- model = load_model("keras_Model.h5", compile=False)
- # Load the labels
- class_names = open("labels.txt", "r").readlines()
- bgc = "#000099";
- def classify_image():
- # Open file dialog to choose an image
- file_path = filedialog.askopenfilename()
- # Load and preprocess the image
- image = Image.open(file_path).convert("RGB")
- size = (224, 224)
- image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
- image_array = np.asarray(image)
- normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
- data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
- data[0] = normalized_image_array
- # Predict the class
- prediction = model.predict(data)
- index = np.argmax(prediction)
- class_name = class_names[index]
- confidence_score = prediction[0][index]
- # Display the result in the GUI
- result_label.config(
- text=f"Class: {class_name[2:]}\nConfidence Score: {confidence_score:.4f}",
- background=bgc,
- fg="white",
- font=("Arial", 15, "bold italic")
- )
- # Display the image in the GUI
- img = ImageTk.PhotoImage(image)
- image_label.config(image=img)
- image_label.image = img
- def clear_image():
- # Set image_label to None to clear the displayed image
- image_label.config(image=None)
- image_label.image = None
- # Create the GUI window
- pro = tk.Tk()
- pro.geometry("950x600+300+100") # width x heigth + left + top
- pro.resizable(True, True)
- pro.title("accessories Recognition")
- pro.config(background="black")
- pro.minsize(400, 400)
- pro.maxsize(2000, 2000)
- lbl = tk.Label(
- text="Accessories",
- fg="white",
- bg=bgc,
- font=("Arial", 15, "bold italic"),
- )
- lbl.pack(side="top", pady=25)
- # Create the browse button
- browse_button = tk.Button(pro, text="Upload Image", command=classify_image)
- browse_button.pack(side="bottom", pady=20)
- browse_button.configure(
- background=bgc, foreground="white", font=("arial", 15, "bold")
- )
- # Create the clear button
- clear_button = tk.Button(pro, text="Clear Image", command=clear_image)
- clear_button.pack(side="bottom", pady=20)
- clear_button.configure(
- background=bgc, foreground="white", font=("arial", 15, "bold")
- )
- # Create the label to display the result
- result_label = tk.Label(pro, text="")
- result_label.pack()
- # Create the label to display the image
- image_label = tk.Label(pro)
- image_label.place(relx=0.5, rely=0.5, anchor="center")
- # Start the GUI event loop
- pro.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement