yongyong-e

2. Capture Video 본문

프로그래밍/OpenCV - Python

2. Capture Video

Yonghan Kim 2017. 9. 21. 20:23

1) Capture Video

import numpy as np
import cv2

# Video 캡쳐를 위해 VideoCapture() 객체생성
# VideoCapture(0)에서 0은 연결된 카메라의 인수
cap = cv2.VideoCapture(0)

while(True):
ret, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('frame',gray)
if cv2.waitKey(5) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()


2) Saving Video

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('save_video.mp4',fourcc, 20.0, (800,600))

while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)

out.write(frame)

cv2.imshow('frame',frame)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
else:
break

cap.release()
out.release()
cv2.destroyAllWindows()

▶ cv2.VideoWriter(outputFile, fourcc, frame, size)

    outputFile   저장될 파일명

    fourcc   Codec정보

    frame   초당 저장될 frame

    size   저장될 사이즈


3) Playing Video

import numpy as np
import cv2

cap = cv2.VideoCapture('video.mp4')

while(cap.isOpened()):
ret, frame = cap.read()

cv2.imshow('frame',frame)
if cv2.waitKey(5) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()


Comments