Siyun He commited on
Commit
acd6b6a
·
1 Parent(s): 9eb8f76

update face detection method

Browse files
Files changed (2) hide show
  1. app.py +14 -14
  2. requirements.txt +1 -1
app.py CHANGED
@@ -3,16 +3,16 @@ import cvzone
3
  import numpy as np
4
  import os
5
  import gradio as gr
6
- import dlib
7
  from datetime import datetime
8
 
9
  # Load the YuNet model
10
  model_path = 'face_detection_yunet_2023mar.onnx'
11
  face_detector = cv2.FaceDetectorYN.create(model_path, "", (320, 320))
12
 
13
- # Load dlib's shape predictor
14
- shape_predictor_path = 'shape_predictor_68_face_landmarks.dat'
15
- shape_predictor = dlib.shape_predictor(shape_predictor_path)
16
 
17
  # Initialize the glass number
18
  num = 1
@@ -58,19 +58,17 @@ def process_frame(frame):
58
  face_detector.setInputSize((width, height))
59
  _, faces = face_detector.detect(frame)
60
 
 
 
 
61
  face_shape = "Unknown"
62
- if faces is not None:
63
  for face in faces:
64
  x, y, w, h = face[:4].astype(int)
65
  face_landmarks = face[4:14].reshape(5, 2).astype(int)
66
 
67
- # Convert to dlib rectangle
68
- dlib_rect = dlib.rectangle(x, y, x + w, y + h)
69
- landmarks = shape_predictor(frame, dlib_rect)
70
- landmarks = np.array([(p.x, p.y) for p in landmarks.parts()])
71
-
72
  # Determine face shape
73
- face_shape = determine_face_shape(landmarks)
74
 
75
  # Get the nose position
76
  nose_x, nose_y = face_landmarks[2].astype(int)
@@ -102,6 +100,11 @@ def process_frame(frame):
102
  except Exception as e:
103
  print(f"Error overlaying glasses: {e}")
104
 
 
 
 
 
 
105
  return frame, face_shape
106
 
107
  # Transform function
@@ -135,9 +138,6 @@ def transform_cv2(frame, transform):
135
  return frame
136
 
137
  def refresh_interface():
138
- # # Reset the transformation dropdown to its default value
139
- # transform.update(value="none")
140
-
141
  # Reset the image to an empty state or a default image
142
  input_img.update(value=None)
143
 
 
3
  import numpy as np
4
  import os
5
  import gradio as gr
6
+ import mediapipe as mp
7
  from datetime import datetime
8
 
9
  # Load the YuNet model
10
  model_path = 'face_detection_yunet_2023mar.onnx'
11
  face_detector = cv2.FaceDetectorYN.create(model_path, "", (320, 320))
12
 
13
+ # Initialize MediaPipe Face Mesh
14
+ mp_face_mesh = mp.solutions.face_mesh
15
+ face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.5)
16
 
17
  # Initialize the glass number
18
  num = 1
 
58
  face_detector.setInputSize((width, height))
59
  _, faces = face_detector.detect(frame)
60
 
61
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
62
+ results = face_mesh.process(frame_rgb)
63
+
64
  face_shape = "Unknown"
65
+ if faces is not None and results.multi_face_landmarks:
66
  for face in faces:
67
  x, y, w, h = face[:4].astype(int)
68
  face_landmarks = face[4:14].reshape(5, 2).astype(int)
69
 
 
 
 
 
 
70
  # Determine face shape
71
+ # face_shape = determine_face_shape(landmarks)
72
 
73
  # Get the nose position
74
  nose_x, nose_y = face_landmarks[2].astype(int)
 
100
  except Exception as e:
101
  print(f"Error overlaying glasses: {e}")
102
 
103
+ for face_landmarks_mp in results.multi_face_landmarks:
104
+ landmarks = np.array([(lm.x * frame.shape[1], lm.y * frame.shape[0]) for lm in face_landmarks_mp.landmark])
105
+ # Determine face shape
106
+ face_shape = determine_face_shape(landmarks)
107
+
108
  return frame, face_shape
109
 
110
  # Transform function
 
138
  return frame
139
 
140
  def refresh_interface():
 
 
 
141
  # Reset the image to an empty state or a default image
142
  input_img.update(value=None)
143
 
requirements.txt CHANGED
@@ -2,4 +2,4 @@ gradio
2
  cvzone
3
  opencv-python
4
  numpy
5
- dlib
 
2
  cvzone
3
  opencv-python
4
  numpy
5
+ mediapipe>=0.10.11,<0.11