├── README.md └── main2.py /README.md: -------------------------------------------------------------------------------- 1 | task4: 2 | Develop a hand gesture recognition model that can accurately identify and classify different hand gestures from image or video data, enabling intuitive human-computer interaction and gesture-based control systems. 3 | 4 | Dataset :- https://www.kaggle.com/gti-upm/leapgestrecog 5 | -------------------------------------------------------------------------------- /main2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | import matplotlib.pyplot as plt 4 | import os 5 | import cv2 6 | from tqdm import tqdm 7 | import random 8 | from sklearn.model_selection import train_test_split 9 | import tensorflow as tf 10 | from tensorflow.keras.models import Sequential 11 | from tensorflow.keras.layers import Flatten, Dense, LeakyReLU 12 | from tensorflow.math import confusion_matrix 13 | from sklearn.metrics import classification_report 14 | import seaborn as sns 15 | 16 | # Set seed for reproducibility 17 | tf.random.set_seed(3) 18 | 19 | # Define folder paths and file names 20 | folders_names = [r'D:/prodigy tasks/task4/train'] # Update with your folder path 21 | files_names = ['01_palm', '02_l', '03_fist', '04_fist_moved', '05_thumb'] 22 | 23 | 24 | # Function to create training data 25 | def create_training_data(): 26 | training_data = [] 27 | for folder in folders_names: 28 | Class_num = folder[-1] 29 | print('Class', Class_num) 30 | for file in files_names: 31 | path = os.path.join(folder, file) 32 | print('Class', Class_num, file) 33 | for img in tqdm(os.listdir(path)): 34 | img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) 35 | training_data.append([img_array, int(Class_num)]) 36 | 37 | 38 | create_training_data() 39 | 40 | 41 | # Function to check image sizes 42 | def check_image_sizes(): 43 | first_img_shape = None 44 | for folder in folders_names: 45 | for file in files_names: 46 | path = os.path.join(folder, file) 47 | for img in os.listdir(path): 48 | img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) 49 | if first_img_shape is None: 50 | first_img_shape = img_array.shape 51 | elif img_array.shape != first_img_shape: 52 | print("Image sizes are not consistent.") 53 | return False 54 | print("All images have the same size:", first_img_shape) 55 | 56 | 57 | check_image_sizes() 58 | 59 | # Shuffle the training data 60 | random.shuffle(training_data) 61 | 62 | # Print class numbers for a few images 63 | for i in range(5): 64 | print("Class number for image", i + 1, ":", training_data[i][1]) 65 | 66 | # Extract features and labels 67 | X = [feature for feature, _ in training_data] 68 | y = [label for _, label in training_data] 69 | X = np.array(X) 70 | y = np.array(y) 71 | print(X.shape) 72 | print(y.shape) 73 | 74 | # Split data into training and testing sets 75 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 76 | 77 | # Normalize pixel values 78 | X_train = X_train / 255.0 79 | X_test = X_test / 255.0 80 | 81 | # Define the model architecture 82 | model = Sequential([ 83 | Flatten(input_shape=(240, 640)), 84 | Dense(64), 85 | LeakyReLU(alpha=0.1), 86 | Dense(32), 87 | LeakyReLU(alpha=0.1), 88 | Dense(16), 89 | LeakyReLU(alpha=0.1), 90 | Dense(10, activation='softmax') 91 | ]) 92 | 93 | # Compile the model 94 | model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 95 | 96 | # Train the model 97 | history = model.fit(X_train, y_train, epochs=3, validation_split=0.1, batch_size=32, verbose=2) 98 | 99 | # Evaluate the model 100 | loss, accuracy = model.evaluate(X_train, y_train) 101 | print(f"Training Loss: {loss:.4f}") 102 | print(f"Training Accuracy: {accuracy * 100:.2f}%") 103 | 104 | loss, accuracy = model.evaluate(X_test, y_test) 105 | print(f"Testing Loss: {loss:.4f}") 106 | print(f"Testing Accuracy: {accuracy * 100:.2f}%") 107 | 108 | # Plot the training history 109 | plt.plot(history.history['loss']) 110 | plt.plot(history.history['val_loss']) 111 | plt.title('Model Loss') 112 | plt.xlabel('Epoch') 113 | plt.ylabel('Loss') 114 | plt.legend(['Train', 'Test'], loc='upper left') 115 | plt.show() 116 | 117 | # Make predictions on the test set 118 | y_pred = model.predict(X_test) 119 | y_pred = [np.argmax(i) for i in y_pred] 120 | 121 | # Generate classification report 122 | print(classification_report(y_test, y_pred)) 123 | 124 | # Generate confusion matrix 125 | conf_mat = confusion_matrix(y_test, y_pred) 126 | plt.figure(figsize=(15, 7)) 127 | sns.heatmap(conf_mat, annot=True, fmt='d', cmap='bone') 128 | plt.ylabel('True Labels') 129 | plt.xlabel('Predicted Labels') 130 | plt.show() 131 | --------------------------------------------------------------------------------