Siyun He commited on
Commit
22360ca
·
1 Parent(s): 74def1e
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -36,46 +36,47 @@ def process_frame(frame):
36
  global overlay
37
  height, width = frame.shape[:2]
38
  face_detector.setInputSize((width, height))
 
 
39
  _, faces = face_detector.detect(frame)
40
-
41
  if faces is not None:
42
  for face in faces:
 
43
  x, y, w, h = face[:4].astype(int)
44
- face_landmarks = face[4:14].reshape(5, 2).astype(int) # Facial landmarks
45
 
46
- # Get the nose position
47
- nose_x, nose_y = face_landmarks[2].astype(int)
48
- # left eye postion
49
- left_eye_x, left_eye_y = face_landmarks[0].astype(int)
50
- # right eye postion
51
- right_eye_x, right_eye_y = face_landmarks[1].astype(int)
52
 
53
- # Calculate the midpoint between the eyes
54
  eye_center_x = (left_eye_x + right_eye_x) // 2
55
  eye_center_y = (left_eye_y + right_eye_y) // 2
56
-
57
- # Calculate the angle of rotation
58
  delta_x = right_eye_x - left_eye_x
59
  delta_y = right_eye_y - left_eye_y
60
  angle = np.degrees(np.arctan2(delta_y, delta_x))
61
 
62
- # Resize the overlay
63
  overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
64
-
65
- # Rotate the overlay
66
  overlay_center = (overlay_resize.shape[1] // 2, overlay_resize.shape[0] // 2)
67
  rotation_matrix = cv2.getRotationMatrix2D(overlay_center, angle, 1.0)
68
- overlay_rotated = cv2.warpAffine(overlay_resize, rotation_matrix, (overlay_resize.shape[1], overlay_resize.shape[0]), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0))
 
 
 
 
69
 
70
- # Calculate the position to center the glasses on the eyes
71
- overlay_x = eye_center_x - overlay_rotated.shape[1] // 2
72
- overlay_y = eye_center_y - overlay_rotated.shape[0] // 2 # Adjust vertical position
73
 
74
- # Overlay the glasses
75
  try:
76
  frame = cvzone.overlayPNG(frame, overlay_rotated, [overlay_x, overlay_y])
77
  except Exception as e:
78
  print(f"Error overlaying glasses: {e}")
 
79
  return frame
80
 
81
 
 
36
  global overlay
37
  height, width = frame.shape[:2]
38
  face_detector.setInputSize((width, height))
39
+
40
+ # Detect faces
41
  _, faces = face_detector.detect(frame)
 
42
  if faces is not None:
43
  for face in faces:
44
+ # Extract bounding box and facial landmarks
45
  x, y, w, h = face[:4].astype(int)
46
+ face_landmarks = face[4:14].reshape(5, 2).astype(int) # Adjust if needed
47
 
48
+ # Get the nose and eye positions
49
+ nose_x, nose_y = face_landmarks[2]
50
+ left_eye_x, left_eye_y = face_landmarks[0]
51
+ right_eye_x, right_eye_y = face_landmarks[1]
 
 
52
 
53
+ # Calculate eye center and rotation angle
54
  eye_center_x = (left_eye_x + right_eye_x) // 2
55
  eye_center_y = (left_eye_y + right_eye_y) // 2
 
 
56
  delta_x = right_eye_x - left_eye_x
57
  delta_y = right_eye_y - left_eye_y
58
  angle = np.degrees(np.arctan2(delta_y, delta_x))
59
 
60
+ # Resize and rotate the overlay
61
  overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
 
 
62
  overlay_center = (overlay_resize.shape[1] // 2, overlay_resize.shape[0] // 2)
63
  rotation_matrix = cv2.getRotationMatrix2D(overlay_center, angle, 1.0)
64
+ overlay_rotated = cv2.warpAffine(
65
+ overlay_resize, rotation_matrix,
66
+ (overlay_resize.shape[1], overlay_resize.shape[0]),
67
+ flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)
68
+ )
69
 
70
+ # Calculate position for overlay
71
+ overlay_x = max(0, eye_center_x - overlay_rotated.shape[1] // 2)
72
+ overlay_y = max(0, eye_center_y - overlay_rotated.shape[0] // 2)
73
 
74
+ # Overlay the glasses using cvzone
75
  try:
76
  frame = cvzone.overlayPNG(frame, overlay_rotated, [overlay_x, overlay_y])
77
  except Exception as e:
78
  print(f"Error overlaying glasses: {e}")
79
+
80
  return frame
81
 
82