diff --git a/GUI.py b/GUI.py index f7bf6c9..9554b72 100644 --- a/GUI.py +++ b/GUI.py @@ -111,9 +111,7 @@ def update(self): words.append((x, y, w, h, text)) if words: sentences = group_words_to_sentences(words) - ss = "" - for sentence in sentences: - ss += sentence + "\n" + ss = "".join(sentence + "\n" for sentence in sentences) if prev_ss != ss: self.text_label.delete(1.0, tk.END) self.text_label.insert(tk.END, ss) @@ -165,16 +163,14 @@ def save_face(self): if name == "": tk.messagebox.showerror("Error", "Please enter a name.") return - filename = os.path.join("./known_faces/", name + ".jpg") + filename = os.path.join("./known_faces/", f"{name}.jpg") if os.path.exists(filename): return tk.messagebox.showerror( "Error", "A face with this name already exists." ) - # Show a pop-up message - resp = tk.messagebox.askokcancel( + if resp := tk.messagebox.askokcancel( "Cheese", "Smile for the camera! Click OK to click and image." - ) - if resp: + ): ret, frame = self.cap.read() if ret: cv2.imwrite(filename, frame) diff --git a/detectors/face.py b/detectors/face.py index 0b3d425..da59435 100644 --- a/detectors/face.py +++ b/detectors/face.py @@ -10,10 +10,10 @@ faces_folder_path = "./known_faces/" dlib_frontal_face_detector = dlib.get_frontal_face_detector() shape_predictor = dlib.shape_predictor( - data_dir + "/shape_predictor_68_face_landmarks.dat" + f"{data_dir}/shape_predictor_68_face_landmarks.dat" ) face_recognition_model = dlib.face_recognition_model_v1( - data_dir + "/dlib_face_recognition_resnet_model_v1.dat" + f"{data_dir}/dlib_face_recognition_resnet_model_v1.dat" ) face_detector = dlib.get_frontal_face_detector() @@ -40,7 +40,7 @@ def find_match(known_faces, person_names, face): if min_value < 0.58: return person_names[min_index] + " ({0:.2f})".format(min_value) if min_value < 0.65: - return person_names[min_index] + "?" + " ({0:.2f})".format(min_value) + return f"{person_names[min_index]}?" + " ({0:.2f})".format(min_value) return "Not Found" @@ -77,7 +77,7 @@ def load_face_encodings(faces_folder_path): def recognize_faces_in_frame(frame): faceClassifier = cv2.CascadeClassifier( - data_dir + "/haarcascade_frontalface_default.xml" + f"{data_dir}/haarcascade_frontalface_default.xml" ) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) face_rects = faceClassifier.detectMultiScale( @@ -89,8 +89,7 @@ def recognize_faces_in_frame(frame): ) for x, y, w, h in face_rects: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) - face_encodings_in_image = get_face_encodings(frame) - if face_encodings_in_image: + if face_encodings_in_image := get_face_encodings(frame): match = find_match(face_encodings, person_names, face_encodings_in_image[0]) cv2.putText( frame, diff --git a/detectors/traffic.py b/detectors/traffic.py index e608aa3..5de78ff 100644 --- a/detectors/traffic.py +++ b/detectors/traffic.py @@ -52,10 +52,9 @@ async def image_processing(img): model = load_model("./models/TSR.h5") - data = [] image = Image.fromarray(img) image = image.resize((30, 30)) - data.append(np.array(image)) + data = [np.array(image)] X_test = np.array(data) Y_pred = np.argmax(model.predict(X_test)) try: diff --git a/detectors/utils.py b/detectors/utils.py index 60f82ec..188b899 100644 --- a/detectors/utils.py +++ b/detectors/utils.py @@ -50,9 +50,7 @@ def group_words_to_sentences(words): # Convert the words to strings sentences = [[w[4] for w in s] for s in sentences] - sentences = [" ".join(s) for s in sentences] - - return sentences + return [" ".join(s) for s in sentences] def wrap(func):