import cv2 import argparse import subprocess def main(): # Set up command-line argument parser parser = argparse.ArgumentParser(description='Real-time display of GREY image from /dev/video0') parser.add_argument('--roix', type=int, default=0, help='roi start x (default: 0)') parser.add_argument('--roiy', type=int, default=0, help='roi start y (default: 0)') parser.add_argument('--width', type=int, default=2448, help='image width (default: 1280)') parser.add_argument('--height', type=int, default=2048, help='image height (default: 960)') parser.add_argument('--fps', type=int, default=30, help='frame rate (default: 30)') parser.add_argument('--preferred_stride', type=int, default=2560,help='preferred stride(default: 2560)') args = parser.parse_args() v4l2_cmd = "v4l2-ctl -d /dev/video0 --set-ctrl roi_x=%d" % args.roix subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video0 --set-ctrl roi_y=%d" % args.roiy subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video0 --set-fmt-video=width=%d,height=%d,pixelformat=GREY" % (args.width,args.height) subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video0 --set-ctrl frame_rate=%d" % args.fps subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl --set-ctrl preferred_stride=%d " % args.preferred_stride subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video1 --set-ctrl roi_x=%d" % args.roix subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video1 --set-ctrl roi_y=%d" % args.roiy subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video1 --set-fmt-video=width=%d,height=%d,pixelformat=GREY" % (args.width,args.height) subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video1 --set-ctrl frame_rate=%d" % args.fps subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl --set-ctrl preferred_stride=%d " % args.preferred_stride subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video2 --set-ctrl roi_x=%d" % args.roix subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video2 --set-ctrl roi_y=%d" % args.roiy subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video2 --set-fmt-video=width=%d,height=%d,pixelformat=GREY" % (args.width,args.height) subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video2 --set-ctrl frame_rate=%d" % args.fps subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl --set-ctrl preferred_stride=%d " % args.preferred_stride subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video3 --set-ctrl roi_x=%d" % args.roix subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video3 --set-ctrl roi_y=%d" % args.roiy subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video3 --set-fmt-video=width=%d,height=%d,pixelformat=GREY" % (args.width,args.height) subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video3 --set-ctrl frame_rate=%d" % args.fps subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl --set-ctrl preferred_stride=%d " % args.preferred_stride subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video4 --set-ctrl roi_x=%d" % args.roix subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video4 --set-ctrl roi_y=%d" % args.roiy subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video4 --set-fmt-video=width=%d,height=%d,pixelformat=GREY" % (args.width,args.height) subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl -d /dev/video4 --set-ctrl frame_rate=%d" % args.fps subprocess.call(v4l2_cmd, shell=True) v4l2_cmd = "v4l2-ctl --set-ctrl preferred_stride=%d " % args.preferred_stride subprocess.call(v4l2_cmd, shell=True) # Open the /dev/video0 device cap0 = cv2.VideoCapture('/dev/video0',cv2.CAP_V4L) cap1 = cv2.VideoCapture('/dev/video1',cv2.CAP_V4L) cap2 = cv2.VideoCapture('/dev/video2',cv2.CAP_V4L) cap3 = cv2.VideoCapture('/dev/video3',cv2.CAP_V4L) cap4 = cv2.VideoCapture('/dev/video4',cv2.CAP_V4L) if not cap0.isOpened(): print("Failed to open /dev/video0") return if not cap1.isOpened(): print("Failed to open /dev/video1") return if not cap2.isOpened(): print("Failed to open /dev/video2") return if not cap3.isOpened(): print("Failed to open /dev/video3") return if not cap3.isOpened(): print("Failed to open /dev/video4") return # Set the image size cap0.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) cap0.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) cap1.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) cap2.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) cap3.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) cap3.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) cap4.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) cap4.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) # Calculate the aligned width #aligned_width = (args.width + 3)& ~3 # Loop over frames and display them while True: # Read a frame ret0, frame0 = cap0.read() ret1, frame1 = cap1.read() ret2, frame2 = cap2.read() ret3, frame3 = cap3.read() ret4, frame4 = cap4.read() # Check if reading was successful if not ret0: print("Failed to read frame from /dev/video0") break if not ret1: print("Failed to read frame from /dev/video1") break if not ret2: print("Failed to read frame from /dev/video2") break if not ret3: print("Failed to read frame from /dev/video3") break if not ret4: print("Failed to read frame from /dev/video4") break #if frame.shape[1]!= aligned_width: # aligned_frame = np.zeros((args.height, aligned_width), dtype=np.uint8) # aligned_frame[:, args.width] = frame # frame = aligned_frame # Display the frame cv2.imshow('preview (video0)', frame0) cv2.imshow('preview (video1)', frame1) cv2.imshow('preview (video2)', frame2) cv2.imshow('preview (video3)', frame3) cv2.imshow('preview (video4)', frame4) # Exit if 'q' key is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release resources cap0.release() cap1.release() cap2.release() cap3.release() cap4.release() cv2.destroyAllWindows() if __name__ == '__main__': main()