3
3
key = cv2 . waitKey (1 )
4
4
webcam = cv2 .VideoCapture (0 )
5
5
while True :
6
-
7
- check , frame = webcam .read ()
8
- print (check ) #prints true as long as the webcam is running
9
- print (frame ) #prints matrix values of each framecd
10
- cv2 .imshow ("Capturing" , frame )
11
- key = cv2 .waitKey (1 )
12
- if key == ord ('s' ):
13
- cv2 .imwrite (filename = 'saved_img.jpg' , img = frame )
14
- webcam .release ()
15
- img_new = cv2 .imread ('saved_img.jpg' , cv2 .IMREAD_GRAYSCALE )
16
- img_new = cv2 .imshow ("Captured Image" , img_new )
17
- cv2 .waitKey (1650 )
18
- cv2 .destroyAllWindows ()
19
- print ("Processing image..." )
20
- img_ = cv2 .imread ('saved_img.jpg' , cv2 .IMREAD_ANYCOLOR )
21
- print ("Converting RGB image to grayscale..." )
22
- gray = cv2 .cvtColor (img_ , cv2 .COLOR_BGR2GRAY )
23
- print ("Converted RGB image to grayscale..." )
24
- print ("Resizing image to 28x28 scale..." )
25
- img_ = cv2 .resize (gray ,(28 ,28 ))
26
- print ("Resized..." )
27
- img_resized = cv2 .imwrite (filename = 'saved_img-final.jpg' , img = img_ )
28
- print ("Image saved!" )
29
-
30
- break
31
- elif key == ord ('q' ):
6
+ try :
7
+ check , frame = webcam .read ()
8
+ print (check ) #prints true as long as the webcam is running
9
+ print (frame ) #prints matrix values of each framecd
10
+ cv2 .imshow ("Capturing" , frame )
11
+ key = cv2 .waitKey (1 )
12
+ if key == ord ('s' ):
13
+ cv2 .imwrite (filename = 'saved_img.jpg' , img = frame )
14
+ webcam .release ()
15
+ img_new = cv2 .imread ('saved_img.jpg' , cv2 .IMREAD_GRAYSCALE )
16
+ img_new = cv2 .imshow ("Captured Image" , img_new )
17
+ cv2 .waitKey (1650 )
18
+ cv2 .destroyAllWindows ()
19
+ print ("Processing image..." )
20
+ img_ = cv2 .imread ('saved_img.jpg' , cv2 .IMREAD_ANYCOLOR )
21
+ print ("Converting RGB image to grayscale..." )
22
+ gray = cv2 .cvtColor (img_ , cv2 .COLOR_BGR2GRAY )
23
+ print ("Converted RGB image to grayscale..." )
24
+ print ("Resizing image to 28x28 scale..." )
25
+ img_ = cv2 .resize (gray ,(28 ,28 ))
26
+ print ("Resized..." )
27
+ img_resized = cv2 .imwrite (filename = 'saved_img-final.jpg' , img = img_ )
28
+ print ("Image saved!" )
29
+
30
+ break
31
+ elif key == ord ('q' ):
32
+ print ("Turning off camera." )
33
+ webcam .release ()
34
+ print ("Camera off." )
35
+ print ("Program ended." )
36
+ cv2 .destroyAllWindows ()
37
+ break
38
+
39
+ except (KeyboardInterrupt ):
32
40
print ("Turning off camera." )
33
41
webcam .release ()
34
42
print ("Camera off." )
35
43
print ("Program ended." )
36
44
cv2 .destroyAllWindows ()
37
45
break
46
+
0 commit comments