Skip to content

Commit

Permalink
Merge pull request spmallick#272 from vishwesh5/master
Browse files Browse the repository at this point in the history
Add Facial Landmark Detection demo using Xeus Cling
  • Loading branch information
spmallick authored Mar 5, 2019
2 parents 89680cd + b88094d commit 34fffe7
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Learn OpenCV : C++ and Python Examples. You can find the details at [LearnOpenC

| Blog Post | |
| ------------- |:-------------|
|[Xeus-Cling: Run C++ code in Jupyter Notebook](https://www.learnopencv.com/xeus-cling-run-c-code-in-jupyter-notebook/) | [Code](https://github.com/spmallick/learnopencv/tree/master/XeusCling) |
|[Gender & Age Classification using OpenCV Deep Learning ( C++/Python )](https://www.learnopencv.com/age-gender-classification-using-opencv-deep-learning-c-python/) | [Code](https://github.com/spmallick/learnopencv/tree/master/AgeGender) |
|[Invisibility Cloak using Color Detection and Segmentation with OpenCV](https://www.learnopencv.com/invisibility-cloak-using-color-detection-and-segmentation-with-opencv/) | [Code](https://github.com/spmallick/learnopencv/tree/master/InvisibilityCloak) |
|[Fast Image Downloader for Open Images V4 (Python)](https://www.learnopencv.com/fast-image-downloader-for-open-images-v4/) | [Code](https://github.com/spmallick/learnopencv/tree/master/downloadOpenImages) |
Expand Down
25 changes: 25 additions & 0 deletions XeusCling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Xeus-Cling: Run C++ code in Jupyter Notebook

The repository contains notebook for **Facial Landmark Detection** demo in C++ with **C++11** kernel and **`includeLibraries.h`** header file for loading necessarily libraries. The blog post for the same can be found [here](https://www.learnopencv.com/xeus-cling-run-c-code-in-jupyter-notebook/).

## Installing Xeus-Cling

Create a new conda environment and activate it.

```
conda create -n xeus-cling
source activate xeus-cling
```

Use **conda package installer** to install Xeus-Cling.

```
conda install -c conda-forge xeus-cling
```

To create a Jupyter Notebook with C++ kernel, use the following.

```
source activate xeus-cling
jupyter-notebook
```
315 changes: 315 additions & 0 deletions XeusCling/facialLandmarkDetection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#include \"includeLibraries.h\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#include <string>\n",
"#include <cstring>\n",
"#include <fstream>\n",
"#include <dlib/opencv.h>\n",
"#include <dlib/image_processing.h>\n",
"#include <dlib/image_processing/frontal_face_detector.h>\n",
"#include <opencv2/opencv.hpp>\n",
"\n",
"using namespace cv;\n",
"using namespace std;\n",
"using namespace dlib;"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"// Write landmarks to file\n",
"void writeLandmarksToFile(dlib::full_object_detection &landmarks, const std::string &filename)\n",
"{\n",
" // Open file\n",
"\tstd::ofstream ofs;\n",
"\tofs.open(filename);\n",
" \n",
" // Loop over all landmark points\n",
" for (int i = 0; i < landmarks.num_parts(); i++)\n",
"\t{\n",
" // Print x and y coordinates to file\n",
"\t\tofs << landmarks.part(i).x() << \" \" << landmarks.part(i).y() << std::endl;\n",
"\n",
"\t}\n",
" // Close file\n",
"\tofs.close();\n",
"} "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"void drawLandmarks(Mat &im, dlib::full_object_detection landmarks )\n",
"{\n",
" for(int i = 0; i < landmarks.num_parts(); i++)\n",
" {\n",
" int px = landmarks.part(i).x();\n",
" int py = landmarks.part(i).y();\n",
"\n",
" char landmark_index[3];\n",
" sprintf(landmark_index, \"%d\", i+1);\n",
"\n",
" circle(im, Point(px, py), 1, Scalar(0, 0, 255), 2, CV_AA);\n",
" putText(im, landmark_index, Point(px, py), FONT_HERSHEY_SIMPLEX, .3, Scalar(255, 0, 0), 1);\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n"
]
}
],
"source": [
"std::cout << \"Hello world\" << std::endl;"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"dlib::frontal_face_detector faceDetector = dlib::get_frontal_face_detector();"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"dlib::shape_predictor landmarkDetector;"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"dlib::deserialize(\"shape_predictor_68_face_landmarks.dat\") >> landmarkDetector;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Input Image](girl.jpg)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"std::string imageFilename(\"girl.jpg\");"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"cv::Mat im = cv::imread(imageFilename);"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"std::string landmarksBasename(\"output\");"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"dlib::cv_image<dlib::bgr_pixel> dlibIm(im);"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"std::vector<dlib::rectangle> faceRects;"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"faceRects = faceDetector(dlibIm);"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of faces detected: 1\n"
]
}
],
"source": [
"std::cout << \"Number of faces detected: \" << faceRects.size() << std::endl;"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"std::vector<dlib::full_object_detection> landmarksAll;"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of landmarks : 68\n",
"Saving landmarks to output_0.txt\n"
]
}
],
"source": [
"for (int i = 0; i < faceRects.size(); i++)\n",
" {\n",
" // For every face rectangle, run landmarkDetector\n",
" dlib::full_object_detection landmarks;\n",
" \n",
" landmarks = landmarkDetector(dlibIm, faceRects[i]);\n",
" \n",
" // Print number of landmarks\n",
" if (i == 0) std::cout << \"Number of landmarks : \" << landmarks.num_parts() << std::endl;\n",
" \n",
" // Store landmarks for current face\n",
" landmarksAll.push_back(landmarks);\n",
" \n",
" // Draw landmarks on face\n",
" drawLandmarks(im, landmarks);\n",
"\n",
" // Write landmarks to disk\n",
" std::stringstream landmarksFilename;\n",
" landmarksFilename << landmarksBasename << \"_\" << i << \".txt\";\n",
" std::cout << \"Saving landmarks to \" << landmarksFilename.str() << std::endl;\n",
" writeLandmarksToFile(landmarks, landmarksFilename.str());\n",
" \n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"std::string outputFilename(\"result_Landmarks.jpg\");"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saving output image to result_Landmarks.jpg\n"
]
}
],
"source": [
"std::cout << \"Saving output image to \" << outputFilename << std::endl;"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"cv::imwrite(outputFilename, im);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Output image](result_Landmarks.jpg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++11",
"language": "C++11",
"name": "xeus-cling-cpp11"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "-std=c++11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
22 changes: 22 additions & 0 deletions XeusCling/includeLibraries.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma cling add_library_path("/home/ubuntu/installation/installation/OpenCV-3.4.1/lib/")
#pragma cling add_include_path("/home/ubuntu/installation/installation/OpenCV-3.4.1/include/")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_shape.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_face.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_stitching.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_objdetect.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_superres.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_videostab.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_calib3d.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_features2d.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_highgui.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_videoio.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_imgcodecs.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_video.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_photo.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_ml.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_imgproc.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_dnn.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_tracking.so.3.4.1")
#pragma cling load("/home/hp/OpenCV_installation/installation/OpenCV-3.4.1/lib/libopencv_viz.so.3.4.1")
#pragma cling load("/usr/local/lib/libdlib.so")
#pragma cling load("/usr/local/lib/libdlib.so.19.7.0")

0 comments on commit 34fffe7

Please sign in to comment.