Advertisement
sherry_ahmos

Untitled

May 9th, 2023
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog
  3. from keras.models import load_model
  4. from PIL import Image, ImageOps, ImageTk
  5. import numpy as np
  6.  
  7. # Load the model
  8. model = load_model("keras_Model.h5", compile=False)
  9.  
  10. # Load the labels
  11. class_names = open("labels.txt", "r").readlines()
  12. bgc = "#000099";
  13.  
  14. def classify_image():
  15.     # Open file dialog to choose an image
  16.     file_path = filedialog.askopenfilename()
  17.  
  18.     # Load and preprocess the image
  19.     image = Image.open(file_path).convert("RGB")
  20.     size = (224, 224)
  21.     image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
  22.     image_array = np.asarray(image)
  23.     normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
  24.     data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
  25.     data[0] = normalized_image_array
  26.  
  27.     # Predict the class
  28.     prediction = model.predict(data)
  29.     index = np.argmax(prediction)
  30.     class_name = class_names[index]
  31.     confidence_score = prediction[0][index]
  32.  
  33.     # Display the result in the GUI
  34.     result_label.config(
  35.         text=f"Class: {class_name[2:]}\nConfidence Score: {confidence_score:.4f}",
  36.         background=bgc,
  37.         fg="white",
  38.         font=("Arial", 15, "bold italic")
  39.     )
  40.  
  41.     # Display the image in the GUI
  42.     img = ImageTk.PhotoImage(image)
  43.     image_label.config(image=img)
  44.     image_label.image = img
  45.  
  46. def clear_image():
  47.     # Set image_label to None to clear the displayed image
  48.     image_label.config(image=None)
  49.     image_label.image = None
  50.  
  51. # Create the GUI window
  52. pro = tk.Tk()
  53. pro.geometry("950x600+300+100") # width x heigth + left + top
  54. pro.resizable(True, True)
  55. pro.title("accessories Recognition")
  56. pro.config(background="black")
  57. pro.minsize(400, 400)
  58. pro.maxsize(2000, 2000)
  59.  
  60. lbl = tk.Label(
  61.     text="Accessories",
  62.     fg="white",
  63.     bg=bgc,
  64.     font=("Arial", 15, "bold italic"),
  65. )
  66. lbl.pack(side="top", pady=25)
  67.  
  68. # Create the browse button
  69. browse_button = tk.Button(pro, text="Upload Image", command=classify_image)
  70. browse_button.pack(side="bottom", pady=20)
  71. browse_button.configure(
  72.     background=bgc, foreground="white", font=("arial", 15, "bold")
  73. )
  74.  
  75. # Create the clear button
  76. clear_button = tk.Button(pro, text="Clear Image", command=clear_image)
  77. clear_button.pack(side="bottom", pady=20)
  78. clear_button.configure(
  79.     background=bgc, foreground="white", font=("arial", 15, "bold")
  80. )
  81.  
  82. # Create the label to display the result
  83. result_label = tk.Label(pro, text="")
  84. result_label.pack()
  85.  
  86. # Create the label to display the image
  87. image_label = tk.Label(pro)
  88. image_label.place(relx=0.5, rely=0.5, anchor="center")
  89.  
  90. # Start the GUI event loop
  91. pro.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement