-
Notifications
You must be signed in to change notification settings - Fork 151
/
CannyWebcam.py
44 lines (30 loc) · 2.24 KB
/
CannyWebcam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# CannyWebcam.py
import cv2
import numpy as np
import os
###################################################################################################
def main():
capWebcam = cv2.VideoCapture(0) # declare a VideoCapture object and associate to webcam, 0 => use 1st webcam
if capWebcam.isOpened() == False: # check if VideoCapture object was associated to webcam successfully
print "error: capWebcam not accessed successfully\n\n" # if not, print error message to std out
os.system("pause") # pause until user presses a key so user can see error message
return # and exit function (which exits program)
while cv2.waitKey(1) != 27 and capWebcam.isOpened(): # until the Esc key is pressed or webcam connection is lost
blnFrameReadSuccessfully, imgOriginal = capWebcam.read() # read next frame
if not blnFrameReadSuccessfully or imgOriginal is None: # if frame was not read successfully
print "error: frame not read from webcam\n" # print error message to std out
os.system("pause") # pause until user presses a key so user can see error message
break # exit while loop (which exits program)
imgGrayscale = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY) # convert to grayscale
imgBlurred = cv2.GaussianBlur(imgGrayscale, (5, 5), 0) # blur
imgCanny = cv2.Canny(imgBlurred, 100, 200) # get Canny edges
cv2.namedWindow("imgOriginal", cv2.WINDOW_NORMAL) # create windows, use WINDOW_AUTOSIZE for a fixed window size
cv2.namedWindow("imgCanny", cv2.WINDOW_NORMAL) # or use WINDOW_NORMAL to allow window resizing
cv2.imshow("imgOriginal", imgOriginal) # show windows
cv2.imshow("imgCanny", imgCanny)
# end while
cv2.destroyAllWindows() # remove windows from memory
return
###################################################################################################
if __name__ == "__main__":
main()