We have realized a simple haarcascade-based face detection with openCV. Images are acquired from a Basler dart camera. You can use that example also for any Basler 2D cameras.
This is a simple example of running face detection with OpenCV on images acquired from a Basler camera. Please ensure you have pylon, PyPylon and OpenCV installed. See the guide here on how to set up the required software on your system.
Face Detection with a Basler Dart Camera and OpenCV on Raspberry Pi B+
With the launch of Imaginghub Basler is releasing a fully functional Python 3 wrapper for the Pylon C++ API called PyPylon.
In this demo I would like to give you an impression of the simplicity of writing an image processing application straight forward with Python. Furthermore I will run the project on an ultra-low budget setup like the Raspberry Pi to give you a feeling of how easy and affordable embedded vison can be. So let’s dive right in!
Basically there are only two things needed for the basic setup. One the one side you need an Basler camera to be able to use the Python Wrapper. And on the other side some sort of single-board-computer.
As we are talking about an embedded setup I suggest one of the dart USB3 Vison cameras which are perfect for a low-budget setup like this.
You can choose most of the available SBCs out there. It just has to have an ARMv7 architecture because PyPylon is only released therefore.
I will use Raspbian Jessie as operating system, but any Linux distribution should work equally. Just make sure that you use at least a 16GB SD-Card because we need a lot of disk storage for compiling.
After initial setup I recommend setting the Pi to ”boot only to command line” to save some resources. You can do this under “sudo raspi-config” and then “boot_behaviour”.
From now on you can do every command of this guide via SSH as well so the choice is up to you. For that you will only need the IP Address by writing “ip addr show”.
For this setup I recommend Python 3.5 which already is fully compatible with PyPylon.
Follow this guide to get PyPylon and OpenCV up and running: From Zero to Image
A very smart way of writing Python code is using Jupyter Notebook. It lets you connect to a Web-based Python IDE from any machine in your network. So when you’re not working directly on the Pi, this is great way to write your first samples very straight forward.
Hereis a good tutorial for getting it done.
Getting an Image with PyPylon
When you are familiar with Pylons C++ API you will feel very familiar with the Python Wrapper. All functions are the same but only in Python typical syntax. When you are searching for more advanced features, just check out the detailed C++ Programmers-guide.
Grabbing an image only requires very few steps. I recommend just using this short demo as a first impression and using the sample code afterwards.
First the InstantCamera has to be initialized:
import pypylon.pylon as py icam = py.InstantCamera(py.TlFactory.GetInstance().CreateFirstDevice())
Then just open the Camera and grab an image:
icam.Open() img = icam.GrabOne(4000)
Now you’ve got the image as an object. To get it as an numpy array for OpenCV just do the following:
img = img.Array
You can change each camera parameter within PyPylon, but it’s a lot easier to create a profile in Pylon Viewer and load it like this:
One important setting I would like to mention is the PixelFormat. The Dart supports RGB8 output, which makes the handling in OpenCV a lot easier. Safe this setting in the profile or set it within Python by:
icam.PixelFormat = "RGB8"
If you want to see the picture in Python you can use for example Matplotlibs pylab class and just write
sudo apt-get install python-matplotlib sudo apt-get install python3-matplotlib import matplotlib.pyplot as plt import cv2 plt.imshow(cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
The color-conversion is needed, because plt expects the image to be in BGR format.
You will need an Haar Cascade XML file, which contains the parameters of all kinds of faces. You can find a ton of them for different kinds of objects in the internet or even create your own. First import it with this code:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
In some cases the classifier doesn't load succefully from the file. In these cases you should add the whole path (f.e. '/opt/opencv-3.2.0/data/haarcascades/haarcascade_frontalface_default.xml'). Now you can run the face_cascade function. It will return a list of faces which we print into the image for illustration.
faces = face_cascade.detectMultiScale(img, 1.3, 5) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
Now just show the edited image:
plt.imshow(cv2.cvtColor(img, cv2.COLOR_RGB2BGR)) plt.show()
As I mentioned I recommend using Jupyter Notebook for your first PyPylon approaches. I will provide not only a Python but also a Jupyter Notebook code sample. Therefore click on the "Code" tab.
|StudentCV||pushed 465200ede12f9354067d0bc2d306830d0c514cb2||2016-12-19 13:05:50 UTC|
|StudentCV||pushed c6e6f1a5c93aa5c2f0fbfc3cbec0b7ffb71aec55||2016-12-19 13:05:12 UTC|
|StudentCV||pushed faab3fb5325c3870894b92e6a681b1ebb0adc122||2016-09-16 11:14:29 UTC|
|StudentCV||pushed 90e7ea76028bf80a7d647f4d324eb0f21ef34850||2016-08-19 10:59:48 UTC|
|StudentCV||pushed e94fb9d9bba14e64dcd56fee518cb64c83039db9||2016-07-15 08:13:50 UTC|
|StudentCV||pushed ec6e06e6b752ee0b21c6f0d8762186484d588a02||2016-07-13 09:52:55 UTC|
|StudentCV||pushed 933323a2eadeaa7f3b196e33c9d3ef56a19e0bf3||2016-07-13 09:50:47 UTC|
|StudentCV||pushed baeda90652d0a6f57d6e682913addd9063f5c668||2016-07-13 09:50:13 UTC|