personal_color / modules /face_visualize.py
turnning2025's picture
1128_first_uploading
d562845 verified
Raw
History Blame Contribute Delete
1.18 kB
# face_visualize.py
import cv2
from pathlib import Path
from .face_mesh_utils import init_face_mesh
class FaceNotFoundError(Exception):
"""FaceMesh ๋žœ๋“œ๋งˆํฌ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์„ ๋•Œ ๋ฐœ์ƒ"""
pass
def visualize_facemesh(image_path, save_path=None):
"""FaceMesh ๋žœ๋“œ๋งˆํฌ๋ฅผ ์ด๋ฏธ์ง€์— ํ‘œ์‹œํ•˜๊ณ  ์ €์žฅ"""
img_path = Path(image_path)
save_path = Path(save_path) if save_path else img_path.parent / "face_mesh_result.jpg"
img = cv2.imread(str(img_path))
if img is None:
raise FileNotFoundError(f"์ด๋ฏธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ: {image_path}")
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
mesh = init_face_mesh()
result = mesh.process(img_rgb)
if not result.multi_face_landmarks:
raise FaceNotFoundError(f"FaceMesh ๋žœ๋“œ๋งˆํฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ: {image_path}")
landmarks = result.multi_face_landmarks[0]
h, w, _ = img.shape
for lm in landmarks.landmark:
x = int(lm.x * w)
y = int(lm.y * h)
cv2.circle(img, (x, y), 1, (0, 255, 0), -1)
cv2.imwrite(str(save_path), img)
print(f"FaceMesh ์‹œ๊ฐํ™” ์ด๋ฏธ์ง€ ์ €์žฅ๋จ โ†’ {save_path}")
return str(save_path)