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
