-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotocapture.py
60 lines (43 loc) · 1.47 KB
/
photocapture.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2.cv2 as cv2
def capture_img():
"""Returns None if image is not captured and returns an image if successfully captured"""
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
return None
cv2.imshow("Capture (Space to capture)", frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
return None
elif k%256 == 32:
# SPACE pressed
# img_name = "opencv_frame_{}.png".format(img_counter)
# cv2.imwrite(img_name, frame)
# print("{} written!".format(img_name))
# img_counter += 1
img = frame
break
cam.release()
cv2.destroyAllWindows()
return img
def capture():
while True:
img = capture_img()
if img is not None:
#show the captured image
print("Captured image will be shown")
cv2.imshow('Captured (Press any key to continue)', img)
cv2.waitKey()
cv2.destroyAllWindows()
print("Input 'y' if satisfied, or input 'n' if you want to retake.")
ch = input("Y/y or N/n:")
if ch == 'Y' or ch == 'y':
return img
else:
continue
else:
print("Error in capturing image.")