• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

Xeus-Cling: Run C++ code in Jupyter Notebook

Vishwesh Shrimali
March 4, 2019 Leave a Comment
Compile how-to Tools Tutorial

March 4, 2019 By Leave a Comment

Facial Landmark Detection using OpenCV and Dlib in C++

Jupyter Notebook, formerly known as IPython Notebook, in my opinion, is one of the best tools for a programmer. You can interactively experiment with your code while writing it and that makes for a pleasurable programming experience.

The vast range of programming languages covered – R, Python, Julia, MATLAB, etc. along with an amazing set of widgets make it a very powerful and easy-to-use solution. This power of Jupyter Notebook can be witnessed live in the gallery of interesting Jupyter Notebooks.

One thing that I always found missing in Jupyter Notebook was the support for the C++ language. Of course, the reason it was not supported was that C++ programs have to be first compiled and then executed unlike Python which is an interpreted language and that’s why we are able to use it in Python or IPython prompt.

Imagine my surprise when I came across Xeus-Cling kernel which allows users to run their C++ codes on Jupyter Notebook. How? Well, the trick is in the word Cling which is an interactive C++ interpreter built on top of Clang and LLVM. Cling works on the principle of REPL – Read-Evaluate-Print-Loop which is also utilized by programming languages like Julia.

Let’s see how we can use Xeus-Cling kernel to run OpenCV and Dlib C++ code.

1. Installing Xeus-Cling

Xeus-Cling kernel requires conda package and has been built mainly for Linux and OS X platforms. The support for Windows is still experimental.

The best way to install Xeus-Cling is to create a new conda environment and install the kernel there. This is to make sure that the kernel installation doesn’t create any conflict with existing libraries like zeromq.

First, let’s create a new conda environment and activate it.

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

Next, we will use conda package installer to install Xeus-Cling.

conda install -c conda-forge xeus-cling

And tada! We are done. 🙂

2. Testing C++ Kernel

Now that we have installed Xeus-Cling, let’s see if it’s even working or not.

Let’s first activate the conda environment and then run a Jupyter Notebook.

source activate xeus-cling
jupyter-notebook

The notebook server will start running on your localhost. Next, create a new Jupyter Notebook and select C++11 kernel.

List of kernels available

Once the Jupyter Notebook opens, we can start working on it. Let’s start off with the classic Hello World example.

Hello World Example Using Xeus-Cling

That was pretty simple right?

Become an expert in Computer Vision, Machine Learning, and AI in 12-weeks! Check out our course

Computer Vision Course

3. Using OpenCV and Dlib with Xeus-Cling

Now let’s go to some serious stuff. What happens if we want to use OpenCV and Dlib with Xeus-Cling? Well, the solution was not very simple for me, but thanks to the community support, I managed to make it work in the end.

The trick lies in the following set of commands.

  1. Add library path using #pragma cling add_library_path
  2. Add include path using #pragma cling add_include_path
  3. Add an external shared library using #pragma cling load

That’s it! That’s all it takes to make it work.

So, if our OpenCV and Dlib libraries are present in /usr/local/lib we can simply do the following:

#pragma cling add_library_path("/usr/local/lib")

Personally, I find it best to write all these commands in one includeLibraries.h header file and just include it in beginning of the notebook.

Here’s how my includeLibraries.h file looks like:

A section of my includeLibraries.h file

Dlib library can be added similarly using:

#pragma cling load("/usr/local/lib/libdlib.so")

4. Test OpenCV and Dlib with Xeus-Cling

Once we have the includeLibraries.h file all ready, we can quickly create a new Jupyter-Notebook with C++11 kernel.

Test OpenCV with Xeus-Cling

The video present in the beginning of the post shows a Facial Landmark Detection demo which requires both OpenCV and Dlib and how the code can be run on Jupyter-Notebook using Xeus-Cling.

I hope you enjoyed the post. Feel free to reach out to us in case of any queries and suggestions. Happy hacking!

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in this post, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now


Tags: C++ Computer Vision deep learning dlib Jupyter Notebook OpenCV Xeus-Cling

Filed Under: Compile, how-to, Tools, Tutorial

About

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • Making A Low-Cost Stereo Camera Using OpenCV
  • Optical Flow in OpenCV (C++/Python)
  • Introduction to Epipolar Geometry and Stereo Vision
  • Depth Estimation using Stereo matching
  • Classification with Localization: Convert any Keras Classifier to a Detector

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2020 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.AcceptPrivacy policy