0

I am creating a raspberry pi timelapse camera encoding video with CV2 videowriter Each image captured with picamera is added to the videowriter and once the intended number of images are taken the videowriter closes.

However - while this works for a few thousand images - it stops at some limit with a filesize of 366Mb which is now frustrating me and I ask you - the internet and hoard of coders to tell me why I am bad a coding and how to fix this - you must be tempted by this..

Here is my offering of garbage for you to laugh pitifully at

import os, cv2
from picamera import PiCamera
from picamera.array import PiRGBArray
from datetime import datetime
from time import sleep

now = datetime.now()
x = now.strftime("%Y")+"-"+now.strftime("%m")+"-"+now.strftime("%d")+"-"+now.strftime("%H")+"-"+now.strftime("%M")  #string of dateandtimestart
print(x)
def main():
  imagenum = 10000 #how many images
  period = 1 #seconds between images
  os.chdir ("/home/pi/t_lapse")
  os.mkdir(x)
  os.chdir(x)
  filename = x + ".avi"

  camera = PiCamera()
  camera.resolution=(1920,1088)
  camera.vflip = True
  camera.hflip = True
  camera.color_effects = (128,128) #makes a black and white image for IR camera
  sleep(0.1)
  out = cv2.VideoWriter(filename, cv2.cv.CV_FOURCC(*'XVID'), 30, (1920,1088))
  for c in range(imagenum):
      with PiRGBArray(camera, size=(1920,1088)) as output:
          camera.capture(output, 'bgr')
          imagec = output.array
          out.write(imagec)
          output.truncate(0) #trying to get more than 300mb files..
          pass
      sleep(period-0.5)

  camera.close()
  out.release()
if __name__ == '__main__':
  main()

This example is a part of the whole code I've written (https://github.com/gchennell/RPi-PiLapse) which has an OLED display and buttons and selection of how many images as I have this all in an enclosure - the number of images seems to be limited to about 3000-4000 and then it just gives up and goes home... I tried adding the output.truncate(0) I have also recreated this in python3 before you cry "BUT CV2.CV2.VIDEOWRITER!!!!" and that hasn't changed a thing - I'm missing something here...

  • The picamera module has the ability to do timelapse with the 'capture_continuous' method. See https://picamera.readthedocs.io/en/release-1.13/recipes1.html?highlight=timelapse#capturing-timelapse-sequences – sativay Dec 17 '20 at 21:31
  • But the memory limit is based on the machine you're running the application on. What are the specs of your machine? RAM? – sativay Dec 17 '20 at 21:39
  • you can always save every frame in separated image (jpg or png) and later use other tools to convert it to movie - On RPi I use program `motion` to register moves in garden and later I use `ffmpeg` to convert it to video. – furas Dec 17 '20 at 22:57
  • @fth This is on a Pi2 or 3 so not loads - someone else I shared this with said the same. the idea is to avoid storing all the images in the first place and just create a video. – George Chennell Dec 18 '20 at 19:03
  • @furas I will probably try to avoid creating thousands of images and creating the video afterwards. but I think making seperate shorter videos every 3000 frames or so may work so I'll create the spliced version out of that. – George Chennell Dec 18 '20 at 19:04
  • Thanks for comments and tips so far – George Chennell Dec 18 '20 at 19:04
  • @furas makes a good point that using `ffmpeg` to stitch the images. The `motion` program has a time-lapse feature but is definitely not made for it and I would not go that route (not suggesting you did @furas). Mostly be careful with resolutions, codecs, fps, etc. to make it work. – sativay Dec 19 '20 at 09:07
  • 1
    Someone mentioned RAM - so I printed out memory usage after each image with psutil and hey ho - my RAM fills after 5000 images or so. Maybe it's time finally to upgrade to a Pi4 - or adapt the script to limit number of images... – George Chennell Dec 19 '20 at 12:48

0 Answers0