You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python图像水平翻转及面部关键点目标重排技术咨询

Handling Facial Landmark Reordering After Horizontal Flip

Hey there! Let's walk through exactly how to reorder facial landmarks after horizontal flipping—this is a super common step in data augmentation for facial keypoint detection, so I’ve got you covered.

Core Idea First

When you flip an image horizontally:

  • The y-coordinate of every landmark stays the same (since we’re only flipping left-right, not up-down).
  • The x-coordinate needs to be mirrored across the image’s vertical center. For an image with width image_width (in pixels), the new x-value becomes: new_x = image_width - original_x - 1 (remember, pixel coordinates start at 0, so we subtract 1 to avoid off-by-one errors).
  • Any paired left/right landmarks (like left eye ↔ right eye) need to swap places after calculating their mirrored x-values.

Step-by-Step Implementation

Let’s use Python with OpenCV (a common tool for this) as an example—adjustments for PIL/Pillow or other libraries will be straightforward.

1. Map Out Your Landmark Pairs

First, list all the left-right landmark pairs from your dataset. This ensures you don’t miss any swaps:

# Define which landmarks need to be swapped
swap_pairs = [
    ("left_eye_center", "right_eye_center"),
    ("left_eye_inner_corner", "right_eye_inner_corner"),
    ("left_eye_outer_corner", "right_eye_outer_corner"),
    ("left_eyebrow_inner_end", "right_eyebrow_inner_end"),
    ("left_eyebrow_outer_end", "right_eyebrow_outer_end"),
    ("mouth_left_corner", "mouth_right_corner"),
    # Add any other paired landmarks from your dataset here
]

2. Flip the Image and Adjust Landmarks

Assume you’re storing landmarks in a dictionary (easy to map by name) and have loaded your image:

import cv2

# Load your image and original landmarks
image = cv2.imread("your_face_image.jpg")
original_landmarks = {
    "left_eye_center": (120, 160),
    "right_eye_center": (220, 160),
    "nose_tip": (170, 220),
    # Add all your other landmarks here
}

# Flip the image horizontally
flipped_image = cv2.flip(image, 1)
image_width = image.shape[1]  # Get the image's width in pixels

# Create a dictionary to hold flipped landmarks
flipped_landmarks = {}

# Handle paired landmarks (swap and mirror)
for left_key, right_key in swap_pairs:
    # Get original coordinates
    left_x, left_y = original_landmarks[left_key]
    right_x, right_y = original_landmarks[right_key]
    
    # Calculate mirrored x-values
    flipped_left_x = image_width - left_x - 1
    flipped_right_x = image_width - right_x - 1
    
    # Swap the landmarks and store them
    flipped_landmarks[right_key] = (flipped_left_x, left_y)
    flipped_landmarks[left_key] = (flipped_right_x, right_y)

# Handle unpaired landmarks (like nose tip, chin—just mirror x)
unpaired_keys = [
    key for key in original_landmarks 
    if key not in [k for pair in swap_pairs for k in pair]
]
for key in unpaired_keys:
    x, y = original_landmarks[key]
    flipped_x = image_width - x - 1
    flipped_landmarks[key] = (flipped_x, y)

3. Double-Check the Result

It’s always a good idea to visualize the flipped landmarks to make sure they’re correct:

# Draw circles on the flipped image for each landmark
for name, (x, y) in flipped_landmarks.items():
    cv2.circle(flipped_image, (x, y), 3, (0, 255, 0), -1)  # Green circles

# Show the result
cv2.imshow("Flipped Image with Landmarks", flipped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Quick Tips

  • If your landmarks are stored in a NumPy array (instead of a dictionary), use index pairs instead of name pairs. For example, if index 0 is left eye center and index 1 is right eye center, define swap_indices = [(0,1), (2,3), ...] and adjust the code to work with array indices.
  • For PIL/Pillow users, flipping is done with image.transpose(PIL.Image.FLIP_LEFT_RIGHT), and the x-coordinate formula stays exactly the same.
  • Always double-check your swap pairs—missing even one pair will lead to bad augmented data that hurts your model’s performance.

内容的提问来源于stack exchange,提问作者Karl Alexius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:42:26