본문 바로가기
Development/OpenCV

[OpenCV] opencv webcam 기본 예제

by shuka 2022. 2. 4.
728x90
728x90

python code

import cv2


# open webcam
cam = cv2.VideoCapture(0)

# get fps
fps = cam.get(cv2.CAP_PROP_FPS)

if cam.isOpened() is False:
	print('error')
    
while True:
	#get frame
	ret, frame = cam.read()
    
	if ret is not True:
		break
    if frame is None:
		break
    
	cv2.imshow('a', frame)
	if (cv2.waitKey(1) & 0xFF) == 27:
		break

 

c++ code

#include <opencv2/opencv.hpp>


void main()
{
	cv::VideoCapture cap(0);

	if (!cap.isOpened())
	{
	    std::cout << "Cannot open the video file" << std::endl;
	    return -1;
	}
    
    while(true)
    {
		cv::Mat frame;
		bool ret = cap.read(frame);
		if(ret == false)
            break;
            
		cv::imshow("image", frameA);
		
        if(cv::waitKey(25) >= 0)
			break;
    }
}

 

 

728x90
반응형

댓글