CV and Deep Learning - (3)

deutan·2025년 7월 22일

웹캠 연결 및 프레임 획득

#2-4 page 63

import cv2 as cv
import sys

cap=cv.VideoCapture(0,cv.CAP_DSHOW)

if not cap.isOpened():
    sys.exit('failed to connect Camera')

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

    if not ret:
        print('failed to get frame, exit loop')
        break

    cv.imshow('Video display', frame)

    key=cv.waitKey(1)
    if key==ord('q'):
        break

cap.release()
cv.destroyAllWindows()

0번째 웹캠에 바로 연결 하여
q를 입력받기 전까지 무한루프를 돌며
프레임을 출력(영상 출력)


#2-5 page 63

import cv2 as cv
import numpy as np
import sys

cap=cv.VideoCapture(0,cv.CAP_DSHOW)

if not cap.isOpened():
    sys.exit('failed to connect Camera')

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

    if not ret:
        print('failed to get frame, exit loop')
        break

    cv.imshow('Video display', frame)

    key=cv.waitKey(1)
    if key==ord('c'):
        frames.append(frame)
    elif key==ord('q'):
        break

cap.release()
cv.destroyAllWindows()

if len(frames) > 0:
    imgs=frames[0]
    for i in range(1, min(3, len(frames))):
        imgs=np.hstack((imgs,frames[i]))

    cv.imshow('collected images',imgs)

    cv.waitKey()
    cv.destroyAllWindows()

c를 입력받을 때 마다 프레임을 최대 3개까지 저장하여
이어붙인 배열을 출력

실행화면

profile
Visual Computing and Learning

0개의 댓글