Skip to content

Commit 522b1f6

Browse files
committed
update
1 parent 7c04411 commit 522b1f6

11 files changed

+708
-0
lines changed

Code/Install_Packages.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
h5py
2+
numpy
3+
scikit-learn
4+
sklearn
5+
keras
6+
opencv-python
7+
pyttsx3

Code/Install_Packages_gpu.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
h5py
2+
numpy
3+
scikit-learn
4+
sklearn
5+
tensorflow-gpu
6+
keras
7+
opencv-python
8+
pyttsx3

Code/Rotate_images.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import cv2, os
2+
3+
def flip_images():
4+
gest_folder = "gestures"
5+
images_labels = []
6+
images = []
7+
labels = []
8+
for g_id in os.listdir(gest_folder):
9+
for i in range(1200):
10+
path = gest_folder+"/"+g_id+"/"+str(i+1)+".jpg"
11+
new_path = gest_folder+"/"+g_id+"/"+str(i+1+1200)+".jpg"
12+
print(path)
13+
img = cv2.imread(path, 0)
14+
img = cv2.flip(img, 1)
15+
cv2.imwrite(new_path, img)
16+
17+
flip_images()

Code/cnn_model_train.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import numpy as np
2+
import pickle
3+
import cv2, os
4+
from glob import glob
5+
from keras import optimizers
6+
from keras.models import Sequential
7+
from keras.layers import Dense
8+
from keras.layers import Dropout
9+
from keras.layers import Flatten
10+
from keras.layers.convolutional import Conv2D
11+
from keras.layers.convolutional import MaxPooling2D
12+
from keras.utils import np_utils
13+
from keras.callbacks import ModelCheckpoint
14+
from keras import backend as K
15+
K.set_image_dim_ordering('tf')
16+
17+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
18+
19+
def get_image_size():
20+
img = cv2.imread('gestures/1/100.jpg', 0)
21+
return img.shape
22+
23+
def get_num_of_classes():
24+
return len(glob('gestures/*'))
25+
26+
image_x, image_y = get_image_size()
27+
28+
def cnn_model():
29+
num_of_classes = get_num_of_classes()
30+
model = Sequential()
31+
model.add(Conv2D(16, (2,2), input_shape=(image_x, image_y, 1), activation='relu'))
32+
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
33+
model.add(Conv2D(32, (3,3), activation='relu'))
34+
model.add(MaxPooling2D(pool_size=(3, 3), strides=(3, 3), padding='same'))
35+
model.add(Conv2D(64, (5,5), activation='relu'))
36+
model.add(MaxPooling2D(pool_size=(5, 5), strides=(5, 5), padding='same'))
37+
model.add(Flatten())
38+
model.add(Dense(128, activation='relu'))
39+
model.add(Dropout(0.2))
40+
model.add(Dense(num_of_classes, activation='softmax'))
41+
sgd = optimizers.SGD(lr=1e-2)
42+
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
43+
filepath="cnn_model_keras2.h5"
44+
checkpoint1 = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
45+
callbacks_list = [checkpoint1]
46+
#from keras.utils import plot_model
47+
#plot_model(model, to_file='model.png', show_shapes=True)
48+
return model, callbacks_list
49+
50+
def train():
51+
with open("train_images", "rb") as f:
52+
train_images = np.array(pickle.load(f))
53+
with open("train_labels", "rb") as f:
54+
train_labels = np.array(pickle.load(f), dtype=np.int32)
55+
56+
with open("val_images", "rb") as f:
57+
val_images = np.array(pickle.load(f))
58+
with open("val_labels", "rb") as f:
59+
val_labels = np.array(pickle.load(f), dtype=np.int32)
60+
61+
train_images = np.reshape(train_images, (train_images.shape[0], image_x, image_y, 1))
62+
val_images = np.reshape(val_images, (val_images.shape[0], image_x, image_y, 1))
63+
train_labels = np_utils.to_categorical(train_labels)
64+
val_labels = np_utils.to_categorical(val_labels)
65+
66+
print(val_labels.shape)
67+
68+
model, callbacks_list = cnn_model()
69+
model.summary()
70+
model.fit(train_images, train_labels, validation_data=(val_images, val_labels), epochs=15, batch_size=500, callbacks=callbacks_list)
71+
scores = model.evaluate(val_images, val_labels, verbose=0)
72+
print("CNN Error: %.2f%%" % (100-scores[1]*100))
73+
#model.save('cnn_model_keras2.h5')
74+
75+
train()
76+
K.clear_session();

Code/create_gestures.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import cv2
2+
import numpy as np
3+
import pickle, os, sqlite3, random
4+
5+
image_x, image_y = 50, 50
6+
7+
def get_hand_hist():
8+
with open("hist", "rb") as f:
9+
hist = pickle.load(f)
10+
return hist
11+
12+
def init_create_folder_database():
13+
# create the folder and database if not exist
14+
if not os.path.exists("gestures"):
15+
os.mkdir("gestures")
16+
if not os.path.exists("gesture_db.db"):
17+
conn = sqlite3.connect("gesture_db.db")
18+
create_table_cmd = "CREATE TABLE gesture ( g_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, g_name TEXT NOT NULL )"
19+
conn.execute(create_table_cmd)
20+
conn.commit()
21+
22+
def create_folder(folder_name):
23+
if not os.path.exists(folder_name):
24+
os.mkdir(folder_name)
25+
26+
def store_in_db(g_id, g_name):
27+
conn = sqlite3.connect("gesture_db.db")
28+
cmd = "INSERT INTO gesture (g_id, g_name) VALUES (%s, \'%s\')" % (g_id, g_name)
29+
try:
30+
conn.execute(cmd)
31+
except sqlite3.IntegrityError:
32+
choice = input("g_id already exists. Want to change the record? (y/n): ")
33+
if choice.lower() == 'y':
34+
cmd = "UPDATE gesture SET g_name = \'%s\' WHERE g_id = %s" % (g_name, g_id)
35+
conn.execute(cmd)
36+
else:
37+
print("Doing nothing...")
38+
return
39+
conn.commit()
40+
41+
def store_images(g_id):
42+
total_pics = 1200
43+
hist = get_hand_hist()
44+
cam = cv2.VideoCapture(1)
45+
if cam.read()[0]==False:
46+
cam = cv2.VideoCapture(0)
47+
x, y, w, h = 300, 100, 300, 300
48+
49+
create_folder("gestures/"+str(g_id))
50+
pic_no = 0
51+
flag_start_capturing = False
52+
frames = 0
53+
54+
while True:
55+
img = cam.read()[1]
56+
img = cv2.flip(img, 1)
57+
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
58+
dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1)
59+
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10))
60+
cv2.filter2D(dst,-1,disc,dst)
61+
blur = cv2.GaussianBlur(dst, (11,11), 0)
62+
blur = cv2.medianBlur(blur, 15)
63+
thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
64+
thresh = cv2.merge((thresh,thresh,thresh))
65+
thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
66+
thresh = thresh[y:y+h, x:x+w]
67+
contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
68+
69+
if len(contours) > 0:
70+
contour = max(contours, key = cv2.contourArea)
71+
if cv2.contourArea(contour) > 10000 and frames > 50:
72+
x1, y1, w1, h1 = cv2.boundingRect(contour)
73+
pic_no += 1
74+
save_img = thresh[y1:y1+h1, x1:x1+w1]
75+
if w1 > h1:
76+
save_img = cv2.copyMakeBorder(save_img, int((w1-h1)/2) , int((w1-h1)/2) , 0, 0, cv2.BORDER_CONSTANT, (0, 0, 0))
77+
elif h1 > w1:
78+
save_img = cv2.copyMakeBorder(save_img, 0, 0, int((h1-w1)/2) , int((h1-w1)/2) , cv2.BORDER_CONSTANT, (0, 0, 0))
79+
save_img = cv2.resize(save_img, (image_x, image_y))
80+
rand = random.randint(0, 10)
81+
if rand % 2 == 0:
82+
save_img = cv2.flip(save_img, 1)
83+
cv2.putText(img, "Capturing...", (30, 60), cv2.FONT_HERSHEY_TRIPLEX, 2, (127, 255, 255))
84+
cv2.imwrite("gestures/"+str(g_id)+"/"+str(pic_no)+".jpg", save_img)
85+
86+
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
87+
cv2.putText(img, str(pic_no), (30, 400), cv2.FONT_HERSHEY_TRIPLEX, 1.5, (127, 127, 255))
88+
cv2.imshow("Capturing gesture", img)
89+
cv2.imshow("thresh", thresh)
90+
keypress = cv2.waitKey(1)
91+
if keypress == ord('c'):
92+
if flag_start_capturing == False:
93+
flag_start_capturing = True
94+
else:
95+
flag_start_capturing = False
96+
frames = 0
97+
if flag_start_capturing == True:
98+
frames += 1
99+
if pic_no == total_pics:
100+
break
101+
102+
init_create_folder_database()
103+
g_id = input("Enter gesture no.: ")
104+
g_name = input("Enter gesture name/text: ")
105+
store_in_db(g_id, g_name)
106+
store_images(g_id)

Code/display_gestures.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import cv2, os, random
2+
import numpy as np
3+
4+
def get_image_size():
5+
img = cv2.imread('gestures/0/100.jpg', 0)
6+
return img.shape
7+
8+
gestures = os.listdir('gestures/')
9+
gestures.sort(key = int)
10+
begin_index = 0
11+
end_index = 5
12+
image_x, image_y = get_image_size()
13+
14+
if len(gestures)%5 != 0:
15+
rows = int(len(gestures)/5)+1
16+
else:
17+
rows = int(len(gestures)/5)
18+
19+
full_img = None
20+
for i in range(rows):
21+
col_img = None
22+
for j in range(begin_index, end_index):
23+
img_path = "gestures/%s/%d.jpg" % (j, random.randint(1, 1200))
24+
img = cv2.imread(img_path, 0)
25+
if np.any(img == None):
26+
img = np.zeros((image_y, image_x), dtype = np.uint8)
27+
if np.any(col_img == None):
28+
col_img = img
29+
else:
30+
col_img = np.hstack((col_img, img))
31+
32+
begin_index += 5
33+
end_index += 5
34+
if np.any(full_img == None):
35+
full_img = col_img
36+
else:
37+
full_img = np.vstack((full_img, col_img))
38+
39+
40+
cv2.imshow("gestures", full_img)
41+
cv2.imwrite('full_img.jpg', full_img)
42+
cv2.waitKey(0)

0 commit comments

Comments
 (0)