I have successfully coded a Python 3 Script that detects objects in a frame given my the camera. Based on those objects detected, I run commands and logic. I have tested this and I can run it via the terminal thru:
python3 /home/pi/tensorflow1/models/research/object_detection/Object_detection_picamera.py
And I don't have any issues with this. We ran our test cases and we got the results we wanted.
However, we want to "package" the RaspberryPi, and make sure that our script runs once the boot up is successful. This way, we just need to plug it in a socket and it would work. That being the case, needing to plug a monitor, a keyboard, and a mouse to the board is not an option.
After doing some research, we decided that the best approach is via systemd
.
I proceeded to make my systemd file as such:
sudo nano /lib/systemd/system/sample.service
and then I placed the following:
[Unit]
Description=My Sample Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/tesnorflow1/models/research/object_detection/Object_detection_picamera.py
User=pi
[Install]
WantedBy=multi-user.target
After save and exiting, I ran the following:
sudo systemctl daemon-reload
sudo systemctl enable sample.service
sudo reboot
After booting again, I have my program a couple minutes to run. I then checked the status:
sudo systemctl status sample.service
and I was greeted by an error:
Traceback:
File "/home/pi/tensorflow1/models/research/object_detection/Object_detection_picamera.py", line 34, in <module>
from utils import label_map_util
File "/home/pi/tensorflow1/models/research/object_detection/utils/label_map_util.py", line 21, in <module>
from object_detection.protos import string_int_label_map_pb2
ImportError: No module named 'object_detection'
Which was referring to the following line:
#Import utilities
from utils import label_map_util # <----- this line
from utils import visualization_utils as vis_utils
The interesting thing now is that if I run the script via the terminal, I don't encounter the same error message.
Clearly, there must be some timing concerns involved.
How do I run my service once I'm sure that everything has been loaded? It seems that my service is trying to run and some files have not been loaded.
Any leads would be very much appreciated.
EDIT:
With the help of @Ingo's comment, I logged the sys.path
variable. I ran the code via the service, and ran the code via the terminal. After that I got the output of the service and compared it to the output of the terminal.
Initially, the following paths were NOT in the list given by the service:
/home/pi
/home/pi/tensorflow1/models/research
/home/pi/tensorflow1/models/research/slim
So I added those in my [Service]
block:
[Service]
ExecStart= /usr/bin/python3 /homoe/pi/tensorflow1/models/research/object_detection/Object_detection_picamera.py
Environment="PYTHONPATH=/usr/hin/python3"
Environment="PYTHONPATH=/home/pi"
Environment="PYTHONPATH=/home/pi/tensorflow1/models/research"
Environment="PYTHONPATH=/home/pi/tensorflow1/models/research/slim"
User=pi
I saved this and rebooted. I checked and compared the output of the sys.path
from the service and from the terminal, but the following paths were still not added:
/home/pi
/home/pi/tensorflow1/models/research
And I'm not sure how else to add it.
utils
is a custom module or can it be imported system wide? Does it imported just the two times you listed or is there an additional location importingutils
? What directory you are in when running the script from the command line successfully?