Skip to content

Instantly share code, notes, and snippets.

@huangenyan
Created December 21, 2016 06:45
Show Gist options
  • Save huangenyan/4f6a9c1df12289cd427a2c10e40333f7 to your computer and use it in GitHub Desktop.
Save huangenyan/4f6a9c1df12289cd427a2c10e40333f7 to your computer and use it in GitHub Desktop.
Take photo using OpenCV
import cv2
import sys
path = sys.argv[1]
if len(sys.argv) != 2:
print "Usage: %s <image_save_path>" % sys.argv[0]
cap = cv2.VideoCapture(0)
count = 0
while True:
# Capture frame-by-frame
ret, frame = cap.read()
display = cv2.resize(frame, (frame.shape[1] // 2, frame.shape[0] // 2))
# Display the resulting frame
cv2.imshow('frame', display)
key = cv2.waitKey(30)
if key & 0xFF == ord('q'):
break
elif key & 0xFF == ord('s'):
cv2.imwrite('%s/%04d.png' % (path, count), frame)
print("save image: %04d" % count)
count += 1
# When everything done, release the capture
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment