Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
vardanagarwal
GitHub Repository: vardanagarwal/Proctoring-AI
Path: blob/master/__pycache__/face_landmarks.cpython-37.pyc
454 views
B

e>#_��@sXdZddlZddlZddlZddlmZddd�Zdd�Zd	d
�Z	dd�Z
ddd�ZdS)z2
Created on Wed Jul 29 19:47:08 2020

@author: hp
�N)�keras�models/pose_modelcCstj�|�}|S)aX
    Get the facial landmark model. 
    Original repository: https://github.com/yinguobing/cnn-facial-landmark

    Parameters
    ----------
    saved_model : string, optional
        Path to facial landmarks model. The default is 'models/pose_model'.

    Returns
    -------
    model : Tensorflow model
        Facial landmarks model

    )r�models�
load_model)�saved_model�model�r�7C:\Users\hp\Desktop\minor project\new\face_landmarks.py�get_landmark_modelsr
c	Cs�|d}|d}|d}|d}||}||}||}tt|�d�}|dkrT|S|dkr�||8}||7}|ddkr�|d7}n$||8}||7}|ddkr�|d7}||||ks�td��||||gS)z7Get a square box out of the given box, by expanding it.r���zBox is not square.)�int�abs�AssertionError)	�box�left_x�top_y�right_x�bottom_y�	box_width�
box_height�diff�deltarrr	�get_square_box!s*
rcCsL|d|d}|d|d}|d|d}|d|d}||||gS)z4Move the box to direction specified by vector offsetrrrr
r)r�offsetrrrrrrr	�move_boxAs
rc	Cstt|d|dd��}t|d|g�}t|�}||d|d�|d|d�f}t�|d�}t�|tj�}|jdt	j
|gt	jd��}t�
|d	���d
d�}t�|d�}||d|d9}|d
d
�df|d7<|d
d
�df|d7<|�tj�}|S)
a�
    Find the facial landmarks in an image from the faces

    Parameters
    ----------
    img : np.uint8
        The image in which landmarks are to be found
    model : Tensorflow model
        Loaded facial landmark model
    face : list
        Face coordinates (x, y, x1, y1) in which the landmarks are to be found

    Returns
    -------
    marks : numpy array
        facial landmark points

    r
rg�������?rr)�r�predict)�dtype�outputN�)�����r)rrrr�cv2�resize�cvtColor�
COLOR_BGR2RGB�
signatures�tf�constant�uint8�np�array�flatten�reshape�astype�uint)	�imgr�face�offset_y�	box_moved�facebox�face_img�predictions�marksrrr	�detect_marksIs r9�r��rc	Cs4x.|D]&}t�||d|dfd|dtj�qWdS)ag
    Draw the facial landmarks on an image

    Parameters
    ----------
    image : np.uint8
        Image on which landmarks are to be drawn.
    marks : list or numpy array
        Facial landmark points
    color : tuple, optional
        Color to which landmarks are to be drawn with. The default is (0, 255, 0).

    Returns
    -------
    None.

    rrrr"N)r#�circle�LINE_AA)�imager8�color�markrrr	�
draw_marksus
rA)r)r:)�__doc__r#�numpyr+�
tensorflowr(rr
rrr9rArrrr	�<module>s
 ,