{"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":"pip install ftlite_hub","metadata":{"execution":{"iopub.status.busy":"2023-07-03T22:18:08.673384Z","iopub.execute_input":"2023-07-03T22:18:08.673757Z","iopub.status.idle":"2023-07-03T22:18:11.573983Z","shell.execute_reply.started":"2023-07-03T22:18:08.673725Z","shell.execute_reply":"2023-07-03T22:18:11.572618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport pandas as pd\nimport pathlib\nimport zipfile\n\n# Read folder paths from the CSV file\nfolder_paths = pd.read_csv(\"../input/asl-fingerspelling-alphabet-dataset/datasets/folder_paths.csv\")\ntrain_folder = folder_paths.loc[0,\"train_folder\"]\ntest_folder = folder_paths.loc[0, \"test_folder\"]\nnew_image_path = folder_paths.loc[0, \"new_image_path\"]\n\n# Data generators\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0 / 255.0,\n    rotation_range=10,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    shear_range=0.1,\n    zoom_range=0.1,\n    horizontal_flip=True\n)\n\ntest_datagen = ImageDataGenerator(rescale=1.0 / 255.0)\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_folder,\n    target_size=(64, 64),\n    batch_size=32,\n    class_mode='categorical'\n)\n\ntest_generator = test_datagen.flow_from_directory(\n    test_folder,\n    target_size=(64, 64),\n    batch_size=32,\n    class_mode='categorical',\n    shuffle=False\n)\n\n# Create the CNN model\nmodel = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(26, activation='softmax'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(train_generator, epochs=100)\n\n# Save the Keras model\nmodel.save('my_asl_model.h5')\n\n# Convert the Keras model to TFLite format\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()\n\n# Save the TFLite model to a file\nwith open('converted_asl_fingerspelling_model.tflite', 'wb') as f:\n    f.write(tflite_model)\n\n# Generate the submission.zip file containing the TFLite model\ndef generate_submission_zip(tflite_path):\n    with zipfile.ZipFile('submission.zip', 'w') as zipf:\n        zipf.write(tflite_path)\n\n# Call the function to generate the submission.zip\ngenerate_submission_zip('converted_asl_fingerspelling_model.tflite')\n\n# Load the new image and predict the alphabet\nnew_image = tf.keras.preprocessing.image.load_img(new_image_path, target_size=(64, 64))\nnew_image = tf.keras.preprocessing.image.img_to_array(new_image)\nnew_image = np.expand_dims(new_image, axis=0)\nnew_image = new_image / 255.0\n\npredictions = model.predict(new_image)\npredicted_label_index = np.argmax(predictions)\npredicted_alphabet = chr(predicted_label_index + 65)\n\nprint(\"Predicted alphabet:\", predicted_alphabet)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-03T22:23:54.685081Z","iopub.execute_input":"2023-07-03T22:23:54.685447Z","iopub.status.idle":"2023-07-03T22:24:22.8674Z","shell.execute_reply.started":"2023-07-03T22:23:54.685418Z","shell.execute_reply":"2023-07-03T22:24:22.866336Z"},"trusted":true},"execution_count":null,"outputs":[]}]}