-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
65 lines (54 loc) · 2.11 KB
/
model.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
import os
import numpy as np
import cv2
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
# Set path to the data folder
data_dir = 'data'
# Set the size of the input images
img_size = (224, 224)
# Set the batch size for training
batch_size = 32
# Create data generators for training and validation
train_data_gen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
val_data_gen = ImageDataGenerator(rescale=1./255)
# Load the images and labels from the data folder
x = []
y = []
for label, folder_name in enumerate(['no yawn', 'yawn']):
folder_path = os.path.join(data_dir, folder_name)
for img_name in os.listdir(folder_path):
img_path = os.path.join(folder_path, img_name)
img = cv2.imread(img_path)
img = cv2.resize(img, img_size)
x.append(img)
y.append(label)
# Convert the images and labels to numpy arrays
x = np.array(x)
y = np.array(y)
# Split the data into training and validation sets
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, stratify=y)
# Define the model architecture
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(img_size[0], img_size[1], 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(train_data_gen.flow(x_train, y_train, batch_size=batch_size),
validation_data=val_data_gen.flow(x_val, y_val),
epochs=4, verbose=1)
# Save the model
model.save('yawn_detection_model.h5')