steph2000
Posts: 17
Joined: Sun Dec 14, 2014 8:03 pm

Circular Stream seems not to work

Thu Jan 08, 2015 8:38 pm

Hi!

I am having a strange problem with the Pi Camera.

My little program does the following:
  • The camera uses PiCameraCircularIO to continuously record 20 sec
  • Once I click a button, the previous 20 seconds will be saved to a file on the flash drive
  • After that, the circular IO continues. If I would click again, a new 20 sec interval will be saved.
I now notice that for some reason, the circular stream keeps becoming bigger and bigger. There is not much circular to this...

This is the code:

Code: Select all

import io
import random
import picamera
import RPi.GPIO as GPIO
import time

btnPressed = False

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def write_now():
    global btnPressed
    input_state = GPIO.input(17)
    if input_state == False:
        btnPressed = True
    return btnPressed

def write_video(stream):
    print('Writing video!')
    with stream.lock:
        # Find the first header frame in the video
        for frame in stream.frames:
            if frame.frame_type == picamera.PiVideoFrameType.sps_header:
                stream.seek(frame.position)
                break
        # Write the rest of the stream to disk
        with io.open('motion.h264', 'wb') as output:
            output.write(stream.read())

with picamera.PiCamera() as camera:
    stream = picamera.PiCameraCircularIO(camera, seconds=5)
    camera.start_recording(stream, format='h264')
    try:
        while True:
            camera.wait_recording(1)
            if write_now():
                btnPressed = False
                write_video(stream)
    finally:
        camera.stop_recording()
It is just the code from Basic Recipes Pi Camera. I must be doing something obvious wrong...

Thanks for the help!

dqpi
Posts: 33
Joined: Fri May 24, 2013 12:22 pm

Re: Circular Stream seems not to work

Sun Jan 11, 2015 11:45 pm

How do you check the size of the buffer? /what is your observation based on?

steph2000
Posts: 17
Joined: Sun Dec 14, 2014 8:03 pm

Re: Circular Stream seems not to work

Mon Jan 12, 2015 6:41 pm

I check the buffer simply by using a smartphone's timer. The video records the timer. As I set the circular video to 10 sec, I should at most have 10 seconds in the past of video. However, it was even worse that I thought. The circular video does not work at all, as it just flushes the entire video (from when the camera is started) to the disk.

I am at a loss what is going wrong. Probably I did something stupid, but cannot find it :-(

This is some updated code. It uses a button to start the saving of the video:

Code: Select all

import io
import random
import picamera
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def write_now():
    input_state = GPIO.input(17)
    if input_state == False:
        return True
    else:
        return False

def write_video(stream_circ):
    print('Writing video...')

    m = str(time.time()).replace('.','')[-4:]
    filename = './videos/motion_comb' + m + '.h264'
    with stream_circ.lock:
        # Find the first header frame in the video
        for frame in stream_circ.frames:
            if frame.frame_type == picamera.PiVideoFrameType.sps_header:
                stream_circ.seek(frame.position)
                break

        with io.open(filename, 'wb') as output:
            output.write(stream_circ.read())

    print('Video written')

with picamera.PiCamera() as camera:
    stream = picamera.PiCameraCircularIO(camera, seconds=20)
    camera.start_recording(stream, format='h264')
    print('Started recording...')
    print('Waiting for user action...')
    try:
        while True:
            camera.wait_recording(1)
            if write_now():
                camera.wait_recording(10)
                write_video(stream)
                print('Ended recording')
                break
    finally:
        camera.stop_recording()
I hope that you can find something wrong in this!

steph2000
Posts: 17
Joined: Sun Dec 14, 2014 8:03 pm

Re: Circular Stream seems not to work

Mon Jan 12, 2015 7:13 pm

Wow :-) I found the answer to my darkest dreams :D

Apparently, the camera has to have a resolution set. Namely, the CircularIO buffer is sized not based on the amount of seconds I specify, but using the resolution! If I set the resolution to some high value (eg Full HD), than it only records a few seconds instead of the whole bunch (or 20 seconds for that matter).

So, now it's about finding the correct amount of seconds for a decent resolution :-)

Return to “Camera board”