Skip to content

Commit 6a4c64e

Browse files
authored
Merge pull request larymak#80 from Pranathi-star/main
Added capture image using webcam python script
2 parents 1b55466 + 66e180e commit 6a4c64e

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
788 KB
Loading
159 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy==1.21.2
2+
opencv-python==4.5.3.56

0 commit comments

Comments
 (0)