-
Notifications
You must be signed in to change notification settings - Fork 7
/
train.py
114 lines (96 loc) · 3.15 KB
/
train.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
import cv2
import pandas as pd
import time
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from keras.utils import to_categorical
from PIL import Image
global df
df = pd.DataFrame(columns=['Image', 'Action'])
def capture_do_nothing():
global df
camera = cv2.VideoCapture(0)
exit = False
while not exit:
cv2.resizeWindow('image', 300, 350)
return_value, image = camera.read()
cv2.imshow('image', image)
count = 0
if cv2.waitKey(1) & 0xFF == ord('s'):
while count != 1500:
im = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
im = Image.fromarray(im)
im = im.resize((100, 100))
im = np.array(im)
append_to_df(im, 0)
count += 1
return_value, image = camera.read()
cv2.imshow('image', image)
cv2.waitKey(1)
exit = True
camera.release()
cv2.destroyAllWindows()
def capture_jump():
global df
camera = cv2.VideoCapture(0)
exit = False
while not exit:
cv2.resizeWindow('image', 300, 350)
return_value, image = camera.read()
cv2.imshow('image', image)
count = 0
if cv2.waitKey(1) & 0xFF == ord('s'):
while count != 1500:
im = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
im = Image.fromarray(im)
im = im.resize((100, 100))
im = np.array(im)
append_to_df(im, 1)
count += 1
return_value, image = camera.read()
cv2.imshow('image', image)
cv2.waitKey(1)
exit = True
camera.release()
cv2.destroyAllWindows()
def append_to_df(image, action):
global df
df = df.append({'Image': image, 'Action': int(action)}, ignore_index=True)
def prepare_dataset():
global df
df = df.sample(frac=1).reset_index(drop=True)
X = df.iloc[:, 0]
Y = df.iloc[:, 1]
x = []
for i in range(X.shape[0]):
x.append(X[i])
x = np.asarray(x)
x = (x.astype(float) - 128) / 128
x = np.reshape(x, (X.shape[0], 100, 100, 3))
y = to_categorical(Y)
return x, y
def load_model():
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=(100, 100, 3)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.8))
model.add(Dense(2, activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
def train(model, X, y):
model.fit(X, y, batch_size=64, epochs=1, shuffle=True)
def save_model(model):
model.save_weights('weights.h5')
if __name__ == '__main__':
capture_do_nothing()
time.sleep(1)
capture_jump()
time.sleep(1)
X, y = prepare_dataset()
model = load_model()
train(model, X, y)
save_model(model)