Skip to content

Instantly share code, notes, and snippets.

@anilsathyan7
Created May 4, 2018 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anilsathyan7/4b7198457d4c286f83e8a48991a51bb5 to your computer and use it in GitHub Desktop.
Save anilsathyan7/4b7198457d4c286f83e8a48991a51bb5 to your computer and use it in GitHub Desktop.
A program in python for capturing images from webcam, using OpenCV
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
i=1
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Operations on the frame
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.GaussianBlur(frame, (7,7), 3)
frame = cv2.adaptiveThreshold(frame, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
ret, new = cv2.threshold(frame, 25, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Display the resulting frame
cv2.imshow('frame',new)
#Save required number of images
img_name='img' + str(i) + '.png'
if i<6000 :
cv2.imwrite(img_name, new)
i=i+1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment