{"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\nimport numpy as np\nimport pandas as pd\nimport cv2\n\nfrom tqdm import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:56:34.061254Z","iopub.execute_input":"2022-05-26T14:56:34.062064Z","iopub.status.idle":"2022-05-26T14:56:34.066814Z","shell.execute_reply.started":"2022-05-26T14:56:34.062027Z","shell.execute_reply":"2022-05-26T14:56:34.066099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PAD = True\nWIDTH = 256\nHEIGHT = 256","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:56:34.068022Z","iopub.execute_input":"2022-05-26T14:56:34.068319Z","iopub.status.idle":"2022-05-26T14:56:34.077451Z","shell.execute_reply.started":"2022-05-26T14:56:34.068284Z","shell.execute_reply":"2022-05-26T14:56:34.076745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_folder = \"/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/\"\ntrain_folder = os.path.join(data_folder, 'train_images')\nchain_names = os.listdir(train_folder)\n\nprint(os.listdir(data_folder))\nprint(len(chain_names))","metadata":{"papermill":{"duration":0.225587,"end_time":"2021-04-16T01:53:14.982769","exception":false,"start_time":"2021-04-16T01:53:14.757182","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-26T14:56:34.07858Z","iopub.execute_input":"2022-05-26T14:56:34.079349Z","iopub.status.idle":"2022-05-26T14:56:34.090539Z","shell.execute_reply.started":"2022-05-26T14:56:34.079313Z","shell.execute_reply":"2022-05-26T14:56:34.089533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame(columns={'image_id', 'hotel_id'})\nfor hotel_id in tqdm(chain_names):\n    for image_id in os.listdir(os.path.join(train_folder, hotel_id)):\n        train_df = train_df.append({'image_id': image_id, 'hotel_id': hotel_id}, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:56:34.092022Z","iopub.execute_input":"2022-05-26T14:56:34.092692Z","iopub.status.idle":"2022-05-26T14:58:06.158419Z","shell.execute_reply.started":"2022-05-26T14:56:34.092657Z","shell.execute_reply":"2022-05-26T14:58:06.156885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:58:06.160686Z","iopub.execute_input":"2022-05-26T14:58:06.161104Z","iopub.status.idle":"2022-05-26T14:58:06.179895Z","shell.execute_reply.started":"2022-05-26T14:58:06.161065Z","shell.execute_reply":"2022-05-26T14:58:06.179112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pad_image(img):\n    w, h, c = np.shape(img)\n    if w > h:\n        pad = int((w - h) / 2)\n        img = cv2.copyMakeBorder(img, 0, 0, pad, pad, cv2.BORDER_CONSTANT, value=0)\n    else:\n        pad = int((h - w) / 2)\n        img = cv2.copyMakeBorder(img, pad, pad, 0, 0, cv2.BORDER_CONSTANT, value=0)\n        \n    return img\n\n\ndef open_and_preprocess_image(image_folder, image_name):\n    img = cv2.imread(os.path.join(image_folder, image_name))\n    \n    if PAD:\n        img = pad_image(img)\n    \n    return cv2.resize(img, (WIDTH, HEIGHT))\n\n\ndef save_image(image_name, img):\n    cv2.imwrite(image_name, img)\n    \n    \ndef process_chain(data_folder, chain_name):\n    chain_folder = os.path.join(data_folder, chain_name)\n    \n    for image_name in os.listdir(chain_folder):\n        img = open_and_preprocess_image(chain_folder, image_name)\n        save_image(image_name, img)\n","metadata":{"papermill":{"duration":0.01604,"end_time":"2021-04-16T01:53:15.003615","exception":false,"start_time":"2021-04-16T01:53:14.987575","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-26T14:58:06.1813Z","iopub.execute_input":"2022-05-26T14:58:06.181562Z","iopub.status.idle":"2022-05-26T14:58:06.188179Z","shell.execute_reply.started":"2022-05-26T14:58:06.181527Z","shell.execute_reply":"2022-05-26T14:58:06.187237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_image(image_name, img):\n    cv2.imwrite(image_name, img)\n    \ndef process_chain(data_folder, chain_name):\n    chain_folder = os.path.join(data_folder, chain_name)\n    \n    for image_name in os.listdir(chain_folder):\n        img = cv2.imread(os.path.join(chain_folder, image_name))\n        save_image(image_name, img)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:58:06.189695Z","iopub.execute_input":"2022-05-26T14:58:06.190094Z","iopub.status.idle":"2022-05-26T14:58:06.199108Z","shell.execute_reply.started":"2022-05-26T14:58:06.189969Z","shell.execute_reply":"2022-05-26T14:58:06.198396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndfs_proc = Parallel(n_jobs=4, prefer='threads')(delayed(process_chain)(train_folder, chain_names[i]) for i in range(0, len(chain_names)))","metadata":{"papermill":{"duration":2514.619147,"end_time":"2021-04-16T02:35:09.627391","exception":false,"start_time":"2021-04-16T01:53:15.008244","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-26T15:51:59.878297Z","iopub.status.idle":"2022-05-26T15:51:59.878749Z","shell.execute_reply.started":"2022-05-26T15:51:59.87852Z","shell.execute_reply":"2022-05-26T15:51:59.878543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd /kaggle/working/ & zip -jqr images.zip .\n!find . -name \"*.jpg\" -delete\ntrain_df.to_csv('train.csv', index=False)","metadata":{"papermill":{"duration":401.161771,"end_time":"2021-04-16T02:41:50.796691","exception":false,"start_time":"2021-04-16T02:35:09.63492","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-26T15:51:55.567548Z","iopub.execute_input":"2022-05-26T15:51:55.569345Z","iopub.status.idle":"2022-05-26T15:51:59.875165Z","shell.execute_reply.started":"2022-05-26T15:51:55.569308Z","shell.execute_reply":"2022-05-26T15:51:59.87282Z"},"trusted":true},"execution_count":null,"outputs":[]}]}