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)