Raspberry Pi script with timeout and record does not work if started from autostart

54 Views Asked by At

I'm quite new to coding and cobbled together some code to automatically run our python cameras for animal surveillance.

I have a working code that runs well from autostart (located on the desktop for easy editing). Just some recording after a short sleep.

We used to just change the videofilefolder name during sleep to let the script fail and be able to transfer the files. Rather inelegant.

So I cobbled some code together to have an "abort popup" appear and ask if the script and recording should be terminated. On "Abort" the script terminates otherwise, without userinput, the script starts recording as usual. Works fine in thonny. But if I exchange the script with the old one nothing happens. I suspect that some libraries are not loaded or that at execution some permissions are missing. But I can't figure it out.

Please help me with my script and the autostart.

Recording Script:

import tkinter as tk
import sys
import threading
from picamera import PiCamera

CAMERA_NAME = 'Camera 1'
CAMERA_CODE_PATH = '/home/mantis123/Desktop/VideoFiles/'
RECORDING_DURATION = 60  # Duration for each recording in seconds

def abort_program():
    global timer_thread
    if timer_thread.is_alive():
        timer_thread.cancel()  # Cancel timer thread
    popup.destroy()  # Close popup window
    root.deiconify()  # Show main window again
    sys.exit(0)      # Terminate the program

def timer_expired():
    popup.destroy()  # Close the popup
    start_camera_recording()
    sys.exit(0)      # Terminate

def start_camera_recording():
    camera = PiCamera()
    camera.resolution = (1200, 1200)
    camera.framerate = 25
    camera.annotate_text = (CAMERA_NAME)
    
    for i in range(1, 72):
        filename = f'{CAMERA_CODE_PATH}Camera_1_{i}.h264'
        camera.start_recording(filename, bitrate=15000000)
        camera.wait_recording(RECORDING_DURATION)
        camera.stop_recording()

# MAin
root = tk.Tk()
root.title("Recording")

# Hide main
root.withdraw()

# popup
popup = tk.Toplevel(root)
popup.title("Abort?")
popup_label = tk.Label(popup, text="Click the 'Abort' button to prevent the camera from recording.")
popup_label.pack(padx=20, pady=20)
abort_button = tk.Button(popup, text="Abort", command=abort_program)
abort_button.pack(padx=20, pady=10)

# Timer thread
timer_thread = threading.Timer(60, timer_expired)
timer_thread.start()

# Main event loop
root.mainloop()

what do i have to change to make it work with the autostart method I'm using. Or do I have to change that?

Autostart code does not work. works in thonny and autostart works with different code.

0

There are 0 best solutions below