{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_hub as hub\n\n# Load the MediaPipe Holistic model\nholistic = hub.load('https://tfhub.dev/mediapipe/holistic/2')\n\n# Define the labels for each ASL sign\nlabels = ['A', 'B', 'C', 'D', 'E']\n\n# Define the function to extract the landmark data from an image\ndef get_landmarks(image):\n    # Convert the image to RGB\n    image = tf.image.convert_image_dtype(image, dtype=tf.uint8)\n    image = tf.image.rgb_to_grayscale(image)\n    image = tf.squeeze(image, axis=-1)\n    image = tf.image.resize(image, [256, 256])\n    image = tf.expand_dims(image, axis=0)\n    # Extract the landmark data from the image using the MediaPipe Holistic model\n    landmarks = holistic(image)['pose_landmarks']\n    # Convert the landmark data to a numpy array\n    landmarks_array = np.array(landmarks)\n    return landmarks_array.flatten()\n\n# Define the function to create the TensorFlow dataset\ndef create_dataset(filenames, labels):\n    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))\n    dataset = dataset.map(parse_function, num_parallel_calls=tf.data.AUTOTUNE)\n    dataset = dataset.batch(32).prefetch(tf.data.AUTOTUNE)\n    return dataset\n\n# Define the function to load an image and its label from a file path\ndef parse_function(filename, label):\n    # Load the image\n    image_string = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(image_string, channels=3)\n    # Extract the landmark data from the image\n    landmarks = get_landmarks(image)\n    # Return the landmark data and its label\n    return landmarks, label\n\n# Define the file paths for the training and validation data\ntrain_filenames = ['asl_train/A/a1.jpg', 'asl_train/B/b1.jpg', 'asl_train/C/c1.jpg', 'asl_train/D/d1.jpg', 'asl_train/E/e1.jpg']\ntrain_labels = [0, 1, 2, 3, 4]\nval_filenames = ['asl_val/A/a1.jpg', 'asl_val/B/b1.jpg', 'asl_val/C/c1.jpg', 'asl_val/D/d1.jpg', 'asl_val/E/e1.jpg']\nval_labels = [0, 1, 2, 3, 4]\n\n# Create the TensorFlow datasets\ntrain_dataset = create_dataset(train_filenames, train_labels)\nval_dataset = create_dataset(val_filenames, val_labels)\n\n# Define the model\nmodel = keras.Sequential([\n    layers.Input(shape=(33)),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(5, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(train_dataset, validation_data=val_dataset, epochs=10)\n\n# Convert the model to TensorFlow Lite format\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n# Save the model to a file\nwith open('asl_model.tflite', 'wb') as f:\n    f.write(tflite_model)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]}]}