|
| 1 | +from tkinter import * |
| 2 | +import customtkinter |
| 3 | +from tkinter import filedialog |
| 4 | +from PIL import Image, ImageTk |
| 5 | +from face_qa.face_qa import FaceQA |
| 6 | + |
| 7 | +customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light" |
| 8 | +customtkinter.set_default_color_theme("dark-blue") # Themes: "blue" (standard), "green", "dark-blue" |
| 9 | + |
| 10 | +class InterfaceDemo(customtkinter.CTk): |
| 11 | + |
| 12 | + WIDTH = 600 |
| 13 | + HEIGHT = 600 |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + super().__init__() |
| 17 | + |
| 18 | + self.title("InterfaceDemo") |
| 19 | + self.geometry(f"{InterfaceDemo.WIDTH}x{InterfaceDemo.HEIGHT}") |
| 20 | + self.protocol("WM_DELETE_WINDOW", self.on_closing) # call .on_closing() when app gets closed |
| 21 | + |
| 22 | + |
| 23 | + self.frame_menu = customtkinter.CTkFrame(master=self, |
| 24 | + width=180, |
| 25 | + corner_radius=0) |
| 26 | + self.frame_menu.pack() |
| 27 | + |
| 28 | + self.frame_initial = customtkinter.CTkFrame(master=self) |
| 29 | + self.frame_initial.pack() |
| 30 | + |
| 31 | + # ============ frame_menu ============ |
| 32 | + self.frame_menu.pack() |
| 33 | + |
| 34 | + self.label_1 = customtkinter.CTkLabel(master=self.frame_menu, |
| 35 | + text="InterfaceDemo", |
| 36 | + ) |
| 37 | + self.label_1.pack() |
| 38 | + |
| 39 | + # ============.frame_initial ============ |
| 40 | + self.frame_initial.rowconfigure((0, 1, 2, 3), weight=1) |
| 41 | + self.frame_initial.rowconfigure(7, weight=10) |
| 42 | + self.frame_initial.columnconfigure((0, 1), weight=1) |
| 43 | + self.frame_initial.columnconfigure(2, weight=0) |
| 44 | + |
| 45 | + # ============ frame_info ========= |
| 46 | + self.frame_info = customtkinter.CTkFrame(master=self) |
| 47 | + self.frame_info.pack(side=TOP) |
| 48 | + |
| 49 | + self.button_1 = customtkinter.CTkButton(master=self.frame_info, |
| 50 | + text="Select a Image", |
| 51 | + command=self.load_image |
| 52 | + ) |
| 53 | + self.button_1.pack() |
| 54 | + |
| 55 | + self.image = None |
| 56 | + |
| 57 | + def change_appearance_mode(self, new_appearance_mode): |
| 58 | + customtkinter.set_appearance_mode(new_appearance_mode) |
| 59 | + |
| 60 | + def load_image(self): |
| 61 | + file_path = filedialog.askopenfilename(title='Select a image') |
| 62 | + image = Image.open(file_path) |
| 63 | + |
| 64 | + self.image_check(file_path) |
| 65 | + |
| 66 | + # Remove image |
| 67 | + if self.image: |
| 68 | + self.image_label.configure(image=None) |
| 69 | + self.image_label.image = None |
| 70 | + |
| 71 | + # Resize image |
| 72 | + if image.width > 200 or image.height > 200: |
| 73 | + ratio = min(200/image.width, 200/image.height) |
| 74 | + image = image.resize((int(image.width * ratio), int(image.height * ratio)), Image.ANTIALIAS) |
| 75 | + |
| 76 | + if hasattr(self, "image_label"): |
| 77 | + self.image_label.pack_forget() |
| 78 | + self.image_label.destroy() |
| 79 | + |
| 80 | + photo = ImageTk.PhotoImage(image) |
| 81 | + self.image_label = Label(self.frame_initial, image=photo) |
| 82 | + self.image_label.image = photo |
| 83 | + self.image_label.pack() |
| 84 | + |
| 85 | + self.image = image |
| 86 | + |
| 87 | + def image_check(self, file_path): |
| 88 | + validator = FaceQA(file_path, 2) |
| 89 | + result = validator.check_face() |
| 90 | + |
| 91 | + if hasattr(self, "label_face"): |
| 92 | + self.label_face.pack_forget() |
| 93 | + self.label_face.destroy() |
| 94 | + if hasattr(self, "label_eyes"): |
| 95 | + self.label_eyes.pack_forget() |
| 96 | + self.label_eyes.destroy() |
| 97 | + if hasattr(self, "is_smiling"): |
| 98 | + self.is_smiling.pack_forget() |
| 99 | + self.is_smiling.destroy() |
| 100 | + if hasattr(self, "contrast_is_good"): |
| 101 | + self.contrast_is_good.pack_forget() |
| 102 | + self.contrast_is_good.destroy() |
| 103 | + if hasattr(self, "brightness_is_good"): |
| 104 | + self.brightness_is_good.pack_forget() |
| 105 | + self.brightness_is_good.destroy() |
| 106 | + if hasattr(self, "face_is_centralized"): |
| 107 | + self.face_is_centralized.pack_forget() |
| 108 | + self.face_is_centralized.destroy() |
| 109 | + if hasattr(self, "more_than_one_face"): |
| 110 | + self.more_than_one_face.pack_forget() |
| 111 | + self.more_than_one_face.destroy() |
| 112 | + |
| 113 | + if not result["face_detected"]: |
| 114 | + self.label_face = customtkinter.CTkLabel(master=self.frame_info, |
| 115 | + text="Face was not detected correctly.", |
| 116 | + font=("Helvetica", 22), |
| 117 | + text_color="red") |
| 118 | + self.label_face.pack() |
| 119 | + else: |
| 120 | + self.label_face = customtkinter.CTkLabel(master=self.frame_info, |
| 121 | + text="Face detected.", |
| 122 | + font=("Helvetica", 22), |
| 123 | + text_color="green") |
| 124 | + self.label_face.pack() |
| 125 | + if result["more_than_one_face"]: |
| 126 | + self.more_than_one_face = customtkinter.CTkLabel(master=self.frame_info, |
| 127 | + text="More than one face in image", |
| 128 | + font=("Helvetica", 22), |
| 129 | + text_color="red") |
| 130 | + self.more_than_one_face.pack() |
| 131 | + else: |
| 132 | + self.more_than_one_face = customtkinter.CTkLabel(master=self.frame_info, |
| 133 | + text="Only one face detected", |
| 134 | + font=("Helvetica", 22), |
| 135 | + text_color="green") |
| 136 | + self.more_than_one_face.pack() |
| 137 | + if result["eyes_is_good"]: |
| 138 | + self.label_eyes = customtkinter.CTkLabel(master=self.frame_info, |
| 139 | + text="Eyes are clear and visible", |
| 140 | + font=("Helvetica", 22), |
| 141 | + text_color="green") |
| 142 | + self.label_eyes.pack() |
| 143 | + else: |
| 144 | + self.label_eyes = customtkinter.CTkLabel(master=self.frame_info, |
| 145 | + text="Eyes are closed or not visible", |
| 146 | + font=("Helvetica", 22), |
| 147 | + text_color="red") |
| 148 | + self.label_eyes.pack() |
| 149 | + if result["is_smiling"]: |
| 150 | + self.is_smiling = customtkinter.CTkLabel(master=self.frame_info, |
| 151 | + text="Smile detected", |
| 152 | + font=("Helvetica", 22), |
| 153 | + text_color="red") |
| 154 | + self.is_smiling.pack() |
| 155 | + else: |
| 156 | + self.is_smiling = customtkinter.CTkLabel(master=self.frame_info, |
| 157 | + text="Smile not detected", |
| 158 | + font=("Helvetica", 22), |
| 159 | + text_color="green") |
| 160 | + self.is_smiling.pack() |
| 161 | + if result["contrast_is_good"]: |
| 162 | + self.contrast_is_good = customtkinter.CTkLabel(master=self.frame_info, |
| 163 | + text="Image contrast is good", |
| 164 | + font=("Helvetica", 22), |
| 165 | + text_color="green") |
| 166 | + self.contrast_is_good.pack() |
| 167 | + else: |
| 168 | + self.contrast_is_good = customtkinter.CTkLabel(master=self.frame_info, |
| 169 | + text="Image contrast isn't good", |
| 170 | + font=("Helvetica", 22), |
| 171 | + text_color="red") |
| 172 | + self.contrast_is_good.pack() |
| 173 | + if result["brightness_is_good"]: |
| 174 | + self.brightness_is_good = customtkinter.CTkLabel(master=self.frame_info, |
| 175 | + text="Image brightness is good", |
| 176 | + font=("Helvetica", 22), |
| 177 | + text_color="green") |
| 178 | + self.brightness_is_good.pack() |
| 179 | + else: |
| 180 | + self.brightness_is_good = customtkinter.CTkLabel(master=self.frame_info, |
| 181 | + text="Image brightness isn't good", |
| 182 | + font=("Helvetica", 22), |
| 183 | + text_color="red") |
| 184 | + self.brightness_is_good.pack() |
| 185 | + if result["face_is_centralized"]: |
| 186 | + self.face_is_centralized = customtkinter.CTkLabel(master=self.frame_info, |
| 187 | + text="Face is centralized", |
| 188 | + font=("Helvetica", 22), |
| 189 | + text_color="green") |
| 190 | + self.face_is_centralized.pack() |
| 191 | + else: |
| 192 | + self.face_is_centralized = customtkinter.CTkLabel(master=self.frame_info, |
| 193 | + text="Face isn't centralized", |
| 194 | + font=("Helvetica", 22), |
| 195 | + text_color="red") |
| 196 | + self.face_is_centralized.pack() |
| 197 | + |
| 198 | + def on_closing(self, event=0): |
| 199 | + self.destroy() |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + app = InterfaceDemo() |
| 203 | + app.mainloop() |
0 commit comments