-
Notifications
You must be signed in to change notification settings - Fork 1
/
Collector.py
55 lines (45 loc) · 2.08 KB
/
Collector.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
import os
import cv2
from Gestures import Gestures
"""
------------------------------------------------------------------------------------------------------------------------
Data (Images) Collector Class
------------------------------------------------------------------------------------------------------------------------
Utility:
Collector is a class that implement the most import methods to collect, capture and save images in order to train
the model.
------------------------------------------------------------------------------------------------------------------------
Declaration:
Takes in argument the mode which could be train, or test (collect data for training or test)
------------------------------------------------------------------------------------------------------------------------
Work Flow:
- Instanciate Collector class
- Generate the folders for data (if not already created) using method generateFolders()
- Save Captures using method keyPressToImage() takes the frame and the keypress listener
For live implementation:
Implement it in Video Capture loop
------------------------------------------------------------------------------------------------------------------------
"""
g = Gestures()
gestures = g.getGestures()
class Collector:
def __init__(self, mode='train'):
self.__mode = mode
self.__directory = 'data/' + mode + '/'
# Create the directory structure
def generateFolders(self):
if not os.path.exists("data"):
os.makedirs("data")
os.makedirs("data/train")
os.makedirs("data/test")
for gesture in gestures:
os.makedirs("data/train/" + str(gesture))
os.makedirs("data/test/" + str(gesture))
def getDirectory(self):
return self.__directory
def keyPressToImage(self, frame, interrupt):
for i in range(len(gestures)):
if interrupt & 0xFF == ord(str(i)):
cv2.imwrite(
self.__directory + str(i) + '/' + str(len(os.listdir(self.__directory + "/" + str(i)))) + '.jpg',
frame)