I'm writing a python application which uses a PIR motion sensor to trigger the RPi camera (Rev. 1.3) and start a video. For this purpose I'm using the threading library, to achieve multithreading. At the moment I have 3 threads and one of them is used to trigger the camera. This is the code in the mentioned thread:
from test_cam import start_cam while True: if state == 1: if (GPIO.input(23)==1 or GPIO.input(14)==1 or GPIO.input(15)==1)and triggered == False: update_triggered(True) start_cam()
The start_cam method is in another python file, which is imported at the beginning of the code above. The method is implemented as follows:
def start_cam(): camera = picamera.PiCamera() camera.resolution = '720p' camera.framerate = 30 start = dt.datetime.now() camera.start_recording('./' + start.strftime('%Y-%m-%d_%H-%M-%S') + '.h264') camera.wait_recording(20) camera.stop_recording() camera.close()
The issue with it, is that the video starts recording but a lot of frames are missing in between. The average video length is ca. 5 seconds, although it should be ca. 20 seconds.
My suspect is that the multhithreading is causing this problem, because of the CPU distribution between the threads. I tested the start_cam method in a non multithreading environment and it worked fine. There were no missing frames or skips in the recording.
Does anyone know how to fix or workaround this problem?
Many thanks in advance