Up MJPG, 320×240, fps=4 作成: 2021-04-08
更新: 2021-04-09


    mjpg_320_240.py
    #!/usr/bin/python import cv2 import sys camera_id = 0 delay = 1 window_name = 'streaming' WIDTH = 320 HEIGHT = 240 FPS = 4 cap = cv2.VideoCapture(camera_id) if not cap.isOpened(): sys.exit() def setup(): cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')) cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT) cap.set(cv2.CAP_PROP_FPS, FPS) def decode_fourcc(v): v = int(v) return "".join([chr((v >> 8 * i) & 0xFF) for i in range(4)]) def info(): fourcc = decode_fourcc(cap.get(cv2.CAP_PROP_FOURCC)) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) fps = cap.get(cv2.CAP_PROP_FPS) print("fourcc:{} fps:{} width:{} height:{}".format(fourcc, fps, width, height)) def loop(): while True: ret, frame = cap.read() if ret: cv2.imshow(window_name, frame) # つぎを書かないと画像ウィンドウが開かない cv2.waitKey(delay) def destroy(): cap.release() cv2.destroyWindow(window_name) # The Program starts from here if __name__ == '__main__': setup() info() try: loop() # When control c is pressed child program destroy() will be executed. except KeyboardInterrupt: destroy()


    $ vi mjpg_320_240.py
    $ chmod +x mjpg_320_240.py
    $ ./mjpg_320_240.py fourcc:MJPG fps:4.0 width:320.0 height:240.0 PC のデスクトップに,カメラ動画の Xウィンドウが開く。
    画像サイズは,320x240
    遅延時間は,2秒。
    control+C のキー入力で終了。