#RaspberryPi – 6 commands to install #OpenCV for #Python in #RaspberryPi4

Hi !

Quick post to remind me the 6 commands to install OpenCV in my Raspberry Pi

sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-100 
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo pip3 install opencv-contrib-python

There is an optional command to update pip, which is always nice.

Happy Coding!

Greetings @ Burlington

El Bruno

References

My posts on El Bruno

Advertisements

#VSCode – 20 lines to display a webcam camera feed with #Python using #OpenCV

Hi !

I always write this from scratch, so it seems that I’ll drop this one here. So next time I search for this, I’ll find myself.

And with some extra lines, we can even detect faces and display some face landmarks:

This is the base of some many image recognition scenarios, so I hope this will save me some local search time 😀

Happy coding!

Greetings @ Toronto

El Bruno

References

My posts on Face Recognition using Python

  1. Detecting Faces with 20 lines in Python
  2. Face Recognition with 20 lines in Python
  3. Detecting Facial Features with 20 lines in Python
  4. Facial Features and Face Recognition with 20 lines in Python
  5. Performance improvements with code
  6. More performance improvements, lowering the camera resolution

And some general Python posts

#Anaconda – My steps to install a virtual environment with #TensorFlow, #Keras and more

Hi!

So today post is not a post, just a selfish reminder of the steps I do when I setup a new dev machine

  • Install Anaconda (see references). I use the default settings, and important: I don’t add Anaconda to Windows PATH.
  • Open Anaconda command prompt as administrator
open anaconda as administrator

Need to be open as Admin in order to install updates

  • Install updates with the command
conda update conda 
conda update –all
  • Create a new development environment named “tfEnv” with tensorflow. Activate the environment
conda create -n tfenv tensorflow 
conda activate tfenv
  • The command to install keras is
pip install
keras

However, if it doesn’t work, I install keras with the following packages

pip install matplotlib 
pip install pillow
pip install tensorflow==1.14
conda install mingw libpython
pip install git+git://github.com/Theano/Theano.git
pip install git+git://github.com/fchollet/keras.git
  • Finally, install Jupyter notebook kernel and create a new kernel for the current virtual environment
pip install ipykernel 
ipython kernel install --user --name=tfEnv
  • There seems to be an issue to install OpenCV using pip with the command
pip install
opencv-python

So, I Install the OpenCV nonofficial package. 1st I download a compatible package from

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopencl

Install with

pip install
c:\temp\opencv_python-4.1.1-cp36-cp36m-win_amd64.whl

Happy coding!

Greetings @ Toronto

El Bruno

References

#Anaconda – How to create a custom #Python virtual environment and use it in #Jupyter notebooks (a kernel!)

Hi!

In yesterday post, I created a new virtual environment named [devtf] and in this environment I’ve installed a lot of tools that I need. Then I tried to launch a jupyter notebook from this environment, to use this tools and, of course, it didn’t work.

anaconda start virtual environment and error on launch jupyter notebook

It was time to read and learn how this works. So, when I finally get this I find this amazing article which really explain how this works “Using Virtual Environments in Jupyter Notebook and Python” (see references)

Jupyter Notebook makes sure that the IPython kernel is available, but you have to manually add a kernel with a different version of Python or a virtual environment. First, you need to activate your virtual environment. Next, install ipykernel which provides the IPython kernel for Jupyter. And finally, you can add your virtual environment to Jupyter.

So the commands are

pip install --user ipykernel 
python -m ipykernel install --user --name=devtf

Where “devtf” is the name of the new kernel you want to create. Now, when I launch Jupyter Notebooks, the new kernel is available to be used

jupyter notebook change kernel to one with tensorflow

When I started to use this new kernel (virtual environment) I realized that I didn’t installed TensorFlow. You know, being happy about this, naming the kernel TF but not installing the core component. And, sure, my notebooks didn’t work.

jupyter notebook with kernel without tensorflow

I went to my terminal / command prompt and installed TensorFlow. Then I only need to restart the Kernel, and everything start working. I added a extra couple of lines in my notebook just to check the TensorFlow and keras versions.

jupyter notebook tf ok and test keras version

I find similar errors with another packages, so I pip installed the packages in the terminal and restart the kernel to have the notebook OK. So, my simple reminder for myself about how to do this!

Happy coding!

Greetings @ Mississauga

El Bruno

References

#Python – Can’t install TensorFlow on Anaconda, maybe is the Visual Studio distribution

Hi!

This is the 2nd time I get a weird error when I install TensorFlow in my Anaconda distribution. And this is the 2nd time I realize that I’m using the Anaconda version that is preinstalled with Visual Studio. I’m not sure if the spaces in the path affects the creation of environments or it’s something else, however my current and big and amazing solution is:

  • Uninstall Anaconda
  • Install Anaconda again

And then, follow the simple commands in the official Anaconda and TensorFlow doc (see references)

conda create -n tensorflow_env tensorflow
conda activate tensorflow_env

Once tensorflow is installed, I usually test this in python

> Python 
import tensorflow as tf
print(tf.__version__)

Note: please ignore the typos!

anaconda start python and test anaconda version

Now TensorFlow is installed and it’s time to move forward with a new development environment.

Happy Coding!

Greetings @ Burlington

El Bruno

References

#Python – Let’s use a #FaceRecognition demo app for a performance comparison between #RaspberryPi3 and #RaspberryPi4

Hi!

I started to do some tests with the new Raspberry Pi 4 and the results are amazing. I’m not a performance expert, so I decided to pick up some of the demos / apps I’ve creating for the Raspberry Pi and run them in both models: Raspberry Pi 3 B+ and Raspberry Pi 4.

I started with an amazing set of tutorials on how to perform Face Recognition from Adrian Rosebrock (see references). I’ve been using his Face Recognition python package for this scenarios and it’s an amazing one.

I added some code to a custom version of Adrian’s Face Recognition sample, and it looks great. The main idea was to track in real-time the current FPS (similar to the work I did with the Image AI and Hololens sample a couple of days ago, see references).

This sample load a file with 15 trained faces and analyze frame by frame to

  • Detect faces in the frame.
  • If a face is detected, draw a frame around it.
  • For each detected frame analyze if the face is a trained face.
  • If the face is part of the trained dataset, the app will add the name of the person on top of the frame.

I display in real-time the FPS processed with a USB camera in a Raspberry Pi 3 B+. Doing a lot of tweaks and getting the best performance in the device I could never process 1FPS. The average processing data were between 0.6 and 0.9 FPS in a Raspberry Pi 3B+.

python face recognition in raspberry py 3 with FPS live

IMHO, these results are great for a small device like a Raspberry Pi 3B+. But now it was time to test it in the new Raspberry Pi 4. And an important note here is to remark that even if I did this tests in a Raspberry Pi 4 with 4GB of Rams, the performance results are similar to a RPI4 with just 1 GB of ram. We have more memory, however the processor improvements are quite significant in the new version.

I installed all the necessary software in the Raspberry Pi 4 and I got 3X better results. I’ve even tun this in a 1080p resolution to get a sense of the real processing time. The average processing data were between 2.3 and 2.4 FPS in a Raspberry Pi 4.

python face recognition in raspberry py 4 with FPS live

Amazing! In this scenario the Raspberry Pi 4 is almost 3 times faster than the Raspberry Pi 3. And again, these are amazing times for a 50USD device.

The sample source code is https://github.com/elbruno/Blog/tree/master/20190819%20Rpi%203%20vs%20Rpi%204%20Face%20Recognition

I even have time for some BBQ time with family and friends!

Happy coding!

Greetings @ Toronto

El Bruno

References

#Python – Detecting #Hololens in realtime in webcam feed using #ImageAI and #OpenCV with performance improvements

Hi!

In my previous post I created a sample on how to use ImageAI and OpenCV to detect Hololens from a webcam frame (see references). I added some code to the last sample, and I found that the performance was not very good.

python using imageai to detect hololens less than 1 fps

With the previous sample code, I couldn’t process more than 1 frame per second. So, I started to make some improvements and I got this result

python using imageai to detect hololens little more than 1 fps

Not an amazing one, but still is nice to have more than 1 frame per second analyzed.

I even remove all the camera preview and still works in less than 1FPS.

python using imageai to detect hololens no opencv camera preview

So, now it’s time to read and learn of the deep code on ImageAI. Fun times!

Happy coding!

Greetings @ Burlington

El Bruno

References

#Python –Detecting #Hololens in realtime in webcam feed using #ImageAI and #OpenCV (thanks to @OlafenwaMoses)

Hi!

Let’s start with a very quick intro:

During the past months, I’ve been playing around with several Image Analysis tools. And ImageAI (see references) is one that deserves a full series of posts. Please take a look at the product and the source code in GitHub, and also please thank the one behind this: Moses Olafenwa (@OlafenwaMoses).

And now, my 2 cents. I’ve started to test ImageAI to create my own image detection models. Most of the times, this is a hard path to do, however ImageAI show me an interesting option.

… with the latest release of ImageAI v2.1.0, support for training your custom YOLOv3 models to detect literally any kind and number of objects is now fully supported, …

Wow! That’s mean that I can pick up my own set of images dataset and train on top of a YOLOv3 and use it as a trained model. Again, this is amazing.

So, I started to read the article [Train Object Detection AI with 6 lines of code, see references] where Olafenwa explains how to do this using a data set with almost 500 rows with images for Hololens and Oculus Rift.

The code is very simple and easy to read. There are also examples on how to analyze a single file, or a video, or even a camera feed. The output for the analysis can be also in a new file, in a processed video or even a full log file with the detected information.

I started to read the code samples and I realized that I’m missing a scenario:

Display the realtime feed from a webcam, analyze each webcam frame and if a device is found, add a frame to the realtime feed to display this.

I use OpenCV to access to my camera, and it took me some time to figure out how to convert my OpenCV2 camera frame to the format needed by ImageAI. At the end, thanks to the GitHub code I manage to create this (very slow but working) demo

As usual in this scenario, now it’s time to improve the performance and start testing with some tweaks to get a decent up and running App.

And of course, the code

Happy coding!

Greetings @ Toronto

El Bruno

Resources

#RaspberryPi – Running a #python script in a Python Virtual Environment on reboot / startup

Hi!

Adrian Rosebrock is a very smart person who has tons of great resources about Computer Vision in PyImageSearch.com. Most of them are with Python, and he also have some of them focused on how to perform CV using OpenCV in a Raspberry Pi.

In the post [Running a Python + OpenCV script on reboot, see resources] he explains how to automatically run a Python script when a Raspberry Pi starts. He uses python virtual environments, so the first 2 commands are focused on to load the virtual env. Then, move to the app folder and run the python script.

source ~/.profile
workon cv
cd /home/pi/pi-face-recognition
python startup.py

Something like this:

python source and cv on raspberry pi.

The suggested approach consists on create a Schell Script [.sh file] with these lines and add them to the auto start. However, once you create the file and test it, there seems to be an issue with the Source command.

python source not working on SH file

Ok, so no source command in an SH file. I started to think on install all my python dependencies directly in the main user, however the idea of working with virtual environments is very useful for me. It was to read online about Linux, python and more.

Note: Before moving forward, I may need to add some context. I need to run my python script in a Terminal. My device will always auto-start with a 3.5 inches touch screen and a camera, so I need some GUI loaded.

This is an excellent article on how to add actions to the Raspberry Pi start-up [How to Execute a Script at Startup on the Raspberry Pi, see resources]. So I added my SH file here and it didn’t work and I need to figure out how to load a virtual environment and run a python script.

After a couple of tests, I realized that all the files I need are part of the virtual env location in the device.

raspberry pi python folder for virtual envs

So, I only need to add the full path to my command to make it work without the and [workon] command. My complete command will became:

/home/pi/.virtualenvs/cv/bin/python /home/pi/pi-face-recognition/startup.py

So, I edited my autostart file adding this command

raspberry pi auto start file launching python script with a virtual env

And done! My python script running on a python virtual environment on the device startup is working!

Happy coding!

Greetings @ Toronto

El Bruno

Resources

#Python – How to create a Virtual Environment in #Windows10 (Easy one, and brain backup!)

Hi!

Quick post today, and mostly a reminder on how to create a Virtual Environment in Python in Windows 10. I’ve been doing this mostly in my Mac and my Raspberry Pi, and I always forget how to do this on Windows, so … I’ll write this post to have this

Install Python3.

Download the installers from the official Python source (see references). I usually install it on the root of my C: hard drive and name it with the version. In example: c:\Python37_64 folder.

Remember to also add this folder and the Scripts folder to the Environment Variables.

Note: Once you start to install tools which uses Python, your OS becomes a nightmare. You will have your own installed python, the version installed with Visual Studio, the one with Visual Studio Code, another one for Anaconda.

I’m not sure if this is a best practice or not, but I usually remove all the other versions and keep mines in the root of the C: drive.

Ok, let’s go on.

For a new virtual environment named “testEnv”, open a command prompt and navigate to the python folder. Then run the command

python -m virtualenv testEnv

After a couple of seconds, the virtual Environment will be installed, and you can use it by run the [activate.bat] script. In this example

  • The virtual environment will be created at [c:\Python37_64\testEnv]
  • The virtual environment activate script will be at [c:\Python37_64\testEnv\Scripts\activate.bat]

So the full command sequence is similar to this one

Now you have your virtual environment up and running and you may want to start to add your own packages or tools. Like in example: Numpy or to list the installed packages

Finally, you may leave the virtual environment with the command

deactivate

Happy Coding!

Greetings @ Toronto

El Bruno

Resources