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