There are many ways to start a python script on boot and the processes is suppose to be very easy. Well! it is easy if your python code does not use OpenCV and GPIO pins. But if you have some kind of GUI then it is not possible to launch the script directly from the boot sequence without loading the Desktop. What supposedly is to be a 2 minutes job took a lot many hours so I decided to write this post as a note for future self and also to others
Note: Python OpenCv scripts with image frames can be displayed only after the GUI (Desktop) loads up. It is not possible to launch it from a text console. The best way to launch an OpenCV project is from the LXTerminal by modifying the autostart file. Other methods will fail with import cv2 "no module found" error or with the failed to load X server.
modify the autostart file by following the code for Buster OS
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
For Jessie OS or below, the autostart file can be accessed with below link
sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
When you open the file you will see the below code already in it
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
Add "@lxterminal -e python3 /home/pi/Desktop/test_pgm.py" this line to the file. Make sure you have used the right path and program name. Mine is in desktop and the program name is test_pgm.py you can also choose between python2 or python3 here. Make sure you place it above the @xscreensaver line. So in the code your file should be something like this
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@lxterminal -e python3 /home/pi/Desktop/Athena.py
@xscreensaver -no-splash
Save the file with Ctrl+X and Y and reboot the system with sudo reboot to check the results.
Tip: The autostart file runs everytime you log into pi. So for testing the auto-launch code, you don't have to reboot every time, just log out and log-in and the autostart will get executed.
ok folks, i am reaching out here - - as this is an ooold post - - but im running opencv and realsesne on a raspberry pi 4 and using python wrappers for the coding.
if i launch with python3 main.py it runs ... at a good frame rate ... if i autolaunch it in ANY way using the same command in either autostart or profile or bashrc etc etc it runs REALLY slowly . and the frame rate drops to about a quarter of the normal speed .
ive tried (albeit badly) to force it run under specific users and to change its priority .. but the ONLY way it EVER runs correctly is if i open terminal, navigate to its folder and launch it manually from there .. its gotta be a permissions thing right ? :S .. i just dont know how to debug it :S .. ive even started searching now for a macro program that will allow me to just press one button after desktop has loaded and have that 'physically' write it into terminal and press enter ! .. any ideas at ALL from your side?? please ? O_O
Hi Steve,
Have you tried corntab? I have seen some people using it to launch openCV as weel you might wanna check it out
Let me know if it works you.
If evrything fails you can write another simple python script which will launch your openCV script. You can then launch this new script automatically using corntab or what not.
Sourav Gupta
PermalinkThanks for sharing.
- Log in or register to post comments
Joined February 12, 2018 696Monday at 02:11 PM