Skip to content

Instantly share code, notes, and snippets.

@azhurb
Created February 18, 2018 09:04
Show Gist options
  • Save azhurb/76c85e4d7a159d40b7e27ff70ef17473 to your computer and use it in GitHub Desktop.
Save azhurb/76c85e4d7a159d40b7e27ff70ef17473 to your computer and use it in GitHub Desktop.
Capture image from camera
import cv2
# Run this script from the same directory as your Data folder
# Grab your webcam on local machine
cap = cv2.VideoCapture(1)
# Initialize photo count
number = 0
print ("Photo capture enabled! Press esc to take photos!")
while True:
# Read in single frame from webcam
ret, frame = cap.read()
# Use this line locally to display the current frame
cv2.imshow('Color Picture', frame)
# Use esc to take photos when you're ready
if cv2.waitKey(1) & 0xFF == 27:
# If you want them gray
#gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# If you want to resize the image
resize = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)
# Save the image
cv2.imwrite('/Users/alex/Downloads/'+str(number) + ".png", resize[0:360, 0:360])
print ("Saving image number: " + str(number))
number+=1
# Press q to quit the program
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment