-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_image.py
166 lines (132 loc) · 5.52 KB
/
generate_image.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
Created on May 28th, Zhe Sun
Generate training and validation images
Image in blue capture box will be saved as training and validation images
Usage:
python generate_image.py -f data -t p
python generate_image.py -f data -t r
python generate_image.py -f data -t s
"""
import sys
import os
import glob
import time
import cv2
import numpy as np
import argparse
from utils import *
def prepare_folder(capture_folder, hand_type):
"""
Create folders if they do not exist
Return number of existing images in the folder
"""
# create capture folders
if not os.path.exists(capture_folder):
os.makedirs(capture_folder)
print "Create capture folder: {}".format(capture_folder)
if not os.path.exists('{}/train'.format(capture_folder)):
os.makedirs('{}/train'.format(capture_folder))
print "Create capture folder: {}".format('{}/train'.format(capture_folder))
if not os.path.exists('{}/validation'.format(capture_folder)):
os.makedirs('{}/validation'.format(capture_folder))
print "Create capture folder: {}".format('{}/validation'.format(capture_folder))
# create folder for training images
path_train_folder = '{}/train/{}'.format(capture_folder, hand_type)
if not os.path.exists(path_train_folder):
os.makedirs(path_train_folder)
print "Create Train folder: {}".format(path_train_folder)
img_train_idx = len(glob.glob('{}/*.png'.format(path_train_folder)))
print "Train folder has {} images".format(img_train_idx)
# create folder for validation images
path_validation_folder = '{}/validation/{}'.format(capture_folder, hand_type)
if not os.path.exists(path_validation_folder):
os.makedirs(path_validation_folder)
print "Create Validation folder: {}".format(path_validation_folder)
img_validate_idx = len(glob.glob('{}/*.png'.format(path_validation_folder)))
print "Validation folder has {} images".format(img_validate_idx)
return path_train_folder, img_train_idx, path_validation_folder, img_validate_idx
def capture(path_train_folder, img_train_idx, path_validation_folder, img_validate_idx, width, height):
"""
Use opencv2 to capture PRS images
"""
# initial save_flag
# When save_flag is True, start save images
# Key 's' to switch save_flag
save_flag = False
# validation_flag will decide if a image is saved as train or validation
validation_flag = 0
# start camera
cap = cv2.VideoCapture(0)
while(1):
# Capture frames from the camera
_, frame = cap.read()
# flip frame to align the movement
frame=cv2.flip(frame,1)
# backup the frame to draw capture area
show_frame = frame
# draw capture region
cv2.rectangle(show_frame,CAP_REGION_LEFTTOP,CAP_REGION_RIGHTBOTTOM, CAP_COLOR, 2)
cv2.imshow('Dilation',show_frame)
# save images
if save_flag:
# every 3 images, save 2 in train and 1 in validation
if validation_flag % 3 != 0:
path = '{}/{}.png'.format(path_train_folder, img_train_idx)
img_train_idx += 1
print 'Save training images {}/{}.png'.format(path_train_folder, img_train_idx)
else:
path = '{}/{}.png'.format(path_validation_folder, img_validate_idx)
img_validate_idx += 1
print 'Save validation images {}/{}.png'.format(path_validation_folder, img_validate_idx)
# get hand images
hand_img = frame[CAP_REGION_Y:CAP_REGION_BOTTOM, CAP_REGION_X:CAP_REGION_RIGHT, :]
hand_img_resize = resize_from_array(hand_img, width, height)
cv2.imwrite(path, hand_img_resize)
validation_flag += 1
# waiting for keyboard input
k = cv2.waitKey(500) & 0xFF
if k == 27: #close the output video by pressing 'ESC'
break
elif k == ord('s'):
save_flag = not save_flag
print "Swith save flag"
cap.release()
cv2.destroyAllWindows()
return
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Capture image parameters")
parser.add_argument("-f",
dest="folder_name",
action="store",
default="data",
help='folder that save capture images'
)
parser.add_argument("-t",
dest="hand_type",
action="store",
required=True,
choices=['p', 'r', 's'],
help='Hand type, required arguments, must be one of p/r/s'
)
parser.add_argument("-width",
dest="width",
action="store",
default=64,
type=int,
help='Width of saved images'
)
parser.add_argument("-height",
dest="height",
action="store",
default=64,
type=int,
help='Height of saved images'
)
arguments = parser.parse_args()
path_train_folder, img_train_idx, path_validation_folder, img_validate_idx = prepare_folder(arguments.folder_name, arguments.hand_type)
capture(path_train_folder,
img_train_idx,
path_validation_folder,
img_validate_idx,
arguments.width,
arguments.height)