Skip to content

Instantly share code, notes, and snippets.

@iamvee
Last active November 21, 2021 16:37
Show Gist options
  • Save iamvee/b310d70e47cb180104a9ff6a7940f775 to your computer and use it in GitHub Desktop.
Save iamvee/b310d70e47cb180104a9ff6a7940f775 to your computer and use it in GitHub Desktop.
facial landmarks detection
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## install :\n",
"\n",
"```shell \n",
"pip install numpy opencv-python dlib imutils matplotlib jupyter\n",
"```\n",
"\n",
"## shape predictor\n",
"\n",
"download this shape predictor file: [link](https://raw.githubusercontent.com/italojs/facial-landmarks-recognition/master/shape_predictor_68_face_landmarks.dat)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from imutils import face_utils\n",
"import dlib\n",
"import cv2\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p = \"shape_predictor_68_face_landmarks.dat\"\n",
"detector = dlib.get_frontal_face_detector()\n",
"predictor = dlib.shape_predictor(p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = cv2.imread(\"./image2.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
"rects = detector(gray, 0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rects"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def color(num):\n",
" if num < 17:\n",
" return 'c'\n",
" if num < 22:\n",
" return 'b'\n",
" if num < 27:\n",
" return 'b'\n",
" if num < 31:\n",
" return 'g'\n",
" if num < 36:\n",
" return 'g'\n",
" if num < 42:\n",
" return 'k'\n",
" if num < 48:\n",
" return 'k'\n",
" if num < 61:\n",
" return 'r'\n",
" if num < 68:\n",
" return 'y'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
"plt.figure(figsize=(20,50))\n",
"plt.imshow(im_rgb)\n",
"\n",
"for (i, rect) in enumerate(rects):\n",
" shape = predictor(gray, rect)\n",
" shape = face_utils.shape_to_np(shape)\n",
"\n",
" for j, (x, y) in enumerate(shape):\n",
" plt.scatter([x], [y], c=color(j))\n",
" plt.text(x, y, str(j))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# landmark numbers \n",
"\n",
"| test | from | to |\n",
"|:---- | ---- | ---- |\n",
"| face | 0 | 16 |\n",
"| eyebrow | 17 | 21 |\n",
"| eyebrow | 22 | 26 |\n",
"| nose-up | 27 | 30 | \n",
"| nose-bot | 31 | 35 |\n",
"| eye | 36 | 41 |\n",
"| eye | 42 | 47 |\n",
"| lip-out | 48 | 60 |\n",
"| lip-in | 61 | 67 |"
]
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
@Shirkhani1235q2edccc
Copy link

تب

@Shirkhani1235q2edccc
Copy link

Uploading 16375126221494914537597775260216.jpg…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment