| import cv2 |
| import numpy as np |
| import base64 |
|
|
| def resize_image(image: np.ndarray, target_width=320, target_height=240) -> np.ndarray: |
| """ |
| Resize image to target dimensions while maintaining aspect ratio or not. |
| For performance, we might just resize to fixed size or max dimension. |
| The requirement says 'Resize images to 320x240 before processing'. |
| """ |
| return cv2.resize(image, (target_width, target_height)) |
|
|
| def decode_base64_image(base64_string: str) -> np.ndarray: |
| if "," in base64_string: |
| base64_string = base64_string.split(",")[1] |
| image_bytes = base64.b64decode(base64_string) |
| nparr = np.frombuffer(image_bytes, np.uint8) |
| return cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
|
|
| def align_face(img, landmarks): |
| """ |
| Align face using 5-point landmarks. |
| landmarks: list of [x, y] or np array of shape (5, 2) |
| Order: Left Eye, Right Eye, Nose, Left Mouth, Right Mouth |
| """ |
| |
| src = np.array([ |
| [30.2946, 51.6963], |
| [65.5318, 51.5014], |
| [48.0252, 71.7366], |
| [33.5493, 92.3655], |
| [62.7299, 92.2041] ], dtype=np.float32 ) |
|
|
| if landmarks.shape[0] == 6: |
| |
| |
| |
| |
| pass |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| dst = landmarks.astype(np.float32) |
| tform = cv2.estimateAffinePartial2D(dst, src, method=cv2.LMEDS)[0] |
| warped = cv2.warpAffine(img, tform, (112, 112)) |
| return warped |
|
|
| def compute_cosine_similarity(embed1, embed2): |
| |
| e1 = np.array(embed1).flatten() |
| e2 = np.array(embed2).flatten() |
| |
| norm1 = np.linalg.norm(e1) |
| norm2 = np.linalg.norm(e2) |
| |
| if norm1 == 0 or norm2 == 0: |
| return 0.0 |
| |
| return np.dot(e1, e2) / (norm1 * norm2) |
|
|