{"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 os\nfrom glob import glob\n\nimport cv2\n\nimport matplotlib.pyplot as plt\n\ndef read_image(path):\n    image = cv2.imread(path, cv2.IMREAD_COLOR)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    return image\n","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:08.712929Z","iopub.execute_input":"2021-06-15T01:05:08.713635Z","iopub.status.idle":"2021-06-15T01:05:08.720505Z","shell.execute_reply.started":"2021-06-15T01:05:08.713586Z","shell.execute_reply":"2021-06-15T01:05:08.719592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '../input/state-farm-distracted-driver-detection/'\ntrain_path = data_dir + 'imgs/train/c0/'\nfilename = 'img_100026.jpg'\n\nimage = read_image(train_path + filename)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:08.72228Z","iopub.execute_input":"2021-06-15T01:05:08.722989Z","iopub.status.idle":"2021-06-15T01:05:08.95972Z","shell.execute_reply.started":"2021-06-15T01:05:08.722947Z","shell.execute_reply":"2021-06-15T01:05:08.958616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['c0','c1','c2','c3','c4','c5','c6','c7','c8','c9']\ncol_to_jp = {\n    'c0':'安全運転',\n    'c1':'右手で携帯操作',\n    'c2':'右手で電話',\n    'c3':'左手で携帯操作',\n    'c4':'左手で電話',\n    'c5':'ラジオ操作',\n    'c6':'飲み物摂取',\n    'c7':'後部座席に手を伸ばす',\n    'c8':'顔、髪に触れる',\n    'c9':'助手席と対話'\n}\n\nfor label in labels:\n    f, ax = plt.subplots(figsize=(12, 10))\n    files = glob(f'{data_dir}/imgs/train/{label}/*.jpg')\n    \n    if len(files)>9:\n        n = 9\n    else:\n        n = len(files)\n        \n    for x in range(n):\n        plt.subplot(3, 3, x+1)\n        image = read_image(files[x])\n        plt.imshow(image)\n        plt.axis('off')\n    \n    print(f'\\t\\t\\t\\t# {label} : {col_to_jp[label]}')\n    plt.show()\n    print('#'*100)","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:08.961704Z","iopub.execute_input":"2021-06-15T01:05:08.962003Z","iopub.status.idle":"2021-06-15T01:05:19.063299Z","shell.execute_reply.started":"2021-06-15T01:05:08.961972Z","shell.execute_reply":"2021-06-15T01:05:19.062403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(24, 10))\nfiles = glob(f'{data_dir}/imgs/test/*.jpg')\n\nfor x in range(18):\n    plt.subplot(3, 6, x+1)\n    image = read_image(files[x])\n    plt.imshow(image)\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:19.064789Z","iopub.execute_input":"2021-06-15T01:05:19.065079Z","iopub.status.idle":"2021-06-15T01:05:22.637656Z","shell.execute_reply.started":"2021-06-15T01:05:19.06505Z","shell.execute_reply":"2021-06-15T01:05:22.636785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndriver_list = pd.read_csv(data_dir + 'driver_imgs_list.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:22.638816Z","iopub.execute_input":"2021-06-15T01:05:22.639261Z","iopub.status.idle":"2021-06-15T01:05:22.675672Z","shell.execute_reply.started":"2021-06-15T01:05:22.63923Z","shell.execute_reply":"2021-06-15T01:05:22.674862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_list.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:22.676839Z","iopub.execute_input":"2021-06-15T01:05:22.677299Z","iopub.status.idle":"2021-06-15T01:05:22.698687Z","shell.execute_reply.started":"2021-06-15T01:05:22.677269Z","shell.execute_reply":"2021-06-15T01:05:22.697814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nlen(np.unique(driver_list['subject']).tolist())","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:22.699834Z","iopub.execute_input":"2021-06-15T01:05:22.700136Z","iopub.status.idle":"2021-06-15T01:05:22.725716Z","shell.execute_reply.started":"2021-06-15T01:05:22.70011Z","shell.execute_reply":"2021-06-15T01:05:22.724604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_to_img = {}\nfor i, row in driver_list.iterrows():\n    driver = row['subject']\n    label = row['classname']\n    image_path = row['img']\n    if not driver_to_img.get(driver, False):\n        driver_to_img[driver] = [image_path]\n    else:\n        driver_to_img.get(driver).append(image_path)\n        \nfor driver in np.unique(driver_list['subject']).tolist():\n    for label in labels:\n        f, ax = plt.subplots(figsize=(12, 10))\n        files = glob(f'{data_dir}/imgs/train/{label}/*.jpg')\n        print_files = []\n        for fl in files:\n            if (driver_list[driver_list['img'] == os.path.basename(fl)]['subject'] == driver).values[0]:\n                print_files.append(fl)\n                \n        if len(print_files)>9:\n            n = 9\n        else:\n            n = len(print_files)\n            \n        for x in range(n):\n            plt.subplot(3, 3, x+1)\n            image = read_image(print_files[x])\n            plt.imshow(image)\n            plt.axis('off')\n        \n        print(f'\\t\\t\\t\\t# ドライバー：{driver}|クラス：{label}（{col_to_jp[label]}）')\n        plt.show()\n        print('#'*100)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-15T01:05:22.72738Z","iopub.execute_input":"2021-06-15T01:05:22.72768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 例外的なデータ","metadata":{}},{"cell_type":"code","source":"label = \"c0\"\nimgs = [21155, 31121]\n\nprint(\"安全運転の例外\")\nf, ax = plt.subplots(figsize=(12,10))\nfor x in range(len(imgs)):\n    plt.subplot(1, 2, x+1)\n    image = read_image(f\"{data_dir}/imgs/train/{label}/img_{imgs[x]}.jpg\")\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = \"c3\"\nimgs = [38563,45874,49269,62784]\n\nprint(f\"{col_to_jp[label]}の例外\")\nf, ax = plt.subplots(figsize=(12,10))\nfor x in range(len(imgs)):\n    plt.subplot(2, 2, x+1)\n    image = read_image(f\"{data_dir}/imgs/train/{label}/img_{imgs[x]}.jpg\")\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = \"c4\"\nimgs = [92769,38427,41743,69998,77347,16077]\n\nprint(f\"{col_to_jp[label]}の例外\")\nf, ax = plt.subplots(figsize=(18,10))\nfor x in range(len(imgs)):\n    plt.subplot(2, 3, x+1)\n    image = read_image(f\"{data_dir}/imgs/train/{label}/img_{imgs[x]}.jpg\")\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = \"c9\"\nimgs = [28068,37708,73663]\n\nprint(f\"{col_to_jp[label]}の例外\")\nf, ax = plt.subplots(figsize=(18,10))\nfor x in range(len(imgs)):\n    plt.subplot(1, 3, x+1)\n    image = read_image(f\"{data_dir}/imgs/train/{label}/img_{imgs[x]}.jpg\")\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 間違ったラベル","metadata":{}},{"cell_type":"code","source":"label = \"c0\"\nimgs = [('c5',30288),('c7',46617),('c8',3835)]\n\nprint(f\"本来は{label}：{col_to_jp[label]}のはずが、それ以外にラベルされたもの \")\n\nf, ax = plt.subplots(figsize=(18,10))\nfor x in range(len(imgs)):\n    plt.subplot(1, 3, x+1)\n    image = read_image(f\"{data_dir}/imgs/train/{imgs[x][0]}/img_{imgs[x][1]}.jpg\")\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = \"c1\"\nimgs = [('c0',29923),('c0',79819),('c2',32934)]\n\nprint(f\"本来は{label}：{col_to_jp[label]}のはずが、それ以外にラベルされたもの \")\n\nf, ax = plt.subplots(figsize=(18,10))\nfor x in range(len(imgs)):\n    plt.subplot(1, 3, x+1)\n    image = read_image(f\"{data_dir}/imgs/train/{imgs[x][0]}/img_{imgs[x][1]}.jpg\")\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = \"c8\"\nimgs = [('c0',34380),('c3',423),('c5',78504)]\n\nprint(f\"本来は{label}：{col_to_jp[label]}のはずが、それ以外にラベルされたもの \")\n\nf, ax = plt.subplots(figsize=(18,10))\nfor x in range(len(imgs)):\n    plt.subplot(1, 3, x+1)\n    image = read_image(f\"{data_dir}/imgs/train/{imgs[x][0]}/img_{imgs[x][1]}.jpg\")\n    \n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}