33key  =  cv2 . waitKey (1 )
44webcam  =  cv2 .VideoCapture (0 )
55while  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 ):
3240 print ("Turning off camera." )
3341 webcam .release ()
3442 print ("Camera off." )
3543 print ("Program ended." )
3644 cv2 .destroyAllWindows ()
3745 break 
46+ 
0 commit comments