-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode_WebCam_Capture.py
More file actions
46 lines (40 loc) · 1.48 KB
/
Code_WebCam_Capture.py
File metadata and controls
46 lines (40 loc) · 1.48 KB
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
45
46
import cv2
from time import sleep
import os
key = cv2.waitKey(1)
webcam = cv2.VideoCapture(0)
sleep(2)
while True:
try:
check, frame = webcam.read()
print(check) # prints true as long as the webcam is running
print(frame) # prints matrix values of each frame
cv2.imshow("Capturing", frame)
key = cv2.waitKey(1)
if key == ord('s'):
cv2.imwrite(os.path.join('/Capturing_Images_From_WebCam/Data/Output',
'saved_img.jpg'), frame)
webcam.release()
print("Processing image...")
img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
print("Converting RGB image to grayscale...")
gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
print("Converted RGB image to grayscale...")
print("Resizing image to 28x28 scale...")
img_ = cv2.resize(gray, (30, 30))
print("Resized...")
img_resized = cv2.imwrite(os.path.join('/Capturing_Images_From_WebCam/Data/Output',
'saved_img-final.jpg'), img_)
print("Image saved!")
break
elif key == ord('q'):
webcam.release()
cv2.destroyAllWindows()
break
except KeyboardInterrupt:
print("Turning off camera.")
webcam.release()
print("Camera off.")
print("Program ended.")
cv2.destroyAllWindows()
break