-1

I am trying to save 10 seconds of buffered video using Python script, in particular '.rgb' format.

In order to do so, I have been using a PiCamera connected to a Raspberry Pi.

Based on the script below, if I choose to save the video using h264 format, I will be able to accomplish the desired goal successfully but if change the format from h264 to .rgb (targeted format), no outputs are generated.

Any thoughts what might be the issue here?

Thanks

Code snap:

import time
import io
import os 
import picamera
import datetime as dt
from PIL import Image
import cv2 

#obtain current time
def return_currentTime():
    return dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

#trigger event declaration
def motion_detected():
    while True:
        print ("Trigger event(y)?")
        trigger = input ()
        if trigger =="y":
            time = return_currentTime()
            print ("Buffering...")
            camera.wait_recording(5)     
            stream.copy_to(str(time)+'.rgb')            
           
        else: 
           camera.stop_recording()
           
           break
        
#countdown timer 
def countdown (t):
    while t:
        mins, secs = divmod (t,60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        t-=1
    print('Buffer available!')
   

camera = picamera.PiCamera()

camera.resolution = (640, 480)

stream = picamera.PiCameraCircularIO(camera, seconds = 5)

#code will work using h264 as format
camera.start_recording (stream, format = 'rgb')
countdown(5)
motion_detected()
Bert
  • 29
  • 5

1 Answers1

0

This question has to do with your stream format and how stream.copy_to() works.

According to the docs for the function copy_to(output, size=None, seconds=None, first_frame=2), first_frame is a restriction on the first frame to be copied. This is set to sps_header by default, which is usually the first frame of an H264 stream.

Since your stream format is RGB instead of H264, though, there are no sps_header's, and so copy_to cannot find an sps_header and copies nothing.

To solve this, you have to allow any frame to be the first frame, not just sps_header's. This can be done by setting first_frame=None in your call, like copy_to(file, first_frame=None).

id01
  • 1,491
  • 1
  • 14
  • 21
  • First problem solved...but now I might be at the edge of facing a new one. After applying the changes, I noticed that generated unencoded files (e.g. rgb, yuv) are smaller than generated encoded data files(e.g. h264). Shouldn't it be the other way around? Am I missing anything here? Thanks, – Bert Sep 02 '20 at 16:05
  • @Bert that's strange. Can you run `file output.rgb` to figure out the format of your output, and maybe hexdump the first few tens of bytes here? Let's try to figure out what's wrong. – id01 Sep 02 '20 at 21:07
  • Hi @id01, please see the info requested below (first command): pi@raspberrypi:~/Desktop $ file 2020-09-02\ 11\:13\:31.rgb 2020-09-02 11:13:31.rgb: data – Bert Sep 03 '20 at 15:09
  • second command: pi@raspberrypi:~/Desktop $ hexdump 2020-09-02\ 11\:13\:31.rgb 0000000 0000 0000 0000 0000 0000 0000 0000 0000 * 08ca000 – Bert Sep 03 '20 at 15:10
  • @Bert it looks like your video is completely black, which is very strange. Does the script work with h264? – id01 Sep 03 '20 at 21:36
  • @ id01 Yes, it does. It produces an output for both formats h264 and rgb. The main difference is that for h264, I need to remove the first_frame line in order for the script to work. – Bert Sep 04 '20 at 15:03
  • @Bert I mean, does it produce a completely black output with h264? Or does the output actually have images? – id01 Sep 05 '20 at 00:05
  • @id01- yes, the output has images. Thank you very much for all the help, I really appreciate it sir. I will put on hold this task for now and proceed with h264 encoder since I need to implement other functionalities. – Bert Sep 06 '20 at 13:21
  • That's very strange. I can't really figure out why it's doing that - maybe a bitrate problem? – id01 Sep 06 '20 at 22:01