コーディングにおいて特筆するべきところは、以下の通り。
- VideoWriter_fourcc関数で動画のフォーマットを決める(どんなフォーマットがあるかは調べてみましょう)
- readメソッドの戻り値は、読み込みのTrue/Falseとフレーム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from PIL import Image | |
import cv2 as cv | |
# FPS | |
fps = 30 | |
# Frame size | |
WIDTH=1920 | |
HEIGHT=1080 | |
size = (WIDTH, HEIGHT) | |
#Setting recording format | |
fourcc = cv.VideoWriter_fourcc(*'XVID') | |
#fourcc = cv.VideoWriter_fourcc('M', 'J', 'P', 'G') | |
#Recording file Setting | |
video = cv.VideoWriter('sample.avi', fourcc, fps, size) | |
#Camera Setting | |
camera = cv.VideoCapture(0) | |
camera.set(cv.CAP_PROP_FRAME_WIDTH, WIDTH) | |
camera.set(cv.CAP_PROP_FRAME_HEIGHT, HEIGHT) | |
try: | |
while True: | |
#frame read | |
_, frame = camera.read() | |
cv.imshow('image', frame) | |
#record frame | |
video.write(frame) | |
key = cv.waitKey(1) | |
if key == 27: # ESC key for closing | |
video.release() | |
f.close() | |
break | |
except KeyboardInterrupt: | |
print("Close me? -> Enter ESC") |
0 件のコメント:
コメントを投稿