
    h                     f    S SK Jr  S SK Jr  S SK Jr  S SKrS SKJr  S r	S
S jr
SS jrSS jrS	 rg)    )absolute_import)division)print_functionNc                 8   [         R                  " U 5      nUS   nUS   nUS   US   -   R                  [        R                  5      US'   US   R                  [        R                  5      US'   US   R                  [        R                  5      US'   US   R                  [        R                  5      US'   US   R                  [        R                  5      US'   US	   R
                  R                  S
S9R                  [        R                  5      S-
  US	'   US   R
                  R                  S
S9R                  [        R                  5      S-
  US'   [        R                  " US   5      S-
  R                  [        R                  5      US'   U$ )a  load BFM 3DMM model
Args:
    model_path: path to BFM model. 
Returns:
    model: (nver = 53215, ntri = 105840). nver: number of vertices. ntri: number of triangles.
        'shapeMU': [3*nver, 1]
        'shapePC': [3*nver, 199]
        'shapeEV': [199, 1]
        'expMU': [3*nver, 1]
        'expPC': [3*nver, 29]
        'expEV': [29, 1]
        'texMU': [3*nver, 1]
        'texPC': [3*nver, 199]
        'texEV': [199, 1]
        'tri': [ntri, 3] (start from 1, should sub 1 in python and c++)
        'tri_mouth': [114, 3] (start from 1, as a supplement to mouth triangles)
        'kpt_ind': [68,] (start from 1)
PS:
    You can change codes according to your own saved data.
    Just make sure the model has corresponding attributes.
modelr   r   shapeMUexpMUshapePCshapeEVexpEVexpPCtriCorder   	tri_mouthkpt_ind)	sioloadmatastypenpfloat32Tcopyint32squeeze)
model_pathr   r   s      l/var/www/fran/franai/venv/lib/python3.13/site-packages/insightface/thirdparty/face3d/morphable_model/load.pyload_BFMr!   	   sa   , 	JAgJE#JE i(5>9AA"**ME)Y'..rzz:E)Y'..rzz:E)7^**2::6E'N7^**2::6E'N <>>&&s&3::288DqHE%L{+--2232?FFrxxPSTTE+ 

5#34q8@@JE)L    c                 F    [         R                  " U 5      nUS   nUS   nU$ )aX  load 3DMM model extra information
Args:
    path: path to BFM info. 
Returns:  
    model_info:
        'symlist': 2 x 26720
        'symlist_tri': 2 x 52937
        'segbin': 4 x n (0: nose, 1: eye, 2: mouth, 3: cheek)
        'segbin_tri': 4 x ntri 
        'face_contour': 1 x 28
        'face_contour_line': 1 x 512
        'face_contour_front': 1 x 28
        'face_contour_front_line': 1 x 512
        'nose_hole': 1 x 142
        'nose_hole_right': 1 x 71
        'nose_hole_left': 1 x 71
        'parallel': 17 x 1 cell
        'parallel_face_contour': 28 x 1 cell
        'uv_coords': n x 2

model_infor   )r   r   )pathr   r$   s      r    load_BFM_infor&   4   s*    * 	DA<JCJr"   c                 V    [         R                  " U 5      nUS   R                  SS9nU$ )zdload uv coords of BFM
Args:
    path: path to data.
Returns:  
    uv_coords: [nver, 2]. range: 0-1
UVr   r   )r   r   r   )r%   r   	uv_coordss      r    load_uv_coordsr*   N   s,     	DA$S)Ir"   c                 P    [         R                  " U 5      nUS   R                  nU$ )zload pncc code of BFM
PNCC code: Defined in 'Face Alignment Across Large Poses: A 3D Solution Xiangyu'
download at http://www.cbsr.ia.ac.cn/users/xiangyuzhu/projects/3DDFA/main.htm.
Args:
    path: path to data.
Returns:  
    pncc_code: [nver, 3]
vertex_code)r   r   r   )r%   r   	pncc_codes      r    load_pncc_coder.   Y   s(     	DA- ""Ir"   c           
      ^   U S   R                  [        5      n[        R                  " USSS24   5      S   n[	        SUR
                  S   S-
  5       H7  n[        R                  " U[        R                  " XSS24   5      S   5      nM9     UR                  [        R                  5      $ )zget nose, eye, mouth index
    segbinr   Nr   )r   boolr   nonzerorangeshapeunion1dr   )r$   	valid_bin	organ_indis       r    get_organ_indr9   g   s     8$++D1I

9QqS>*1-I1iooa(1,-JJy"**Ys^*DQ*GH	 .BHH%%r"   )zBFM_info.mat)z
BFM_UV.mat)zpncc_code.mat)
__future__r   r   r   numpyr   scipy.ioior   r!   r&   r*   r.   r9    r"   r    <module>r?      s.    &  %  )V4	&r"   