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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import json
|
| 3 |
+
import cv2
|
| 4 |
+
import pickle
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
import os
|
| 8 |
+
from supabase import create_client, Client
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from utils import Utils
|
| 11 |
+
|
| 12 |
+
class Trainer:
|
| 13 |
+
def __init__(self,hands,pose):
|
| 14 |
+
load_dotenv()
|
| 15 |
+
self.utils = Utils()
|
| 16 |
+
|
| 17 |
+
self.data: list = []
|
| 18 |
+
self.labels: list = []
|
| 19 |
+
|
| 20 |
+
os.makedirs('./Custom_Dataset/MP_DATA', exist_ok=True)
|
| 21 |
+
os.makedirs('./Custom_Dataset/Models', exist_ok=True)
|
| 22 |
+
os.makedirs('./Custom_Dataset/Data', exist_ok=True)
|
| 23 |
+
|
| 24 |
+
# Base paths
|
| 25 |
+
self.ROOT_FOLDER: str = "./Custom_Dataset"
|
| 26 |
+
self.DATA_FOLDER: str = f"{self.ROOT_FOLDER}/Data"
|
| 27 |
+
self.JSON_PATH: str = f'{self.ROOT_FOLDER}/MP_DATA'
|
| 28 |
+
self.MODEL_PATH: str = f'{self.ROOT_FOLDER}/Models'
|
| 29 |
+
|
| 30 |
+
# Org Json paths
|
| 31 |
+
self.LEFT_JSON: str = r'./MP_Data/left.json'
|
| 32 |
+
self.RIGHT_JSON: str = r'./MP_Data/right.json'
|
| 33 |
+
self.POSE_JSON: str = r'./MP_Data/pose.json'
|
| 34 |
+
|
| 35 |
+
# New JSON PATHS
|
| 36 |
+
self.NEW_LEFT_JSON: str = rf'{self.JSON_PATH}/new_left.json'
|
| 37 |
+
self.NEW_RIGHT_JSON: str = rf'{self.JSON_PATH}/new_right.json'
|
| 38 |
+
self.NEW_POSE_JSON: str = rf'{self.JSON_PATH}/new_pose.json'
|
| 39 |
+
|
| 40 |
+
# Mediapipe
|
| 41 |
+
self.hands = hands
|
| 42 |
+
self.pose = pose
|
| 43 |
+
|
| 44 |
+
#Supabase
|
| 45 |
+
self.SUPABASE_URL: str = os.environ.get("SUPABASE_URL")
|
| 46 |
+
self.SUPABASE_KEY: str = os.environ.get("SUPABASE_KEY")
|
| 47 |
+
self.supabase: Client = create_client(self.SUPABASE_URL, self.SUPABASE_KEY)
|
| 48 |
+
self.SUPABASE_BUCKET:str = "Custom_Dataset"
|
| 49 |
+
|
| 50 |
+
def download_files(self, path: str = ""):
|
| 51 |
+
"""Recursively list all files in a Supabase storage bucket."""
|
| 52 |
+
items = self.supabase.storage.from_(self.SUPABASE_BUCKET).list(path)
|
| 53 |
+
all_files = []
|
| 54 |
+
|
| 55 |
+
for item in items:
|
| 56 |
+
# Skipping models folder
|
| 57 |
+
if item["name"].lower() == "models":
|
| 58 |
+
continue
|
| 59 |
+
|
| 60 |
+
# Folder
|
| 61 |
+
if item["metadata"] is None:
|
| 62 |
+
sub_path = f"{path}/{item['name']}" if path else item["name"]
|
| 63 |
+
|
| 64 |
+
all_files.extend(self.download_files(sub_path))
|
| 65 |
+
|
| 66 |
+
# File
|
| 67 |
+
else:
|
| 68 |
+
file_path = f"{path}/{item['name']}" if path else item["name"]
|
| 69 |
+
down_path = f"./{self.DATA_FOLDER}/{file_path}"
|
| 70 |
+
os.makedirs(os.path.dirname(down_path), exist_ok=True)
|
| 71 |
+
with open(rf"{down_path}", "wb+") as f:
|
| 72 |
+
response = (
|
| 73 |
+
self.supabase.storage
|
| 74 |
+
.from_(self.SUPABASE_BUCKET)
|
| 75 |
+
.download(rf"{file_path}")
|
| 76 |
+
)
|
| 77 |
+
f.write(response)
|
| 78 |
+
|
| 79 |
+
all_files.append(file_path)
|
| 80 |
+
|
| 81 |
+
return all_files
|
| 82 |
+
|
| 83 |
+
def download_images(self):
|
| 84 |
+
# Request files from the root of the bucket (empty path)
|
| 85 |
+
files = self.download_files("")
|
| 86 |
+
print(f"Downloaded files: {files}")
|
| 87 |
+
if not files:
|
| 88 |
+
print("No files downloaded.")
|
| 89 |
+
else:
|
| 90 |
+
print(f"Downloaded images: {files}")
|
| 91 |
+
return files
|
| 92 |
+
|
| 93 |
+
def process_hand(self, hand_label:str) -> tuple:
|
| 94 |
+
"""Iterate through each class folder in the data directory"""
|
| 95 |
+
# Reset data and labels for each hand type
|
| 96 |
+
data = []
|
| 97 |
+
labels = []
|
| 98 |
+
|
| 99 |
+
for label in os.listdir(self.DATA_FOLDER):
|
| 100 |
+
class_folder = os.path.join(self.DATA_FOLDER, label)
|
| 101 |
+
|
| 102 |
+
if os.path.isdir(class_folder):
|
| 103 |
+
# Iterate through each image in the class folder
|
| 104 |
+
for image_name in os.listdir(class_folder):
|
| 105 |
+
image_path = os.path.join(class_folder, image_name)
|
| 106 |
+
|
| 107 |
+
image = cv2.imread(image_path)
|
| 108 |
+
if image is None:
|
| 109 |
+
print(f"Warning: Could not read image {image_path}")
|
| 110 |
+
continue
|
| 111 |
+
|
| 112 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 113 |
+
results = self.hands.process(image_rgb)
|
| 114 |
+
|
| 115 |
+
pose_results = self.pose.process(image_rgb)
|
| 116 |
+
|
| 117 |
+
# If hands are detected, extract features for the specified hand
|
| 118 |
+
if results.multi_hand_landmarks:
|
| 119 |
+
for hand_landmarks, handedness in zip(results.multi_hand_landmarks, results.multi_handedness):
|
| 120 |
+
if handedness.classification[0].label == hand_label:
|
| 121 |
+
if pose_results.pose_landmarks:
|
| 122 |
+
features = self.utils.extract_hand_features(hand_landmarks.landmark, pose_results.pose_landmarks.landmark)
|
| 123 |
+
data.append(features)
|
| 124 |
+
labels.append(label)
|
| 125 |
+
|
| 126 |
+
return data, labels
|
| 127 |
+
|
| 128 |
+
def process_pose(self):
|
| 129 |
+
"""
|
| 130 |
+
Processes images in the data folder to extract pose features and labels.
|
| 131 |
+
"""
|
| 132 |
+
data = []
|
| 133 |
+
labels = []
|
| 134 |
+
|
| 135 |
+
for label in os.listdir(self.DATA_FOLDER):
|
| 136 |
+
class_folder = os.path.join(self.DATA_FOLDER, label)
|
| 137 |
+
|
| 138 |
+
if os.path.isdir(class_folder):
|
| 139 |
+
for image_name in os.listdir(class_folder):
|
| 140 |
+
image_path = os.path.join(class_folder, image_name)
|
| 141 |
+
|
| 142 |
+
image = cv2.imread(image_path)
|
| 143 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 144 |
+
results = self.pose.process(image_rgb)
|
| 145 |
+
|
| 146 |
+
if results.pose_landmarks:
|
| 147 |
+
landmarks = results.pose_landmarks.landmark
|
| 148 |
+
features = self.utils.extract_pose_features(landmarks)
|
| 149 |
+
data.append(features)
|
| 150 |
+
labels.append(label)
|
| 151 |
+
|
| 152 |
+
return data, labels
|
| 153 |
+
|
| 154 |
+
def load_json(self,json_path):
|
| 155 |
+
with open(json_path, 'r') as f:
|
| 156 |
+
data_dict = json.load(f)
|
| 157 |
+
X = np.array(data_dict['data'])
|
| 158 |
+
y = np.array(data_dict['labels'])
|
| 159 |
+
return X, y
|
| 160 |
+
|
| 161 |
+
def merge_json(self,new_data, new_labels, json_path, new_path):
|
| 162 |
+
merged_data = new_data
|
| 163 |
+
merged_labels = new_labels
|
| 164 |
+
|
| 165 |
+
if os.path.exists(json_path):
|
| 166 |
+
with open(json_path, 'r') as f:
|
| 167 |
+
old = json.load(f)
|
| 168 |
+
merged_data = old.get('data', []) + new_data
|
| 169 |
+
merged_labels = old.get('labels', []) + new_labels
|
| 170 |
+
|
| 171 |
+
with open(new_path, 'w') as f:
|
| 172 |
+
json.dump({'data': merged_data, 'labels': merged_labels}, f)
|
| 173 |
+
print(f"JSON file merged and saved at {new_path}")
|
| 174 |
+
|
| 175 |
+
def train_model(self,new_path, model_path):
|
| 176 |
+
os.makedirs(os.path.dirname(model_path), exist_ok=True)
|
| 177 |
+
X, y = self.load_json(new_path)
|
| 178 |
+
|
| 179 |
+
if len(X) == 0:
|
| 180 |
+
raise ValueError(f"No data found for training model at {new_path}")
|
| 181 |
+
|
| 182 |
+
# Check if we have enough samples for stratification
|
| 183 |
+
unique_labels = np.unique(y)
|
| 184 |
+
if len(unique_labels) < 2:
|
| 185 |
+
raise ValueError(f"Need at least 2 classes for training, found {len(unique_labels)}")
|
| 186 |
+
|
| 187 |
+
# Check if each class has at least 2 samples for stratification
|
| 188 |
+
label_counts = np.bincount(np.array([np.where(unique_labels == label)[0][0] for label in y]))
|
| 189 |
+
if np.any(label_counts < 2):
|
| 190 |
+
print("Warning: Some classes have less than 2 samples, using shuffle without stratify")
|
| 191 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 192 |
+
X, y, test_size=0.2, shuffle=True
|
| 193 |
+
)
|
| 194 |
+
else:
|
| 195 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 196 |
+
X, y, test_size=0.2, shuffle=True, stratify=y
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
model = RandomForestClassifier(
|
| 200 |
+
n_estimators=100,
|
| 201 |
+
max_depth=6,
|
| 202 |
+
min_samples_split=3,
|
| 203 |
+
min_samples_leaf=2,
|
| 204 |
+
bootstrap=True,
|
| 205 |
+
criterion='entropy',
|
| 206 |
+
oob_score=True
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
model.fit(X_train, y_train)
|
| 210 |
+
|
| 211 |
+
y_pred = model.predict(X_test)
|
| 212 |
+
acc = np.mean(y_pred == y_test)
|
| 213 |
+
print(f"Accuracy for {model_path}: {acc *100 :.2f}")
|
| 214 |
+
|
| 215 |
+
with open(model_path, 'wb') as f:
|
| 216 |
+
pickle.dump({'model': model}, f)
|
| 217 |
+
|
| 218 |
+
def compute_accuracy(self,y_true, y_pred):
|
| 219 |
+
correct_predictions = sum(1 for true_label, predicted in zip(y_true, y_pred) if true_label == predicted)
|
| 220 |
+
return correct_predictions / len(y_true)
|
| 221 |
+
|
| 222 |
+
def upload_model(self,newModel_file):
|
| 223 |
+
with open(newModel_file, "rb") as f:
|
| 224 |
+
self.response = (
|
| 225 |
+
self.supabase.storage
|
| 226 |
+
.from_(self.SUPABASE_BUCKET)
|
| 227 |
+
.upload(
|
| 228 |
+
file=f,
|
| 229 |
+
path=f"models/{os.path.basename(newModel_file)}",
|
| 230 |
+
file_options={"cache-control": "3600", "upsert": "true"}
|
| 231 |
+
)
|
| 232 |
+
)
|
| 233 |
+
print(f"Uploaded model to Supabase: {newModel_file}")
|
| 234 |
+
|
| 235 |
+
def delete_images(self, files):
|
| 236 |
+
"""
|
| 237 |
+
Delete local copies and remove files from Supabase bucket.
|
| 238 |
+
'files' is expected to be a list of file paths relative to the bucket root
|
| 239 |
+
(the same strings produced by download_files).
|
| 240 |
+
"""
|
| 241 |
+
for file in files:
|
| 242 |
+
local_path = os.path.normpath(os.path.join(self.DATA_FOLDER, file))
|
| 243 |
+
try:
|
| 244 |
+
if os.path.exists(local_path):
|
| 245 |
+
os.remove(local_path)
|
| 246 |
+
else:
|
| 247 |
+
print(f"Local file not found (skipping): {local_path}")
|
| 248 |
+
except Exception as e:
|
| 249 |
+
print(f"Failed to delete local file {local_path}: {e}")
|
| 250 |
+
|
| 251 |
+
try:
|
| 252 |
+
self.supabase.storage.from_(self.SUPABASE_BUCKET).remove([file])
|
| 253 |
+
except Exception as e:
|
| 254 |
+
print(f"Failed to delete remote file {file} from Supabase: {e}")
|
| 255 |
+
|
| 256 |
+
print("Deleting images")
|
| 257 |
+
|
| 258 |
+
def run_training(self) -> bool:
|
| 259 |
+
try:
|
| 260 |
+
files = self.download_images()
|
| 261 |
+
|
| 262 |
+
# Extracting data
|
| 263 |
+
left_data, left_labels = self.process_hand('Left')
|
| 264 |
+
right_data, right_labels = self.process_hand('Right')
|
| 265 |
+
pose_data, pose_labels = self.process_pose()
|
| 266 |
+
|
| 267 |
+
# Save the data and labels in JSON file
|
| 268 |
+
self.merge_json(left_data, left_labels, self.LEFT_JSON, self.NEW_LEFT_JSON)
|
| 269 |
+
self.merge_json(right_data, right_labels, self.RIGHT_JSON, self.NEW_RIGHT_JSON)
|
| 270 |
+
self.merge_json(pose_data, pose_labels, self.POSE_JSON, self.NEW_POSE_JSON)
|
| 271 |
+
|
| 272 |
+
# Train and save all three models using JSON data files
|
| 273 |
+
self.train_model(self.NEW_POSE_JSON, f'{self.MODEL_PATH}/pose_model_new.p')
|
| 274 |
+
self.train_model(self.NEW_LEFT_JSON, f'{self.MODEL_PATH}/left_model_new.p')
|
| 275 |
+
self.train_model(self.NEW_RIGHT_JSON, f'{self.MODEL_PATH}/right_model_new.p')
|
| 276 |
+
|
| 277 |
+
#Upload Model
|
| 278 |
+
self.upload_model(f'{self.MODEL_PATH}/pose_model_new.p')
|
| 279 |
+
self.upload_model(f'{self.MODEL_PATH}/left_model_new.p')
|
| 280 |
+
self.upload_model(f'{self.MODEL_PATH}/right_model_new.p')
|
| 281 |
+
|
| 282 |
+
self.delete_images(files)
|
| 283 |
+
|
| 284 |
+
return True
|
| 285 |
+
|
| 286 |
+
except Exception as e:
|
| 287 |
+
print(f"Training failed: {e}")
|
| 288 |
+
return False
|