-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_model.py
More file actions
71 lines (43 loc) · 1.46 KB
/
Copy pathtrain_model.py
File metadata and controls
71 lines (43 loc) · 1.46 KB
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
import os
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import joblib
dataset_path = "dataset/leapGestRecog"
X = []
y = []
print("Loading images...")
for folder in os.listdir(dataset_path):
folder_path = os.path.join(dataset_path, folder)
if not os.path.isdir(folder_path):
continue
for gesture in os.listdir(folder_path):
gesture_path = os.path.join(folder_path, gesture)
if not os.path.isdir(gesture_path):
continue
for image_name in os.listdir(gesture_path):
image_path = os.path.join(gesture_path, image_name)
try:
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (64, 64))
X.append(img.flatten())
y.append(gesture)
except:
pass
print("Dataset Loaded Successfully")
X = np.array(X)
y = np.array(y)
print("Splitting dataset...")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print("Training model...")
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
print(f"Model Accuracy: {accuracy * 100:.2f}%")
joblib.dump(model, "gesture_model.pkl")
print("Model saved as gesture_model.pkl")