{"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-03-24T17:17:36.306015Z","iopub.execute_input":"2022-03-24T17:17:36.306668Z","iopub.status.idle":"2022-03-24T17:17:36.768378Z","shell.execute_reply.started":"2022-03-24T17:17:36.306534Z","shell.execute_reply":"2022-03-24T17:17:36.767614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PAD = True\nWIDTH = 256\nHEIGHT = 256","metadata":{"execution":{"iopub.status.busy":"2022-03-24T17:17:36.769964Z","iopub.execute_input":"2022-03-24T17:17:36.770359Z","iopub.status.idle":"2022-03-24T17:17:36.774938Z","shell.execute_reply.started":"2022-03-24T17:17:36.770325Z","shell.execute_reply":"2022-03-24T17:17:36.773931Z"},"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-03-24T17:17:36.776719Z","iopub.execute_input":"2022-03-24T17:17:36.777025Z","iopub.status.idle":"2022-03-24T17:17:37.155222Z","shell.execute_reply.started":"2022-03-24T17:17:36.776994Z","shell.execute_reply":"2022-03-24T17:17:37.154487Z"},"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-03-24T17:17:37.157943Z","iopub.execute_input":"2022-03-24T17:17:37.158336Z","iopub.status.idle":"2022-03-24T17:18:01.300643Z","shell.execute_reply.started":"2022-03-24T17:17:37.158291Z","shell.execute_reply":"2022-03-24T17:18:01.299207Z"},"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)","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-03-24T17:18:01.301628Z","iopub.status.idle":"2022-03-24T17:18:01.301919Z","shell.execute_reply.started":"2022-03-24T17:18:01.301772Z","shell.execute_reply":"2022-03-24T17:18:01.301788Z"},"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-03-24T17:18:01.303615Z","iopub.status.idle":"2022-03-24T17:18:01.303924Z","shell.execute_reply.started":"2022-03-24T17:18:01.303776Z","shell.execute_reply":"2022-03-24T17:18:01.303793Z"},"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-03-24T17:18:01.304921Z","iopub.status.idle":"2022-03-24T17:18:01.305205Z","shell.execute_reply.started":"2022-03-24T17:18:01.305060Z","shell.execute_reply":"2022-03-24T17:18:01.305075Z"},"trusted":true},"execution_count":null,"outputs":[]}]}