liveBook · Manning

Ramdisk

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

A ramdisk is a filesystem whose stored files reside in volatile computer memory instead of physical storage devices.

A ramdisk is a filesystem whose files reside in volatile memory rather than physical storage. For live robot video, it stores the latest camera frame without adding disk workload, allowing multiple applications to read it efficiently. section 10.4 section 10.4.1

A practical camera pipeline uses a ramdisk as follows:

  • Read the Linux ramdisk location from XDG_RUNTIME_DIR and construct paths for the final image and a temporary image. section 10.4.1
  • Capture frames with OpenCV, optionally resize or flip them, and write each frame to the temporary path. section 10.4.1
  • Replace the final image with os.replace so readers see complete images rather than partially written files. section 10.4 section 10.4.1

Robot video frame

A Raspberry Pi camera service saves 320×240 frames to /run/user/1000/robo_stream.jpg through the ramdisk. Desktop streaming, web streaming, and QR-code detection applications can poll or read the same current image without competing for physical disk access. section 10.4.1 section 10.6

Listing 10.2 stream_save.py: Saving captured video frames to the filesystem
#!/usr/bin/env python3
import os
import cv2
 
IMG_PATH = os.environ['XDG_RUNTIME_DIR'] + '/robo_stream.jpg'
TMP_PATH = os.environ['XDG_RUNTIME_DIR'] + '/robo_stream_tmp.jpg'
FRAME_WIDTH = 320
FRAME_HEIGHT = 240
 
def save_frames(cap):
    counter = 0
    while True:
        counter += 1
        ret, frame = cap.read()
        assert ret, 'Cannot read frame from camera'
        frame = cv2.flip(frame, -1)
        cv2.imwrite(TMP_PATH, frame)
        os.replace(TMP_PATH, IMG_PATH)
        print('frames:', counter, end='\r', flush=True)
 
def init_camera():
    cap = cv2.VideoCapture(0)
    assert cap.isOpened(), 'Cannot open camera'
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, FRAME_WIDTH)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT)
    return cap
 
def main():
    cap = init_camera()
    try:
        save_frames(cap)
    finally:
        print('releasing video capture device...')
        cap.release()
 
main()

Runnable Python code combines OpenCV capture, ramdisk paths, image flipping, temporary writes, atomic replacement, and camera cleanup. section 10.4.1

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Explains Linux ramdisk paths, frame sizing, camera capture, orientation correction, temporary writes, atomic replacement, and cleanup.
Examples
Demonstrates a Raspberry Pi camera writing flipped 320×240 JPEG frames to a shared runtime-directory image.
What it assumes
Assumes Linux filesystem access and a camera, while demonstrating the required environment-variable and OpenCV usage.
What you can do after
Operate/configure a Python and OpenCV camera service that publishes current frames through a Linux ramdisk.
How it connects
Connects ramdisks with filesystems, OpenCV, atomic operations, video streaming, filesystem polling, and QR-code detection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.