{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "Untitled0.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "3Z1o8qnvbtYJ"
      },
      "source": [
        "# Install deepface framework.\r\n",
        "!pip install deepface\r\n",
        "# Install matplotlib\r\n",
        "!pip install matplotlib  \r\n",
        "!pip install dlib"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7rbozVm9b_rO"
      },
      "source": [
        "# Load deepface libary\r\n",
        "from deepface import DeepFace\r\n",
        "# Import OpenCV to read images \r\n",
        "import cv2\r\n",
        "# Import matplotlib to plot images    \r\n",
        "import matplotlib.pyplot as plt\r\n",
        "import pandas as pd"
      ],
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kXmSdy8YcHLB"
      },
      "source": [
        "def verify(img1_path, img2_path):\r\n",
        "    # read images\r\n",
        "    img1 = cv2.imread('images/' + img1_path)\r\n",
        "    img2 = cv2.imread('images/' + img2_path)\r\n",
        "\r\n",
        "    # plot images\r\n",
        "    img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)\r\n",
        "    plt.imshow(img1)\r\n",
        "    plt.show()\r\n",
        "    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)\r\n",
        "    plt.imshow(img2)\r\n",
        "    plt.show()\r\n",
        "    \r\n",
        "    # Loop models and print results\r\n",
        "    models = [\"DeepFace\", \"Facenet\", \"VGG-Face\", \"Dlib\"]\r\n",
        "    for model in models:\r\n",
        "        result = DeepFace.verify('images/' + img1_path, 'images/' + img2_path, model_name = model)\r\n",
        "        print (model + ' result: ', result)"
      ],
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w5-Xqm5vgG9C",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 658
        },
        "outputId": "6ecae585-de0c-4b77-f9a5-3692d34ef590"
      },
      "source": [
        "verify(\"angelinajolie2.jpg\", \"angelinajolie4.jpg\")"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 85332 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 126212 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "DeepFace result:  {'verified': False, 'distance': 0.2314280867576599, 'max_threshold_to_verify': 0.23, 'model': 'DeepFace', 'similarity_metric': 'cosine'}\n",
            "WARNING:tensorflow:5 out of the last 48 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f821003f8c0> 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.\n",
            "Facenet result:  {'verified': True, 'distance': 0.28272944688796997, 'max_threshold_to_verify': 0.4, 'model': 'Facenet', 'similarity_metric': 'cosine'}\n",
            "WARNING:tensorflow:5 out of the last 48 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f81cc34bf80> 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.\n",
            "VGG-Face result:  {'verified': True, 'distance': 0.28341615200042725, 'max_threshold_to_verify': 0.4, 'model': 'VGG-Face', 'similarity_metric': 'cosine'}\n",
            "WARNING:tensorflow:5 out of the last 48 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f80ec03d680> 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.\n",
            "Dlib result:  {'verified': True, 'distance': 0.044739138376828524, 'max_threshold_to_verify': 0.07, 'model': 'Dlib', 'similarity_metric': 'cosine'}\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 658
        },
        "id": "oyscJzJQcteg",
        "outputId": "7290ce82-90e4-4df9-f4fd-292ec02c922e"
      },
      "source": [
        "verify(\"jenniferaniston.jpg\", \"reesewitherspoon.jpg\")"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 120728 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 115100 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "DeepFace result:  {'verified': False, 'distance': 0.3862358331680298, 'max_threshold_to_verify': 0.23, 'model': 'DeepFace', 'similarity_metric': 'cosine'}\n",
            "WARNING:tensorflow:5 out of the last 43 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f82896ad290> 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.\n",
            "Facenet result:  {'verified': False, 'distance': 0.8775187879800797, 'max_threshold_to_verify': 0.4, 'model': 'Facenet', 'similarity_metric': 'cosine'}\n",
            "WARNING:tensorflow:5 out of the last 43 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f8289526c20> 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.\n",
            "VGG-Face result:  {'verified': False, 'distance': 0.5625437796115875, 'max_threshold_to_verify': 0.4, 'model': 'VGG-Face', 'similarity_metric': 'cosine'}\n",
            "WARNING:tensorflow:5 out of the last 43 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f81cd399050> 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.\n",
            "Dlib result:  {'verified': False, 'distance': 0.074644552661988, 'max_threshold_to_verify': 0.07, 'model': 'Dlib', 'similarity_metric': 'cosine'}\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "f0VHrdupDwNX"
      },
      "source": [
        "def find(img_path):\r\n",
        "    # read images\r\n",
        "    img = cv2.imread(img_path)\r\n",
        "\r\n",
        "    # plot images\r\n",
        "    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\r\n",
        "    plt.imshow(img)\r\n",
        "    plt.show()\r\n",
        "    \r\n",
        "    # Loop models and print results\r\n",
        "    models = [\"DeepFace\", \"Facenet\", \"VGG-Face\"]\r\n",
        "    for model in models:\r\n",
        "        result = DeepFace.find(img_path, db_path = \"images\", model_name = model, distance_metric = 'cosine')\r\n",
        "        print (model + ' result: ', result)\r\n",
        "        # plot all verified images\r\n",
        "        for result_table in result.iloc:\r\n",
        "          print(\"File: \" + result_table.identity)\r\n",
        "          # plot image\r\n",
        "          img = cv2.imread(result_table.identity)\r\n",
        "          img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\r\n",
        "          plt.imshow(img)\r\n",
        "          plt.show()\r\n",
        "          "
      ],
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "YRGM5JfeDxxN",
        "outputId": "01392268-fd7b-452a-b2ef-b5594db38530"
      },
      "source": [
        "find(\"dicaprio.jpg\")"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 124856 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Finding representations: 100%|██████████| 17/17 [00:54<00:00,  3.18s/it]\n",
            "Analyzing:   0%|          | 0/1 [00:00<?, ?it/s]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Representations stored in  images / representations_deepface.pkl  file. Please delete this file when you add new identities in your database.\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "Analyzing: 100%|██████████| 1/1 [00:01<00:00,  1.94s/it]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "find function lasts  66.25598549842834  seconds\n",
            "DeepFace result:                       identity  DeepFace_cosine\n",
            "0  images/dicaprio_gatsby.jpg         0.186243\n",
            "1   images/angelinajolie2.jpg         0.220758\n",
            "File: images/dicaprio_gatsby.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 122928 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/angelinajolie2.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 85332 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Finding representations: 100%|██████████| 17/17 [00:20<00:00,  1.20s/it]\n",
            "Analyzing:   0%|          | 0/1 [00:00<?, ?it/s]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Representations stored in  images / representations_facenet.pkl  file. Please delete this file when you add new identities in your database.\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "Analyzing: 100%|██████████| 1/1 [00:01<00:00,  1.25s/it]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "find function lasts  24.68978524208069  seconds\n",
            "Facenet result:                            identity  Facenet_cosine\n",
            "0       images/dicaprio_gatsby.jpg        0.093151\n",
            "1    images/dicaprio_revenant3.jpg        0.297671\n",
            "2  images/dicaprio_wallstreet3.jpg        0.299356\n",
            "3     images/dicaprio_revenant.jpg        0.338166\n",
            "4  images/dicaprio_wallstreet2.jpg        0.340474\n",
            "File: images/dicaprio_gatsby.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 122928 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_revenant3.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 116520 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_wallstreet3.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 121196 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_revenant.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 140308 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_wallstreet2.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 75592 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Finding representations: 100%|██████████| 17/17 [00:20<00:00,  1.18s/it]\n",
            "Analyzing:   0%|          | 0/1 [00:00<?, ?it/s]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Representations stored in  images / representations_vgg_face.pkl  file. Please delete this file when you add new identities in your database.\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "Analyzing: 100%|██████████| 1/1 [00:01<00:00,  1.27s/it]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "find function lasts  22.05269742012024  seconds\n",
            "VGG-Face result:                            identity  VGG-Face_cosine\n",
            "0       images/dicaprio_gatsby.jpg         0.117203\n",
            "1  images/dicaprio_wallstreet3.jpg         0.248785\n",
            "2    images/dicaprio_revenant3.jpg         0.263308\n",
            "3             images/bradpitt3.jpg         0.338696\n",
            "4     images/dicaprio_revenant.jpg         0.357867\n",
            "5              images/bradpitt.jpg         0.360150\n",
            "6  images/dicaprio_wallstreet2.jpg         0.371260\n",
            "7             images/bradpitt4.jpg         0.372646\n",
            "File: images/dicaprio_gatsby.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 122928 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_wallstreet3.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 121196 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_revenant3.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 116520 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/bradpitt3.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 92672 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_revenant.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 140308 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/bradpitt.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 120604 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/dicaprio_wallstreet2.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 75592 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "File: images/bradpitt4.jpg\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 135796 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    }
  ]
}