<jupyter_start><jupyter_text>Detecting Facial Landmarks using ```MTCNN```+ Use MTCNN, a deep learning model to identify faces and landmarks+ Use the detector to detect face and facial landmarks on a given image+ Visualize the results [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/PacktPublishing/Hands-On-Generative-AI-with-Python-and-TensorFlow-2/blob/master/Chapter_8/mtcnn_facial_landmarks_demo.ipynb)  Install MTCNN<jupyter_code>!pip install pip install mtcnn<jupyter_output>Requirement already satisfied: pip in /usr/local/lib/python3.6/dist-packages (19.3.1)
Collecting install
  Downloading https://files.pythonhosted.org/packages/f0/a5/fd2eb807a9a593869ee8b7a6bcb4ad84a6eb31cef5c24d1bfbf7c938c13f/install-1.3.4-py3-none-any.whl
Collecting mtcnn
[?25l  Downloading https://files.pythonhosted.org/packages/67/43/abee91792797c609c1bf30f1112117f7a87a713ebaa6ec5201d5555a73ef/mtcnn-0.1.0-py3-none-any.whl (2.3MB)
[K     |████████████████████████████████| 2.3MB 5.3MB/s 
[?25hRequirement already satisfied: opencv-python>=4.1.0 in /usr/local/lib/python3.6/dist-packages (from mtcnn) (4.1.2.30)
Requirement already satisfied: keras>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from mtcnn) (2.4.3)
Requirement already satisfied: numpy>=1.11.3 in /usr/local/lib/python3.6/dist-packages (from opencv-python>=4.1.0->mtcnn) (1.19.5)
Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras>=2.0.0->mtcnn) (2.10.0)
Requirement already satisfie[...]<jupyter_text>Load Libraries<jupyter_code>from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle, Circle
from mtcnn.mtcnn import MTCNN
import cv2<jupyter_output><empty_output><jupyter_text>Utility function to mark and visualize results<jupyter_code>def detect_faces(image, face_list):
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    ax = plt.gca()
    for face in face_list:
        # mark faces
        x, y, width, height = face['box']
        rect = Rectangle((x, y), width, height, fill=False, color='orange')
        ax.add_patch(rect)
        # mark landmark features
        for key, value in face['keypoints'].items():
            dot = Circle(value, radius=12, color='red')
            ax.add_patch(dot)
    plt.axis('off')
    plt.show()<jupyter_output><empty_output><jupyter_text>Instantiate MTCNN detector<jupyter_code>detector = MTCNN()<jupyter_output><empty_output><jupyter_text>Load Sample Image<jupyter_code>image = cv2.imread('trump_ref_cc.jpg')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Use MTCNN detector to identify face and landmark features<jupyter_code>faces = detector.detect_faces(image)<jupyter_output>WARNING:tensorflow:5 out of the last 15 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f1ac87cd7b8> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
WARNING:tensorflow:6 out of the last 16 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f1ac87cd7b8> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.functio[...]<jupyter_text>Visualize Results<jupyter_code>detect_faces(image, faces)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12098638582070383, 'fraction_numerical': 0.045723092730542, 'mean_word_length': 5.6, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 38, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '194458', 'license': 'MIT', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1365, 'n_words': 296, 'path': 'Chapter_8/mtcnn_facial_landmarks_demo.ipynb', 'repo_name': 'PacktPublishing/Hands-On-Generative-AI-with-Python-and-TensorFlow-2', 'size': '3893'}