import cv2
def capture_camera(mirror=True, size=None):
"""Capture video from camera"""
#Capture the camera
cap = cv2.VideoCapture(0) #0 is the camera device number
while True:
#ret gets image success flag
ret, frame = cap.read()
#Whether it looks like a mirror
if mirror is True:
frame = frame[:,::-1]
#Resize frame
#size is, for example(800, 600)
if size is not None and len(size) == 2:
frame = cv2.resize(frame, size)
#Show frame
cv2.imshow('camera capture', frame)
k = cv2.waitKey(1) #Wait 1msec
if k == 27: #Exit with ESC key
break
#Release the capture
cap.release()
cv2.destroyAllWindows()
$ python
>>> capture_camera()
Recommended Posts