ukzada commited on
Commit
5410cc3
·
verified ·
1 Parent(s): 8e1a54f

Update landmarks.py

Browse files
Files changed (1) hide show
  1. landmarks.py +4 -13
landmarks.py CHANGED
@@ -1,7 +1,7 @@
1
  import cv2
2
  import numpy as np
3
  from typing import List, Iterable, Optional
4
- from mediapipe import solutions as mp_solutions # Updated import
5
 
6
 
7
  def detect_landmarks(src: np.ndarray, is_stream: bool = False) -> Optional[List]:
@@ -9,27 +9,21 @@ def detect_landmarks(src: np.ndarray, is_stream: bool = False) -> Optional[List]
9
  Given an image `src`, retrieves the facial landmarks associated with it.
10
  Works with Mediapipe 0.10+.
11
  """
12
- # Initialize FaceMesh
13
- with mp_solutions.face_mesh.FaceMesh(
14
  static_image_mode=not is_stream,
15
  max_num_faces=1,
16
- refine_landmarks=True, # optional, provides iris landmarks if needed
17
  min_detection_confidence=0.5,
18
  min_tracking_confidence=0.5
19
  ) as fm:
20
- # Convert BGR to RGB as Mediapipe expects RGB
21
  results = fm.process(cv2.cvtColor(src, cv2.COLOR_BGR2RGB))
22
 
23
- # Return the landmarks if found
24
  if results.multi_face_landmarks:
25
  return results.multi_face_landmarks[0].landmark
26
  return None
27
 
28
 
29
  def normalize_landmarks(landmarks, height: int, width: int, mask: Iterable = None) -> np.ndarray:
30
- """
31
- Converts normalized Mediapipe landmark coordinates to pixel coordinates.
32
- """
33
  normalized_landmarks = np.array([
34
  (int(landmark.x * width), int(landmark.y * height)) for landmark in landmarks
35
  ])
@@ -39,12 +33,9 @@ def normalize_landmarks(landmarks, height: int, width: int, mask: Iterable = Non
39
 
40
 
41
  def plot_landmarks(src: np.ndarray, landmarks: List, show: bool = False) -> np.ndarray:
42
- """
43
- Given a source image and a list of landmarks, plots them onto the image.
44
- """
45
  dst = src.copy()
46
  for x, y in landmarks:
47
- cv2.circle(dst, (x, y), 2, (0, 255, 0), cv2.FILLED) # green dots for visibility
48
  if show:
49
  print("Displaying image plotted with landmarks")
50
  cv2.imshow("Plotted Landmarks", dst)
 
1
  import cv2
2
  import numpy as np
3
  from typing import List, Iterable, Optional
4
+ from mediapipe.python.solutions.face_mesh import FaceMesh # Correct import
5
 
6
 
7
  def detect_landmarks(src: np.ndarray, is_stream: bool = False) -> Optional[List]:
 
9
  Given an image `src`, retrieves the facial landmarks associated with it.
10
  Works with Mediapipe 0.10+.
11
  """
12
+ with FaceMesh(
 
13
  static_image_mode=not is_stream,
14
  max_num_faces=1,
15
+ refine_landmarks=True,
16
  min_detection_confidence=0.5,
17
  min_tracking_confidence=0.5
18
  ) as fm:
 
19
  results = fm.process(cv2.cvtColor(src, cv2.COLOR_BGR2RGB))
20
 
 
21
  if results.multi_face_landmarks:
22
  return results.multi_face_landmarks[0].landmark
23
  return None
24
 
25
 
26
  def normalize_landmarks(landmarks, height: int, width: int, mask: Iterable = None) -> np.ndarray:
 
 
 
27
  normalized_landmarks = np.array([
28
  (int(landmark.x * width), int(landmark.y * height)) for landmark in landmarks
29
  ])
 
33
 
34
 
35
  def plot_landmarks(src: np.ndarray, landmarks: List, show: bool = False) -> np.ndarray:
 
 
 
36
  dst = src.copy()
37
  for x, y in landmarks:
38
+ cv2.circle(dst, (x, y), 2, (0, 255, 0), cv2.FILLED)
39
  if show:
40
  print("Displaying image plotted with landmarks")
41
  cv2.imshow("Plotted Landmarks", dst)