from datetime import datetime
import cv2, os
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
def main():
if cap == None:
return False
out = None
capture = False
while cap.isOpened():
# カメラから映像を読み込む
_, img = cap.read()
cv2.imshow("preview", img)
if capture:
if out != None:
out.write(img)
key = cv2.waitKey(1)
if key == ord('z'):
cv2.imwrite("test.png", img)
if key == ord('x'):
if not capture:
capture = True
out = cv2.VideoWriter('test.avi',fourcc, 20.0, (640,480))
else:
capture = False
out.release()
out = None
elif key < 255:
break
# 事後処理
cap.release()
if out != None:
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()