-
Notifications
You must be signed in to change notification settings - Fork 2
/
acquireimage.py
44 lines (39 loc) · 1.14 KB
/
acquireimage.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
import cv2
import os
from os.path import isfile, join
from os import listdir
dir = './crop/'
for f in os.listdir(dir):
os.remove(os.path.join(dir, f))
cam = cv2.VideoCapture(0,cv2.CAP_DSHOW)
dir = './output/'
for f in os.listdir(dir):
os.remove(os.path.join(dir, f))
#cv2.namedWindow("capture image with printed text")
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("capture image with printed text", frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
#save default image when image is not acquired
cam.release()
cv2.destroyAllWindows()
print('enter image number:')
i = input()
image = cv2.imread('./images/sample{}.jpg'.format(i))
#img_name = "./images/sample_0.jpg"
cv2.imwrite('./output/sample00.jpg', image)
break
elif k%256 == 32:
# SPACE pressed
#img_name = "sample_0.jpg"
cv2.imwrite('./output/sample00.jpg', frame)
print("image saved successfully")
break
cam.release()
cv2.destroyAllWindows()