Ask Your Question
0

imwrite not working when Spawned by Crontab or Systemd??

asked 2019-05-24 11:42:56 -0500

johnnyrobot gravatar image

updated 2019-05-28 07:40:22 -0500

supra56 gravatar image

Hi all,

I've written a Raspberry Pi application (Ubuntu Mate) that's essentially a time-lapse camera. Every 30 seconds, it snaps an image and writes it to disk.

When I start the application manually from the terminal, it works as expected.

When I configure the script to execute automatically at startup, (using crontab or systemd - It needs to be executed as root) the script runs without any error, but no images are written to the disk.

At first I thought a permission issue, but the script is running as root... Any idea of what may be going on?

My script:

import time
import neopixel
import board
import cv2

class Camera01:
    GiGe = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
    Light = neopixel.NeoPixel(board.D18, 16, bpp=4, auto_write=True)


count = 0

while True:
    # Open camera
    Camera01.GiGe.Open()

    # Turn on Light
    Camera01.Light.fill((255, 255, 255, 255))

    # Snap Image
    image = Camera01.GiGe.GrabOne(1000)

    # Convert image to OpenCV Array
    image = cv2.cvtColor(image.Array, cv2.COLOR_BAYER_BG2BGR)

    # Save image to disk
    cv2.imwrite('saved_images/image'+str(count)+'.png', image)

    # Turn off Light
    Camera01.Light.fill((0, 0, 0, 0))

    # Close camera
    Camera01.GiGe.Close()

    # Increment counter
    count = count + 1

    time.sleep(30)
edit retag flag offensive close merge delete

Comments

What is workdir of your script? Does saved_images folder exist there?

mshabunin gravatar imagemshabunin ( 2019-05-27 07:46:20 -0500 )edit

As for root, how did you executed? For root, use sudo python3 script. In folder, use python 3 script.

supra56 gravatar imagesupra56 ( 2019-05-28 07:55:04 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-05-28 09:20:06 -0500

johnnyrobot gravatar image

I figured it out, it was along the lines of what mshabunin asked. I setup the application as a service, there is a "WorkingDirectory" assignment that I was missing. Once specified, it's working as it should.

[Unit] 
Description=Time lapse camera application

[Service]
WorkingDirectory=/var/www/camera/
User=root
ExecStart=/usr/bin/python3 /var/www/camera/main.py
Restart=always

[Install]
WantedBy=multi-user.target
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2019-05-24 11:42:56 -0500

Seen: 37 times

Last updated: May 28