
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:

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.

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.

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

And done! My python script running on a python virtual environment on the device startup is working!
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
Resources
- Running a Python + OpenCV script on reboot https://www.pyimagesearch.com/2016/05/16/running-a-python-opencv-script-on-reboot/
- How to Execute a Script at Startup on the Raspberry Pi https://www.wikihow.com/Execute-a-Script-at-Startup-on-the-Raspberry-Pi
My posts on Raspberry Pi β‘π²β‘
Dev posts for Raspberry Pi
- How to grant permissions to a folder after git clone, to perform dotnet restore on a Raspberry Pi
- How to install .Net Core 3.1 in a Raspberry Pi 4
- Installing Visual Studio Code in a Raspberry Pi 4, run as root, fix black screen
- How to install .Net Core in a Raspberry Pi 4 and test with Hello World
- Build and Run C# NetCore projects in a Raspberry Pi 4 with Visual Studio Code
- Letβs do some Git dev in Raspberry Pi 4 (GitHub and Azure DevOps!)
- Install OpenCV
- Install Python π Virtual Environments in Raspberry Pi
- Setup SSH passwordless access to remote work with Docker π³
- Manage Docker π³ as a non-root user
- Build Docker π³ images from Visual Studio Code remotely using a Raspberry Pi
Tools and Apps for Raspberry Pi
- Where is my Task Manager in RaspberryPi? Letβs try htop
- Multi-monitor πΊ in Raspberry Pi 4 rocks !
- Double Commander on RaspberryPi4, because files are important
- How to install Docker π³ in a Raspberry Pi 4
- Installing Visual Studio Code in a Raspberry Pi
- Installing Visual Studio Code in a Raspberry Pi, run as root, fix black screen (Updated)
- 6 commands to install OpenCV for Python π in a Raspberry Pi 4
Setup the device
- 1st Setup without monitor πΊ: auto connect to WiFi πΆ, enable SSH, update and more
- Setup without monitor: enable VNC
- How to enable auto start with HDMI safe mode
- Running a Python π script in a Python Virtual Environment on reboot / startup
- Setup Wifi on Ubuntu
Sir i did the same thing but mine not working? I am using python3, should i put python3 instead of python in
“/home/pi/.virtualenvs/cv/bin/python”?
LikeLike
I have a program with OpenCV where I need to save a video with some image processing when I run it manually the program works perfectly and saves the video file as I expected. Nevertheless, when I applied this process described above the program run good but when I execute the video saved appeared a problem (“VLC is unable to open the MRL”), I would like to know if you can help me with this issue.
By the way, thank you very much for the tutorial.
LikeLike