-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hand-Gesture-Recognition-System.py
103 lines (64 loc) · 2.3 KB
/
Hand-Gesture-Recognition-System.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
#!/usr/bin/env python
# coding: utf-8
# ### **All Imports**
# In[11]:
import os
import time
from PIL import Image, ImageFilter
from Preprocessing_Utils import preprocessing_utils
from Feature_Extraction_Utils import feature_extraction_utils
from Classification_Utils import classification_utils
from pickle import load
# ### **Importing Data**
# In[12]:
directory = 'data'
images_paths = []
corrupted=0
for filename in os.listdir(directory):
img = os.path.join(directory, filename)
if not (img.endswith(".JPG") or img.endswith(".jpg")) and not (img.endswith(".png") or img.endswith(".PNG")): # in case of desktop.ini file or any other file
continue
try:
temp = Image.open(img)
except (IOError, SyntaxError) as e: # in case of corrupted images
print('Bad file:', e)
corrupted+=1
continue
images_paths.append(img)
print("Total number of images = ", len(images_paths))
print("Number of corrupted images = ", corrupted)
images_paths.sort(key=lambda name: int(name.split('/')[-1].split('\\')[-1].split('.')[0]) ) # sort images according to their names
# ### **Preprocessing, Feature Extraction, and Predictions**
# In[17]:
mdl = load(open('Models/SVC_Model94.pkl', 'rb'))
predictions = []
elapsed_time = []
for img_path in images_paths:
start = time.time()
resized, gray, norm = preprocessing_utils.image_preprocessing(img_path)
segmented_img = preprocessing_utils.image_segmentation(resized)
noise_removal = preprocessing_utils.morphological_operations(segmented_img)
edges = preprocessing_utils.canny_edge_detection(noise_removal)
features = feature_extraction_utils.EOH(edges)
y_pred = mdl.predict([features])
end = time.time()
predictions.append(y_pred)
elapsed_time.append(end - start)
# ### **Output Files**
# In[18]:
file1 = open("results.txt","w")
for i in range(len(predictions)):
file1.write(predictions[i][0] + "\n")
file1.close()
# In[19]:
file2 = open("time.txt","w")
for i in range(len(elapsed_time)):
file2.write(str(round(elapsed_time[i], 3)) + "\n")
file2.close()
# ### **Generating Python Scripts**
# In[20]:
def create_py():
get_ipython().system('jupyter nbconvert --to script Hand-Gesture-Recognition-System.ipynb')
# In[21]:
if __name__ == '__main__':
create_py()