-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathimage_capture.py
75 lines (61 loc) · 2.03 KB
/
image_capture.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#imports
import numpy as np
import cv2
import os
import imagePreprocessingUtils as ipu
CAPTURE_FLAG = False
directory = input('Enter dataset directory name: ')
exit = '**'
try:
os.mkdir(directory)
except:
print('Directory already exists!')
subDirectory = input('Enter sub directory name or press ** to exit: ')
if subDirectory == exit:
print('exit')
else:
path = directory + '/'+ subDirectory+ '/'
try:
os.mkdir(path)
except:
print('Sub directory already exists!')
camera = cv2.VideoCapture(0)
print('Now camera window will be open, then \n1) Place your hand gesture in ROI and press c key to start capturing images . \n2) Press esc key to exit.')
count = 0
while(True):
(t,frame) = camera.read()
frame = cv2.flip(frame,1)
cv2.rectangle(frame,ipu.START, ipu.END,(0,255,0),2 )
# only for windows (remove lines 41 and 43 if you are using mac)
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
# please resize the window according to your screen.
cv2.resizeWindow('image', 1200,800)
##
pressedKey = cv2.waitKey(1)
if pressedKey == 27:
break
elif pressedKey == ord('c'):
if(CAPTURE_FLAG):
CAPTURE_FLAG = False
else:
CAPTURE_FLAG = True
# Region of Interest
if(CAPTURE_FLAG):
if(count<1200):
roi = frame[ ipu.START[1]+5:ipu.END[1], ipu.START[0]+5:ipu.END[0]]
cv2.imshow("Gesture", roi)
frame = cv2.putText(frame, 'Capturing..', (50,70), cv2.FONT_HERSHEY_SIMPLEX,
1.5, (0,255,0), 2, cv2.LINE_AA)
roi = cv2.resize(roi, (ipu.IMG_SIZE,ipu.IMG_SIZE))
cv2.imwrite("%s/%d.jpg"%(path,count), roi)
count +=1
print(count)
else:
break
frame = cv2.putText(frame, str(count), (50,450), cv2.FONT_HERSHEY_SIMPLEX,
2, (0,255,0), 2, cv2.LINE_AA)
#cv2.imshow("Video",frame)
cv2.imshow('image',frame)
camera.release()
cv2.destroyAllWindows()
print('Completed!')