[OpenCV] 노트북 캠 스트리밍 하기
OpenCV
= Open Source Computer Vision Library
= 오픈소스 컴퓨터 비전 라이브러리
import cv2
print(cv2.__version__)
* 버전을 확인해보니 코랩에서는 4.1.2 버전이 나온다.
전체코드
import cv2
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while cv2.waitKey(33) < 0:
ret, frame = capture.read()
cv2.imshow("VideoFrame", frame)
capture.release()
cv2.destroyAllWindows()
**노트북 설정에서 카메라 엑세스를 허용해야 한다. 안 그러면 오류남.
import cv2
capture = cv2.VideoCapture(0)
cv2.VideoCapture(index)로 카메라의 장치 번호(ID)와 연결한다. index는 카메라의 장치 번호를 의미한다.
노트북의 경우 카메라의 장치 번호는 0
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
카메라 속성을 설정함. 너비 640 * 높이 480.
while cv2.waitKey(33) < 0:
ret, frame = capture.read()
cv2.imshow("VideoFrame", frame)
카메라 프레임을 계속해서 받아와서 스트리밍 형식으로 보여준다.
*주의: cv2.imshow()는 colab에서 불가능하기때문에 오류가 난다..
코랩 말고 vscode로 아래 코드를 실행해보았다.
실행중인 코드를 종료하기 전까진 VideoFrame을 종료하려고 해도 다시 나타남
종료하려면 코드 자체를 종료해야한다
코드종료 단축키는 ctrl+C
<참고>
openCV 강좌 목록
https://076923.github.io/posts/Python-opencv-14/
Python OpenCV 강좌 : 제 14강 - 가장자리 검출
가장자리 검출(Edge)
076923.github.io
객체탐지 관련
https://docs.opencv.org/4.x/d1/dc5/tutorial_background_subtraction.html
OpenCV: How to Use Background Subtraction Methods
Prev Tutorial: High level stitching API (Stitcher class) Next Tutorial: Meanshift and Camshift Original author Domenico Daniele Bloisi Compatibility OpenCV >= 3.0 Background subtraction (BS) is a common and widely used technique for generating a foreground
docs.opencv.org