picamera2 icon indicating copy to clipboard operation
picamera2 copied to clipboard

[HOW-TO] Parse raw recorded footage

Open SidSidSid16 opened this issue 11 months ago • 4 comments

I am recording raw footage from a global shutter camera. The footage size is 840x640 and I intend to record in the SBGGR10 format.

import cv2 as cv
import numpy as np
from picamera2 import Picamera2
from picamera2.encoders import Encoder
from datetime import datetime

# Width and height of the recording
REC_WIDTH = 840
REC_HEIGHT = 640

# initialise picam2
picam = Picamera2()

# configure picam2
picam.video_configuration.enable_raw()
picam.video_configuration.raw.size = (REC_WIDTH, REC_HEIGHT)
picam.video_configuration.raw.format = 'SBGGR10'
picam.video_configuration.size = (REC_WIDTH, REC_HEIGHT)
picam.configure("video")

# window to display recording feed (view from X11)
cv.namedWindow("PiCam Feed")

recording = False

encoder = Encoder()

# start the picam
picam.start()

while True:
    # capture a frame from the picam
    frame = picam.capture_array()
    # clone to overlay text
    frame_overlay = frame.copy()

    # detect a key press
    key = cv.waitKey(1)

    # exit if 'e' key is pressed
    if key == ord('e'):
        # refuse to exit if a video is being recorded
        if recording:
            frame_overlay = cv.putText(
                img=frame_overlay,
                text="     CANNOT EXIT: REC IN PROGRESS!",
                org=(10, 50),
                fontFace=cv.FONT_HERSHEY_SIMPLEX,
                fontScale=1,
                color=(0,0,255),
                thickness=2,
                lineType=cv.LINE_AA
            )
        else:
            # stop the picam
            picam.stop()
            # break out of while loop
            break

    # toggle recording if 'r' pressed
    if key == ord('r'):
        if recording:
            recording = False
            picam.stop_recording()
            print("Recording finished")
            picam.stop()
            picam.start()
        else:
            recording = True
            recording_start = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
            filename = "recording_" + recording_start + ".raw"
            print("Started recording to:", filename)
            # use picam recording function with the right encoder
            picam.start_recording(
                encoder=encoder,
                output=filename,
            )

    # add text overlay to gui frame if recording
    if recording:
        frame_overlay = cv.putText(
            img=frame_overlay,
            text="REC",
            org=(10, 50),
            fontFace=cv.FONT_HERSHEY_SIMPLEX,
            fontScale=1,
            color=(255,0,0),
            thickness=2,
            lineType=cv.LINE_AA
        )

    # show the GUI frame
    cv.imshow("PiCam Feed", frame_overlay)

# when exiting, destroy all GUI windows
cv.destroyAllWindows()

How can I go about parsing this to play back the recorded footage?

SidSidSid16 avatar Mar 12 '24 16:03 SidSidSid16