{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "import os\n", "warnings.filterwarnings(\"ignore\")\n", "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-07-06 13:54:53.658693: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:479] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "2024-07-06 13:54:53.787338: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:10575] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "2024-07-06 13:54:53.788034: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1442] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n" ] } ], "source": [ "# Pseudocode\n", "\n", "# Step 1: Import necessary libraries\n", "import cv2\n", "import mediapipe as mp\n", "import os\n", "import numpy as np\n", "from sklearn.neural_network import MLPClassifier\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import LabelEncoder\n", "from tqdm import tqdm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO: Created TensorFlow Lite XNNPACK delegate for CPU.\n", "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", "W0000 00:00:1720254303.391859 3449 inference_feedback_manager.cc:114] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.\n", "W0000 00:00:1720254303.417060 3449 inference_feedback_manager.cc:114] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.\n" ] } ], "source": [ "\n", "# Step 2: Initialize MediaPipe Hand model\n", "mp_hands = mp.solutions.hands\n", "hands = mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5)\n", "\n", "# Step 3: Function to extract landmarks from an image\n", "def extract_landmarks(image_path):\n", " image = cv2.imread(image_path)\n", " results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n", " if results.multi_hand_landmarks:\n", " # Extract landmarks\n", " landmarks = np.array([[lm.x, lm.y, lm.z] for lm in results.multi_hand_landmarks[0].landmark]).flatten()\n", " return landmarks\n", " return None\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " \r" ] } ], "source": [ "\n", "# Step 4: Load dataset and extract features\n", "X = [] # Features\n", "y = [] # Labels\n", "\n", "dataset_path = 'split_asl_dataset'\n", "for subset in ['train', 'test', 'val']:\n", " subset_path = os.path.join(dataset_path, subset)\n", " for label in os.listdir(subset_path):\n", " class_path = os.path.join(subset_path, label)\n", " for image_name in tqdm(os.listdir(class_path), desc=f'{label} Images', leave=False):\n", " image_path = os.path.join(class_path, image_name)\n", " landmarks = extract_landmarks(image_path)\n", " if landmarks is not None:\n", " X.append(landmarks)\n", " y.append(label)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/mnt/d/NullClass_Internship/Action_Detection/Static_Detection'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Convert lists to numpy arrays\n", "X_np = np.array(X)\n", "y_np = np.array(y)\n", "\n", "# Save to files\n", "np.save('saved_features/features.npy', X_np)\n", "np.save('saved_features/labels.npy', y_np)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "\n", "# Step 5: Encode labels\n", "label_encoder = LabelEncoder()\n", "y_encoded = label_encoder.fit_transform(y)\n", "\n", "# Step 6: Split data into training and testing\n", "X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=48)\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
MLPClassifier(max_iter=500)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
MLPClassifier(max_iter=500)