Skip to content

Instantly share code, notes, and snippets.

@anuradhawick
Last active December 5, 2022 12:24
Show Gist options
  • Save anuradhawick/ef937a2acd5d8a2da065dc6b094ff419 to your computer and use it in GitHub Desktop.
Save anuradhawick/ef937a2acd5d8a2da065dc6b094ff419 to your computer and use it in GitHub Desktop.
Face recognition example notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "1b5aeccc-fe82-4225-9783-db5c89a557ca",
"metadata": {},
"source": [
"# Required Installations\n",
"\n",
"## Installing dlib using `conda` with CUDA enabled\n",
"\n",
"The following instructions are copied from [https://gist.github.com/nguyenhoan1988/ed92d58054b985a1b45a521fcf8fa781](https://gist.github.com/nguyenhoan1988/ed92d58054b985a1b45a521fcf8fa781).\n",
"\n",
"Prerequisite: `conda` and/or `miniconda` are already installed. I would rather have `mamba` or `micromamba` for this.\n",
"\n",
"### Create a conda environment (optional, you may use your existing environment).\n",
"\n",
"```console\n",
"$ conda create -n dlib python=3.9 cmake ipython\n",
"```\n",
"\n",
"### Activate the environment (optional, you may use your existing environment).\n",
"\n",
"```console\n",
"$ conda activate dlib\n",
"```\n",
"\n",
"### Install CUDA and cuDNN with `conda` using nvidia channel.\n",
"\n",
"```console\n",
"$ conda install cuda cudnn -c nvidia\n",
"```\n",
"\n",
"Then find the path to the `nvcc` of this environment. We will use this path for the build step below, use the path without the suffix `nvcc`.\n",
"\n",
"```console\n",
"$ which nvcc\n",
"/path/to/your/miniconda3/envs/dlib/bin/nvcc\n",
"```\n",
"\n",
"### Install dlib.\n",
"\n",
"Clone and build dlib from source. If you see a warning about CUBLAS use command `sudo apt-get install libopenblas-dev liblapack-dev` (on ubuntu systems).\n",
"\n",
"```console\n",
"$ git clone https://github.com/davisking/dlib.git\n",
"$ cd dlib\n",
"$ mkdir build\n",
"$ cd build\n",
"$ cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1 -DCUDAToolkit_ROOT=/path/to/your/miniconda3/envs/dlib/bin/\n",
"$ cmake --build .\n",
"$ cd ..\n",
"$ python setup.py install --set DLIB_USE_CUDA=1\n",
"```\n",
"\n",
"### Test dlib\n",
"\n",
"Test whether your installation is successful in the `ipython` terminal.\n",
"\n",
"```console\n",
"(dlib) $ ipython\n",
"Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:56:21) \n",
"[GCC 10.3.0] on linux\n",
"Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
"\n",
"In [1]: import dlib\n",
"\n",
"In [2]: dlib.DLIB_USE_CUDA\n",
"\n",
"Out[2]: True\n",
"\n",
"In [3]: print(dlib.cuda.get_num_devices())\n",
"\n",
"1\n",
"```\n",
"\n",
"## Install face_recognition library\n",
"\n",
"Run the following command or refer to the original image respository [https://github.com/ageitgey/face_recognition](https://github.com/ageitgey/face_recognition).\n",
"```console\n",
"$ pip install face_recognition\n",
"```\n",
"\n",
"## Common error due to mismatching cuda versions\n",
"\n",
"You might observe the following in error when you have a conda installation and a system wide installation for cuda with different versions. For example `CUDA Version: 11.6` in `nvidia-smi` and `Cuda compilation tools, release 11.7, V11.7.99` in `nvcc --version`. In Ubuntu search for Additional drivers app and select the latest version `nvidia-driver-515` in this particular case. Use GUI in windows to update drivers (GEFORCE Experience). \n",
"\n",
"```\n",
"cnn_face_detector = dlib.cnn_face_detection_model_v1('dlib_models/mmod_human_face_detector.dat')\n",
"RuntimeError: Error while calling cudaMallocHost(&data, new_size*sizeof(float)) in ./dlib/dlib/cuda/gpu_data.cpp:211. code: 222, reason: the provided PTX was compiled with an unsupported toolchain.\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b65ffeab-d6b2-4601-9250-e793906646a8",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import face_recognition\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cb790e1c-2ebe-4c24-b41f-b42077b9b634",
"metadata": {},
"outputs": [],
"source": [
"# Load a sample picture and learn how to recognize it.\n",
"person_1 = face_recognition.load_image_file(\"./person-1.jpg\", mode='RGB')\n",
"person_1_face_encoding = face_recognition.face_encodings(person_1)[0]\n",
"\n",
"# Load a second sample picture and learn how to recognize it.\n",
"person_2_image = face_recognition.load_image_file(\"person-2.jpg\", mode='RGB')\n",
"person_2_face_encoding = face_recognition.face_encodings(person_2_image)[0]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3cb3f62f-f03a-454b-be8e-d9bca5402e4c",
"metadata": {},
"outputs": [],
"source": [
"known_face_encodings = [\n",
" person_1_face_encoding,\n",
" person_2_face_encoding\n",
"]\n",
"known_face_names = [\n",
" \"Name 1\",\n",
" \"Name 2\"\n",
"]\n",
"\n",
"# Initialize some variables\n",
"face_locations = []\n",
"face_encodings = []\n",
"face_names = []"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1c5b9475-3d6c-4c34-bcdb-3289de637580",
"metadata": {},
"outputs": [],
"source": [
"video_capture = cv2.VideoCapture(0)\n",
"# width = 1920\n",
"# height = 1080\n",
"# video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)\n",
"# video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)\n",
"# video_capture.set(cv2.CAP_PROP_FPS, 60)\n",
"scale = 2\n",
"\n",
"while True:\n",
" t1 = time.time()\n",
" # Grab a single frame of video\n",
" ret, frame = video_capture.read()\n",
" \n",
" if frame is None:\n",
" continue\n",
" \n",
" # Resize frame of video to 1/4 size for faster face recognition processing\n",
" small_frame = cv2.resize(frame, (0, 0), fx=1/scale, fy=1/scale)\n",
" \n",
" # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)\n",
" rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)\n",
"\n",
" # Find all the faces and face encodings in the current frame of video\n",
" face_locations = face_recognition.face_locations(rgb_small_frame)\n",
" face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)\n",
"\n",
" face_names = []\n",
" \n",
" for face_encoding in face_encodings:\n",
" # See if the face is a match for the known face(s)\n",
" matches = face_recognition.compare_faces(known_face_encodings, face_encoding)\n",
" name = \"Unknown\"\n",
"\n",
" face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)\n",
" best_match_index = np.argmin(face_distances)\n",
" if matches[best_match_index]:\n",
" name = known_face_names[best_match_index]\n",
"\n",
" face_names.append(name)\n",
" \n",
" # Display the results\n",
" for (top, right, bottom, left), name in zip(face_locations, face_names):\n",
" # Scale back up face locations since the frame we detected in was scaled to 1/4 size\n",
" top *= scale\n",
" right *= scale\n",
" bottom *= scale\n",
" left *= scale\n",
"\n",
" # Draw a box around the face\n",
" cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)\n",
"\n",
" # Draw a label with a name below the face\n",
" cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)\n",
" font = cv2.FONT_HERSHEY_DUPLEX\n",
" cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)\n",
" \n",
" t2 = time.time()\n",
" fps = 1/(0.00001 + t2 - t1)\n",
" cv2.putText(frame, f\"{fps:3.1f} FPS\", (30, 30), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 0, 0), 1)\n",
" # Display the resulting image\n",
" cv2.imshow('Video', frame)\n",
"\n",
" # Hit 'q' on the keyboard to quit!\n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
" \n",
"# Release handle to the webcam\n",
"video_capture.release()\n",
"cv2.destroyAllWindows()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment