Raspberry Pi boot self-starting opencv program script and error analysis and expansion

1. Make a test script
First we need to make a script to test if autostart works.

pi@raspberry:~ $ nano /home/pi/start.sh

The content of start.sh is as follows:

/usr/bin/python3 /home/pi/GeometryOperations.py

Be sure to pay attention to the use of absolute paths here, otherwise there is a probability that loading will fail.

Then add the executable file to the script file

pi@raspberry:~ $ chmod 777 start.sh

Test the script function

pi@raspberry:~ $ ./start.sh

If you observe that the program is running normally, you can continue to the next step

2. Add self-start
Modify the rc.local file

pi@raspberry:~ $ sudo nano /etc/rc.local

Find exit 0 in the text, the code added before this will be executed at startup, add a line of code before exit 0:

su pi -c "exec /home/pi/start.sh &"

ctrl+o to save, ctrl+x to exit, restart.

pi@raspberry:~ $ sudo reboot

After restarting the program can run.

If the Raspberry Pi does not respond after restarting, you can check the status of rc-local through systemctl

pi@raspberry:~ $ sudo systemctl status rc-local

After entering, you can see the status of each step after you start up, and check where an error will be reported.

4. Possible errors

Process:454 ExecStart=/etc/rc.local start (code=exited,status=1/FAILURE)

This error is probably because of the script file.
If it is just to start, then the file path must be written as an absolute path, otherwise an error will be reported.

error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

If the boot self-starting program contains codes with window operations such as Opencv, this error will be reported

The reason is due to the following sentence

su pi -c "exec /home/pi/start.sh &"

If your command needs to run for a long time (such as an infinite loop) or cannot exit after running, then you must make sure to add the "&" symbol at the end of the command to let the command run in its background

And if the code contains window interface operations similar to the following

cv2.imshow('Cap', frame)

It will conflict with the background operation, so that the GTK backend cannot be initialized, causing an error.
The solution is to delete all the operations related to the interface and windows, and the program can run well!
The above is reproduced to the blogger: TohkaQAQ
Direct link: https://blog.csdn.net/TohkaQAQ/article/details/121056564

Conclusion and Extension

in conclusion:
Initially, I planned to start the program using boot-up self-starting, but it has been failing. The reason for the error is:

error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

ie: Failed to initialize GTK backend in function "cvInitSystem"

I checked what the blogger quoted above said: delete the statement, but I just need it, and deleting it is unrealistic.

Analysis: The self-starting task is carried out by hanging in the background to run the task, but our programs all use the command line operation after booting, not the graphical interface after booting. Imagine a booting bottom command that only runs with text. Will there be a camera and a window to display the operation of these functions?
So if the code in the program has a command to call the window, it will report an error.

After consulting domestic and foreign forums, I found that there are related problems abroad, but from the questioner to today, two years later, there is no good answer, and now the docker environment is a popular trend, many The problems encountered by users all occur in remote control, but we use the local operation of the machine, and the methods are different. The proposed X11 or MOBAXTER or modifying DISPLAY are invalid for me, and I did not get it. a good answer.

Then: After changing it to boot, double-click the desktop to execute the file to run!
Reason: The command line that is started by double-clicking is the command line after we boot up. At this time, there is no problem in calling the window from the command line! ! !

If we write an executable program that needs to be run frequently, it is inconvenient to run it on the command line every time. If we happen to be installing a desktop version of Linux, then we can create a desktop icon and run the program by double-clicking it every time.

The following example is about how to create a desktop icon under Linux Ubuntu 18.04. 22.4 is also available, which is basically universal.

  1. Make a .txt file
  2. Throw in the following one, and the place that needs to be changed will be changed!
  3. Write the test.py or sh file inside, and make sure there is something that can be run later!
[Desktop Entry] #desktop icon entry
Version=1.0 #The version number is used for version maintenance, the option can be absent
Type=Application #Icon type, which can be Application, Link or Directory. Since it is an execution program, it is Application
Terminal=true #Whether to open a terminal when the program is executed, set it to True if the program prints information, and set it to True if it is false, no command line window will pop up, but it can also be run by double-clicking
Exec=/home/user/www/test.py #The path of the executable file, you can be a .sh file like hello_world.sh, or you can write a python on it
Name=whatever your name is #The name of the desktop icon, which is finally displayed on the desktop icon instead of the file name
Comment=Execture Hello WORLD #Describe function, optional
Icon=/home/hgq/Desktop/helloworld.jpeg #The path of the picture is used to beautify the icon. This sentence can also be used, but you will have this picture if you use it.

Finally, right-click this file, rename it to the suffix of .desktop, and then right-click this file to set it as executable, and then you can double-click to use it.

Expansion: timing detection and restart

The above self-starting at boot is just to try according to the blogger's instructions. It is not very difficult to start at boot! Then regularly check whether it is started and running, you can see this blogger

Direct link:

Tags: Python OpenCV Ubuntu AI Embedded system

Posted by stunna671 on Sun, 22 Jan 2023 03:58:53 +0300