import cv2
cap = cv2.VideoCapture(0)
if cap.isOpen():
print('width: {}, height : {}'.format(cap.get(3), cap.get(4))
while True:
ret, fram = cap.read()
if ret:
gray = cv2.cvtColor(fram, cv2.COLOR_BGR2GRAY)
cv2.imshow('video', gray)
k == cv2.waitKey(1) & 0xFF
if k == 27:
break
else:
print('error')
cap.release()
cv2.destroyAllWindows()
cv2.VideoCapture()
๋ฅผ ์ฌ์ฉํด ๋น๋์ค ์บก์ณ ๊ฐ์ฒด ์์ฑ ๊ฐ๋ฅ
()์์ ์ซ์๋ ์ฅ์น ์ธ๋ฑ์ค(์ด๋ค ์นด๋ฉ๋ผ๋ฅผ ์ฌ์ฉํ ๊ฒ์ธ๊ฐ)
cap.isOpen()
: ๋น๋์ค ์บก์ณ ๊ฐ์ฒด๊ฐ ์ ์์ ์ผ๋ก Open๋์๋์ง ํ์ธ
while True: ํน์ ํค๋ฅผ ๋๋ฅผ๋๊น์ง ๋ฌดํ ๋ฐ๋ณต์ ์ํด ์ฌ์ฉ
ret, fram = cap.read()
: ๋น๋์ค์ ํ ํ๋ ์์ฉ read
์ ๋๋ก ํ๋ ์์ ์ฝ์ผ๋ฉด ret๊ฐ์ด True, ์คํจํ๋ฉด False
fram์ ์ฝ์ ํ๋ ์์ด ๋์ด
cv2.cvtColor()
: frame์ ํ๋ฐฑ์ผ๋ก ๋ณํ
๋๋ ์์ ๊ฐ์ด COLOR_BGR2GRAY๋ก ์ฒ๋ฆฌํ ์์ !
cap.release()
: ์คํํ ์บก์ณ ๊ฐ์ฒด๋ฅผ ํด์
import cv2
cap = cv2.VideoCapture('vtest.avi')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 25.0, (640,480))
while (cap.isOpend()):
ret, frame = cap.read()
if ret:
# ์ด๋ฏธ์ง ๋ฐ์ , 0:์ํ, 1 : ์ข์ฐ
frame = cv2.flip(frame, 0)
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(0) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
cv2.VideoWriter(outputFile, fourcc, frame, size)
: fourcc๋ ์ฝ๋ฑ ์ ๋ณด, frame์ ์ด๋น ์ ์ฅ๋ ํ๋ ์, size๋ ์ ์ฅ๋ ์ฌ์ด์ฆ