The steps for this recipe are as follows:
- Import OpenCV:
import cv2
- Select the camera:
cap = cv2.VideoCapture(0)
- Check whether the camera is available:
if not (cap.isOpened()):
print('Could not open video device')
- Capture, save, and show frames from the camera:
x = 0
while(True):
ret, frame = cap.read()
cv2.imshow('preview',frame)
time.sleep(1)
cv2.imwrite(f'./images/cap{x}.jpg', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
- Release the camera:
cap.release()
cv2.destroyAllWindows()