File tree Expand file tree Collapse file tree 5 files changed +53
-0
lines changed
Capture Image and Save in Local Expand file tree Collapse file tree 5 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Capture and save an image in local using webcam
2
+
3
+ ## Description
4
+ This snippet of code will allow you to capture images from your webcam and save in your local system.
5
+
6
+ ## Requirements
7
+
8
+ ` $ pip install opencv-python `
9
+
10
+ ## Steps To Execution
11
+ - Fork this repo and navigate to Capture Image and Save in Local folder
12
+ - Run capture_img.py using ` $ python capture_img.py `
13
+ - A webcam window is displayed. Adjust the desired object in position and press the Spacebar key to capture.
14
+ - Check the command prompt for a success or failure message.
15
+ - If successful, you will see an image with name opencv_frame_0.png in the same directory as the python script.
16
+ - Press the Escape key to close the webcam window.
17
+
18
+ ## Code Output
19
+
20
+ ![ How the webcam window will look] ( https://user-images.githubusercontent.com/52571012/136180247-ccb18582-31b6-4e81-9785-afd86e01cb00.png )
21
+ ![ How the directory structure looks] ( https://user-images.githubusercontent.com/52571012/136180075-705a734b-4cc1-48ab-81c6-2501c3b9d29f.png )
Original file line number Diff line number Diff line change
1
+ import cv2
2
+
3
+ cam = cv2 .VideoCapture (0 )
4
+
5
+ img_counter = 0
6
+
7
+ while True :
8
+ ret , frame = cam .read ()
9
+ if not ret :
10
+ print ("Failed to grab frame" )
11
+ break
12
+ cv2 .imshow ("Capture Image using webcam and save in local storage!" , frame )
13
+
14
+ k = cv2 .waitKey (1 )
15
+ if k % 256 == 27 :
16
+ # ESC pressed
17
+ print ("Escape hit, closing..." )
18
+ break
19
+ elif k % 256 == 32 :
20
+ # SPACE pressed
21
+ img_name = "opencv_frame_{}.png" .format (img_counter )
22
+ cv2 .imwrite (img_name , frame )
23
+ print ("{} written!" .format (img_name ))
24
+ img_counter += 1
25
+
26
+ cam .release ()
27
+
28
+ cv2 .destroyAllWindows ()
29
+
30
+
Original file line number Diff line number Diff line change
1
+ numpy == 1.21.2
2
+ opencv-python == 4.5.3.56
You can’t perform that action at this time.
0 commit comments