U
    ‰±Ëhµ  ã                   @   sj   d dl mZ d dl mZ d dl mZ d dlZd dlmZ dd„ Z	ddd	„Z
ddd„Zddd„Zdd„ ZdS )é    )Úabsolute_import)Údivision)Úprint_functionNc                 C   sì   t  | ¡}|d }|d }|d |d   tj¡|d< |d  tj¡|d< |d  tj¡|d< |d  tj¡|d< |d  tj¡|d< |d	 jjd
d tj¡d |d	< |d jjd
d tj¡d |d< t |d ¡d  tj¡|d< |S )a8   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   ZshapeMUZexpMUZshapePCZshapeEVZexpEVZexpPCZtriÚC©Úorderé   Z	tri_mouthZkpt_ind)	ÚsioÚloadmatÚastypeÚnpZfloat32ÚTÚcopyÚint32Zsqueeze)Z
model_pathr   r   © r   úV/tmp/pip-unpacked-wheel-5oclok7i/insightface/thirdparty/face3d/morphable_model/load.pyÚload_BFM	   s    
""r   úBFM_info.matc                 C   s   t  | ¡}|d }|d }|S )a¥   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   r   r   r   Úload_BFM_info4   s    
r   ú
BFM_UV.matc                 C   s   t  | ¡}|d jdd}|S )zy load uv coords of BFM
    Args:
        path: path to data.
    Returns:  
        uv_coords: [nver, 2]. range: 0-1
    ZUVr   r   )r   r   r   )r   r   Z	uv_coordsr   r   r   Úload_uv_coordsN   s    
r   úpncc_code.matc                 C   s   t  | ¡}|d j}|S )a   load 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]
    Zvertex_code)r   r   r   )r   r   Z	pncc_coder   r   r   Úload_pncc_codeY   s    	

r   c              
   C   sp   | d   t¡}t |ddd…f ¡d }td|jd d ƒD ]&}t |t ||dd…f ¡d ¡}q<|  tj¡S )z  get nose, eye, mouth index
    Zsegbinr   Nr
   )r   Úboolr   ZnonzeroÚrangeÚshapeZunion1dr   )r   Z	valid_binZ	organ_indÚir   r   r   Úget_organ_indg   s
    $r!   )r   )r   )r   )Ú
__future__r   r   r   Znumpyr   Zscipy.ioÚior   r   r   r   r   r!   r   r   r   r   Ú<module>   s   +


