JeVois Tutorials
1.22
JeVois Smart Embedded Machine Vision Tutorials
|
|
Here we develop a simple Python+OpenCV vision module that counts the total number of pips on some dice presented to JeVois. This application scenario was suggested by JeVois user mapembert at the JeVois Tech Zone in this post:
http://jevois.org/qa/index.php?qa=328
In this tutorial, you will learn:
This tutorial assumes JeVois v1.3 or later.
See A JeVois dice counting module in C++ for an implementation of this algorithm in C++ OpenCV (Linux host only).
The easiest to get started is to grab a copy of the samplepythonmodule in the JeVois github https://github.com/jevois/samplepythonmodule:
git clone https://github.com/jevois/samplepythonmodule.gitAfter this, you should:
jevois-create-python-module
which will grab that same sample code from GitHub, and will also immediately change names of classes and files to match our new module's name: usage is jevois-create-python-module <VendorName> <ModuleName>
, so here let us just run: cd jevois-create-python-module Tutorial PythonDiceCounteryou should now have the following:
pythondicecounter ├── CMakeLists.txt ├── COPYING ├── INSTALL ├── README.md ├── rebuild-host.sh ├── rebuild-platform.sh └── src └── Modules └── PythonDiceCounter ├── postinstall └── PythonDiceCounter.py
The author of the original module mentioned in the above post, Yohann Payet, sent us his code, which is written in C++ and as follows (this is standalone code not intended for operation on JeVois; in this tutorial we will convert it to Python and adapt it for use in JeVois):
Our tasks now are:
This algorithm was written for 640x480 resolution. Let us use that in our module as well. We edit ~/pythondicecounter/src/Modules/PythonDiceCounter/postinstall as follows:
jevois-add-videomapping YUYV 640 480 22 YUYV 640 480 22 Tutorial PythonDiceCounter
The postinstall script will be run by the JeVois camera after we install our new module to microSD. The video mapping required by our module and defined in postinstall will then be added to the main videomappings.cfg file on the microSD. Note that postinstall applies to the platform hardware only. To add the videomapping to your host configuration, just run the above command on your host computer (using sudo).
Note how here we have chosen 22 frames/s as our initial guess for framerate. Because 640x480 is a popular resolution, this will also allow us to avoid clashes with other modules that use this same resolution but rates of 30 frames/s or others. We will adjust this rate later once we know how fast this algorithm runs on JeVois.
The JeVois samplepythonmodule runs fine out of the box and thus our module should run as well if we have not introduced any mistakes.
On Linux: This has been automated through CMake, just connect JeVois and let it boot, then type:
cd ~/pythondicecounter ./rebuild-platform.sh --live
Which will instruct JeVois to export its microSD as a virtual flash drive to the host computer, will copy the required files, and will eject the drive to reboot JeVois. The module will be ready for use once JeVois has restarted.
You should see an output like this:
itti@iLab1:~/pythondicecounter$ ./rebuild-platform.sh --live [sudo] password for itti: -- JeVois version 1.2.3 -- JEVOIS_PLATFORM: ON -- JEVOIS_VENDOR: Tutorial -- JeVois microSD card mount point: /media/itti/JEVOIS -- JeVois serial-over-USB device: /dev/ttyACM0 -- JEVOIS_MODULES_TO_STAGING: OFF -- JEVOIS_MODULES_TO_MICROSD: OFF -- JEVOIS_MODULES_TO_LIVE: ON -- Install prefix for executable programs: /var/lib/jevois-build/usr -- Host path to jevois modules root: /var/lib/jevois-microsd -- The C compiler identification is GNU 6.1.0 -- The CXX compiler identification is GNU 6.1.0 -- Check for working C compiler: /lab/itti/jevois/software/jevois-sdk/out/sun8iw5p1/linux/common/buildroot/host/usr/bin/arm-buildroot-linux-gnueabihf-gcc -- Check for working C compiler: /lab/itti/jevois/software/jevois-sdk/out/sun8iw5p1/linux/common/buildroot/host/usr/bin/arm-buildroot-linux-gnueabihf-gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /lab/itti/jevois/software/jevois-sdk/out/sun8iw5p1/linux/common/buildroot/host/usr/bin/arm-buildroot-linux-gnueabihf-g++ -- Check for working CXX compiler: /lab/itti/jevois/software/jevois-sdk/out/sun8iw5p1/linux/common/buildroot/host/usr/bin/arm-buildroot-linux-gnueabihf-g++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- JeVois SDK root: /lab/itti/jevois/software/jevois-sdk -- Adding setup directives for Python module PythonDiceCounter base src/Modules -- Configuring done -- Generating done -- Build files have been written to: /lab/itti/pythondicecounter/pbuild Scanning dependencies of target modinfo_PythonDiceCounter [100%] Generating ../src/Modules/PythonDiceCounter/modinfo.yaml, ../src/Modules/PythonDiceCounter/modinfo.html [100%] Built target modinfo_PythonDiceCounter [100%] Built target modinfo_PythonDiceCounter Install the project... -- Install configuration: "" JeVois smart camera virtual USB ready at /media/itti/JEVOIS -- Installing: /media/itti/JEVOIS/modules/Tutorial/PythonDiceCounter -- Installing: /media/itti/JEVOIS/modules/Tutorial/PythonDiceCounter/postinstall -- Installing: /media/itti/JEVOIS/modules/Tutorial/PythonDiceCounter/PythonDiceCounter.py JeVois smart camera virtual USB disk ejected -- rebooting JeVois
Fire up your video capture software and set it to 640x480 @ 22fps. You should see the sample python module running, but under our new name:
From here on, we have two basic methods to implement the module:
In both cases, we will use the usbsd
JeVois command to export the microSD inside JeVois as a virtual flash drive, as detailed in user tutorial Live access to contents of the microSD inside JeVois and as done in programmer tutorial Programming a live JeVois camera using Python
Let us convert that C++ code to Python. A quick web search for 'python SimpleBlobDetector' reveals the following great tutorials that will help us with the translation:
When in doubt, we also just search the web; for example, to find out how to translate cv::GaussianBlur(...)
to Python, we just search for cv2.GaussianBlur
to find out the python syntax.
Here is our first attempt:
Let us try it by opening our video capture software, and we see some issues with that code:
There were a number of additional translation bugs in the above code, which we easily detect and fix using the JeVois video error messages as shown in the above image. Perhaps the most difficult to debug was to use a +
operator to combine flags in the cv2..threshold()
call instead of the |
operator of C++, as well as noting how cv2.threshold()
has two return values.
The final code after debugging is:
which yields results like:
Note that this algorithm runs a bit slow on JeVois, about 8 frames/s. One could adjust the videomapping accordingly.
It is likely that we could make it run faster, especially by implementing this module directly in C++. We would:
If you are using a Linux host and have been developing the code by editing the Python file on your host and then running ./rebuild-platform.sh --live
to install it to a live JeVois camera for debugging, you can now type:
./rebuild-platform.sh
Which installs instead to a directory jvpkg in your module:
-- Installing: /lab/itti/pythondicecounter/jvpkg/modules/Tutorial/PythonDiceCounter -- Installing: /lab/itti/pythondicecounter/jvpkg/modules/Tutorial/PythonDiceCounter/postinstall -- Installing: /lab/itti/pythondicecounter/jvpkg/modules/Tutorial/PythonDiceCounter/PythonDiceCounter.py
You would then finally type:
cd pbuild make jvpkg
which creates ~/pythondicecounter/Tutorial_pythondicecounter.jvpkg
You can send that file to your friends, and tell them to copy it to JEVOIS:/packages/ on their microSD. Next time JeVois restarts, it will unpack, install, configure, and delete the package, and the new module will be ready for use.