File size limit in making timelapse video on the fly with OpenCV, Picamera and an IR camera

334 Views Asked by At

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...

0

There are 0 best solutions below