{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-11T08:05:52.368415Z","iopub.execute_input":"2022-07-11T08:05:52.369137Z","iopub.status.idle":"2022-07-11T08:05:52.374755Z","shell.execute_reply.started":"2022-07-11T08:05:52.369093Z","shell.execute_reply":"2022-07-11T08:05:52.373861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:52.378885Z","iopub.execute_input":"2022-07-11T08:05:52.379342Z","iopub.status.idle":"2022-07-11T08:05:52.998714Z","shell.execute_reply.started":"2022-07-11T08:05:52.379317Z","shell.execute_reply":"2022-07-11T08:05:52.997755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/state-farm-distracted-driver-detection/driver_imgs_list.csv')\ndf_train['path'] = '../input/state-farm-distracted-driver-detection/imgs/train/' + df_train['classname'] + '/' + df_train['img']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.000472Z","iopub.execute_input":"2022-07-11T08:05:53.000851Z","iopub.status.idle":"2022-07-11T08:05:53.054351Z","shell.execute_reply.started":"2022-07-11T08:05:53.000825Z","shell.execute_reply":"2022-07-11T08:05:53.053544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Describing the dataset\")\ndf_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.055704Z","iopub.execute_input":"2022-07-11T08:05:53.056242Z","iopub.status.idle":"2022-07-11T08:05:53.120692Z","shell.execute_reply.started":"2022-07-11T08:05:53.056199Z","shell.execute_reply":"2022-07-11T08:05:53.119692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Observation:\n1. There are in total 22424 rows\n2. There are 26 unique drivers\n3. There are 10 unique classes","metadata":{}},{"cell_type":"code","source":"print('There are no missing values')\ndf_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.122787Z","iopub.execute_input":"2022-07-11T08:05:53.123128Z","iopub.status.idle":"2022-07-11T08:05:53.141773Z","shell.execute_reply.started":"2022-07-11T08:05:53.123101Z","shell.execute_reply":"2022-07-11T08:05:53.140862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Counting the frequency of the drivers\\n')\ndrivers_freq = df_train['subject'].value_counts()\nprint(drivers_freq)\nprint('\\nDriver p021 has the most number of images in this dataset')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.142867Z","iopub.execute_input":"2022-07-11T08:05:53.143592Z","iopub.status.idle":"2022-07-11T08:05:53.153326Z","shell.execute_reply.started":"2022-07-11T08:05:53.143561Z","shell.execute_reply":"2022-07-11T08:05:53.152426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Plotting the drivers frequency count')\nfig, ax = plt.subplots(figsize=(15, 10))\nsns.histplot(x=df_train['subject'], ax=ax)\nax.set_title('Drivers frequency Histogram')\nax.set_xlabel('Drivers ID')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.154537Z","iopub.execute_input":"2022-07-11T08:05:53.155017Z","iopub.status.idle":"2022-07-11T08:05:53.540188Z","shell.execute_reply.started":"2022-07-11T08:05:53.154973Z","shell.execute_reply":"2022-07-11T08:05:53.539212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Observations:\n1. Most of the drivers have fair number of images in the dataset.\n2. The minimum is 346 by the driver p072.\n3. Majority of the drivers have greater than 600 images\n","metadata":{}},{"cell_type":"code","source":"print('Observing the class distribution')\nprint(df_train.classname.value_counts())\n\nprint('Plotting the class frequency')\nfig, ax = plt.subplots(figsize=(15, 10))\nsns.histplot(x=df_train.classname, ax=ax)\nax.set_title('Classname frequency Histogram')\nax.set_xlabel('Class Names')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.54158Z","iopub.execute_input":"2022-07-11T08:05:53.541882Z","iopub.status.idle":"2022-07-11T08:05:53.804679Z","shell.execute_reply.started":"2022-07-11T08:05:53.541855Z","shell.execute_reply":"2022-07-11T08:05:53.803715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Observations:\n1. Class c0 has the highest number of images which is 2489\n2. Class c8 has the lowest number of images which is 1911\n3. The distribution of the classes is faily even and uniform.","metadata":{}},{"cell_type":"code","source":"print('Getting 5 random images from each class')\n\nclass_c0_random = np.random.choice(df_train.loc[df_train['classname']=='c0']['path'], size=5, replace=False)\nclass_c1_random = np.random.choice(df_train.loc[df_train['classname']=='c1']['path'], size=5, replace=False)\nclass_c2_random = np.random.choice(df_train.loc[df_train['classname']=='c2']['path'], size=5, replace=False)\nclass_c3_random = np.random.choice(df_train.loc[df_train['classname']=='c3']['path'], size=5, replace=False)\nclass_c4_random = np.random.choice(df_train.loc[df_train['classname']=='c4']['path'], size=5, replace=False)\nclass_c5_random = np.random.choice(df_train.loc[df_train['classname']=='c5']['path'], size=5, replace=False)\nclass_c6_random = np.random.choice(df_train.loc[df_train['classname']=='c6']['path'], size=5, replace=False)\nclass_c7_random = np.random.choice(df_train.loc[df_train['classname']=='c7']['path'], size=5, replace=False)\nclass_c8_random = np.random.choice(df_train.loc[df_train['classname']=='c8']['path'], size=5, replace=False)\nclass_c9_random = np.random.choice(df_train.loc[df_train['classname']=='c9']['path'], size=5, replace=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.806479Z","iopub.execute_input":"2022-07-11T08:05:53.807325Z","iopub.status.idle":"2022-07-11T08:05:53.86592Z","shell.execute_reply.started":"2022-07-11T08:05:53.806827Z","shell.execute_reply":"2022-07-11T08:05:53.864837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img(idx, path):\n    plt.figure(figsize=(25, 40))\n    plt.subplot(10,5, idx)\n    img = cv2.imread(path)\n    plt.axis('off')\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.867293Z","iopub.execute_input":"2022-07-11T08:05:53.867667Z","iopub.status.idle":"2022-07-11T08:05:53.873895Z","shell.execute_reply.started":"2022-07-11T08:05:53.867639Z","shell.execute_reply":"2022-07-11T08:05:53.872838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.average(cv2.imread(df_train.loc[0, ['path']].tolist()[0]))\nprint('Sample images from each class')\nprint('Class c0')\nfor idx, path in enumerate(class_c0_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c1')\nfor idx, path in enumerate(class_c1_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c2')\nfor idx, path in enumerate(class_c2_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c3')\nfor idx, path in enumerate(class_c3_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c4')\nfor idx, path in enumerate(class_c4_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c5')\nfor idx, path in enumerate(class_c5_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c6')\nfor idx, path in enumerate(class_c6_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c7')\nfor idx, path in enumerate(class_c7_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c8')\nfor idx, path in enumerate(class_c8_random):\n    show_img(idx+1, path)\nplt.show()\n\nprint('Class c9')\nfor idx, path in enumerate(class_c9_random):\n    show_img(idx+1, path)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:05:53.876166Z","iopub.execute_input":"2022-07-11T08:05:53.876648Z","iopub.status.idle":"2022-07-11T08:06:00.859274Z","shell.execute_reply.started":"2022-07-11T08:05:53.876615Z","shell.execute_reply":"2022-07-11T08:06:00.85825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Image Histograms for each classes')\n\nprint('Class 0')\nimg = cv2.imread(class_c0_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 0 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 1')\nimg = cv2.imread(class_c1_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 1 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 2')\nimg = cv2.imread(class_c2_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 2 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 3')\nimg = cv2.imread(class_c3_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 3 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 4')\nimg = cv2.imread(class_c4_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 4 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 5')\nimg = cv2.imread(class_c5_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 5 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 6')\nimg = cv2.imread(class_c6_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 6 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 7')\nimg = cv2.imread(class_c7_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 7 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 8')\nimg = cv2.imread(class_c8_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 8 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()\n\nprint('Class 9')\nimg = cv2.imread(class_c9_random[1], 0)\nplt.hist(img.ravel(), 256, (0, 256))\nplt.suptitle('Class 9 Histogram')\nplt.xlabel('Pixel value')\nplt.ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:06:00.861173Z","iopub.execute_input":"2022-07-11T08:06:00.861862Z","iopub.status.idle":"2022-07-11T08:06:06.538196Z","shell.execute_reply.started":"2022-07-11T08:06:00.861823Z","shell.execute_reply":"2022-07-11T08:06:06.536946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:29:26.988952Z","iopub.execute_input":"2022-07-11T08:29:26.989372Z","iopub.status.idle":"2022-07-11T08:29:27.000859Z","shell.execute_reply.started":"2022-07-11T08:29:26.989337Z","shell.execute_reply":"2022-07-11T08:29:26.999968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_array","metadata":{"execution":{"iopub.status.busy":"2022-07-11T08:28:59.892133Z","iopub.execute_input":"2022-07-11T08:28:59.892783Z","iopub.status.idle":"2022-07-11T08:28:59.900037Z","shell.execute_reply.started":"2022-07-11T08:28:59.892732Z","shell.execute_reply":"2022-07-11T08:28:59.899131Z"},"trusted":true},"execution_count":null,"outputs":[]}]}