デフォルトの BGR3 を,MJPG に変更
mjpg.py
#!/usr/bin/python
import cv2
import sys
camera_id = 0
delay = 1
window_name = 'streaming'
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'))
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.py
$ chmod +x mjpg.py
$ ./mjpg.py
fourcc:MJPG fps:30.0 width:640.0 height:480.0
PC のデスクトップに,カメラ動画の Xウィンドウが開く。
画像サイズは,640x480
遅延時間は,30秒弱。
control+C のキー入力で終了。
|