Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference validation update #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 6 additions & 47 deletions tools/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -87,47 +90,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