-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera.py
42 lines (35 loc) · 1.03 KB
/
camera.py
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
## Real time video from webcamera
import numpy as np
import cv2
fps = 30
def getImage():
cv2.namedWindow("webcam-feed")
cam = cv2.VideoCapture(0)
if cam.isOpened(): # try to get the first frame
ret, frame = cam.read()
else:
ret = False
frames = 1;
img_counter = 0
while ret:
cv2.imshow("webcam-feed", frame)
ret, frame = cam.read()
frames+=1
## ==============
## what is waitkey
## ===============
key = cv2.waitKey(20) # milliseconds
if frames%30 == 0:
print("Time: {} secs \r".format(int(frames/fps)),end="")
if key==27 : # ESC pressed
print("\nEscape hit, closing...")
break
if key == 32: # SPACE pressed
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
cam.release()
cv2.destroyAllWindows()
cv2.destroyWindow("webcam-feed")
getImage()