Spaces:
Sleeping
Sleeping
File size: 1,184 Bytes
d562845 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | # 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)
|