{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "noble-faculty",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imutils import paths\n",
    "import numpy as np\n",
    "import argparse\n",
    "import imutils\n",
    "import pickle\n",
    "import cv2\n",
    "import os\n",
    "\n",
    "dataset = 'dataset/train'\n",
    "embeddings = 'output/embeddings.pickle'\n",
    "detector = 'face_detection_model'\n",
    "\n",
    "embedding_model = 'openface_nn4.small2.v1.t7'\n",
    "protoPath = os.path.sep.join(detector, \"deploy.prototxt\"])\n",
    "modelPath = os.path.sep.join(detector,\n",
    "\t\"res10_300x300_ssd_iter_140000.caffemodel\"])\n",
    "detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath)\n",
    "\n",
    "\n",
    "\n",
    "embedder = cv2.dnn.readNetFromTorch(embedding_model)\n",
    "\n",
    "\n",
    "\n",
    "imagePaths = list(paths.list_images(dataset))\n",
    "\n",
    "knownEmbeddings = []\n",
    "knownNames = []\n",
    "\n",
    "# initialize the total number of faces processed\n",
    "total = 0\n",
    "\n",
    "# loop over the image paths\n",
    "for (i, imagePath) in enumerate(imagePaths):\n",
    "\t# extract the person name from the image path\n",
    "\tprint(\"Processing image {}/{}\".format(i + 1,\n",
    "\t\tlen(imagePaths)))\n",
    "\tname = imagePath.split(os.path.sep)[-2]\n",
    "\n",
    "\t\n",
    "\timage = cv2.imread(imagePath)\n",
    "\timage = imutils.resize(image, width=600)\n",
    "\t(h, w) = image.shape[:2]\n",
    "\n",
    "\t\n",
    "\timageBlob = cv2.dnn.blobFromImage(\n",
    "\t\tcv2.resize(image, (300, 300)), 1.0, (300, 300),\n",
    "\t\t(104.0, 177.0, 123.0), swapRB=False, crop=False)\n",
    "\n",
    "\tdetector.setInput(imageBlob)\n",
    "\tdetections = detector.forward()\n",
    "\n",
    "\t\n",
    "\tif len(detections) > 0:\n",
    "\t\t\n",
    "\t\ti = np.argmax(detections[0, 0, :, 2])\n",
    "\t\tconfidence = detections[0, 0, i, 2]\n",
    "\n",
    "\t\t\n",
    "\t\tif confidence > args[\"confidence\"]:\n",
    "\t\t\t\n",
    "\t\t\tbox = detections[0, 0, i, 3:7] * np.array([w, h, w, h])\n",
    "\t\t\t(startX, startY, endX, endY) = box.astype(\"int\")\n",
    "\n",
    "\t\t\t\n",
    "\t\t\tface = image[startY:endY, startX:endX]\n",
    "\t\t\t(fH, fW) = face.shape[:2]\n",
    "\n",
    "\t\t\t\n",
    "\t\t\tif fW < 20 or fH < 20:\n",
    "\t\t\t\tcontinue\n",
    "\n",
    "\t\t\t\n",
    "\t\t\tfaceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255,\n",
    "\t\t\t\t(96, 96), (0, 0, 0), swapRB=True, crop=False)\n",
    "\t\t\tembedder.setInput(faceBlob)\n",
    "\t\t\tvec = embedder.forward()\n",
    "\n",
    "\t\t\t\n",
    "\t\t\tknownNames.append(name)\n",
    "\t\t\tknownEmbeddings.append(vec.flatten())\n",
    "\t\t\ttotal += 1\n",
    "\n",
    "\n",
    "print(\"Serializing {} encodings...\".format(total))\n",
    "data = {\"embeddings\": knownEmbeddings, \"names\": knownNames}\n",
    "f = open(args[\"embeddings\"], \"wb\")\n",
    "f.write(pickle.dumps(data))\n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
