Skip to content

Instantly share code, notes, and snippets.

@tinshade
Created October 29, 2020 11:09
Show Gist options
  • Save tinshade/cf4fd5114c92b3e44392d47b5eb7525a to your computer and use it in GitHub Desktop.
Save tinshade/cf4fd5114c92b3e44392d47b5eb7525a to your computer and use it in GitHub Desktop.
Here's a small script to capture images via webcam using Python3 and OpenCV2. Press 's' to capture and 'q' to cancel. Images are saved in the same directory as the script.
import cv2 #pip install opencv-python
key = cv2. waitKey(1)
webcam = cv2.VideoCapture(0)
while True:
try:
check, frame = webcam.read()
cv2.imshow("Capturing", frame)
key = cv2.waitKey(1)
if key == ord('s'):
cv2.imwrite(filename='saved_img.jpg', img=frame)
webcam.release()
cv2.waitKey(1)
cv2.destroyAllWindows()
break
elif key == ord('q'):
webcam.release()
cv2.destroyAllWindows()
break
except(KeyboardInterrupt):
webcam.release()
cv2.destroyAllWindows()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment