From e8c23ad6f8378a87028165702d3b4d8b12f786b4 Mon Sep 17 00:00:00 2001 From: Franekskc Date: Sun, 26 May 2024 17:49:39 +0200 Subject: [PATCH 1/2] delete unnecessary drawing mediapipe landmarks function --- tools/image.py | 44 -------------------------------------------- 1 file changed, 44 deletions(-) diff --git a/tools/image.py b/tools/image.py index f390e23..c6e012f 100644 --- a/tools/image.py +++ b/tools/image.py @@ -87,47 +87,3 @@ def detect_landmarks(mp_image: mp.Image) -> FaceLandmarkerResult: face_landmarker_result = landmarker.detect(mp_image) return face_landmarker_result - - -def draw_landmarks_on_image(rgb_image, detection_result): - """ Draw landmarks on an image (mediapipe).""" - face_landmarks_list = detection_result.face_landmarks - - # Convert RGB image to BGR format - bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) - - # Loop through the detected faces to visualize. - for idx in range(len(face_landmarks_list)): - face_landmarks = face_landmarks_list[idx] - - # Draw the face landmarks. - face_landmarks_proto = landmark_pb2.NormalizedLandmarkList() - face_landmarks_proto.landmark.extend([ - landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in face_landmarks - ]) - - mp.solutions.drawing_utils.draw_landmarks( - image=bgr_image, - landmark_list=face_landmarks_proto, - connections=mp.solutions.face_mesh.FACEMESH_RIGHT_EYE, - landmark_drawing_spec=None, - connection_drawing_spec=mp.solutions.drawing_styles - .get_default_face_mesh_contours_style()) - - mp.solutions.drawing_utils.draw_landmarks( - image=bgr_image, - landmark_list=face_landmarks_proto, - connections=mp.solutions.face_mesh.FACEMESH_LEFT_EYE, - landmark_drawing_spec=None, - connection_drawing_spec=mp.solutions.drawing_styles - .get_default_face_mesh_contours_style()) - - mp.solutions.drawing_utils.draw_landmarks( - image=bgr_image, - landmark_list=face_landmarks_proto, - connections=mp.solutions.face_mesh.FACEMESH_LIPS, - landmark_drawing_spec=None, - connection_drawing_spec=mp.solutions.drawing_styles - .get_default_face_mesh_contours_style()) - - return bgr_image From dc196456210f0663e41b49afe965bfc19a4f0ab5 Mon Sep 17 00:00:00 2001 From: Franekskc Date: Sun, 26 May 2024 19:02:53 +0200 Subject: [PATCH 2/2] equalize histogram before detecting referenece --- tools/image.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/image.py b/tools/image.py index c6e012f..b268279 100644 --- a/tools/image.py +++ b/tools/image.py @@ -5,8 +5,9 @@ import mediapipe as mp import numpy as np -from mediapipe.framework.formats import landmark_pb2 from mediapipe.tasks.python.vision.face_landmarker import FaceLandmarkerResult +from matplotlib import pyplot as plt + def load_image(file_path: str) -> np.ndarray: @@ -42,9 +43,11 @@ def get_reference_position(img: np.ndarray) -> list[list[float]] | None: """ img_w, img_h, channels = img.shape gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + result_img = cv2.equalizeHist(gray) # equalize histogram - _, thresh = cv2.threshold(gray, 100, 255, 0) - # Find all countours + _, thresh = cv2.threshold(result_img, 100, 255, 0) + thresh = thresh.astype(np.uint8) + # Find all contours contours, _ = cv2.findContours(thresh, 1, 2) for countour in contours: