{"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-20T17:02:09.005263Z","iopub.execute_input":"2022-05-20T17:02:09.00564Z","iopub.status.idle":"2022-05-20T17:02:09.54528Z","shell.execute_reply.started":"2022-05-20T17:02:09.005542Z","shell.execute_reply":"2022-05-20T17:02:09.544157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PAD = True\nWIDTH = 512\nHEIGHT = 512","metadata":{"execution":{"iopub.status.busy":"2022-05-20T17:02:09.547276Z","iopub.execute_input":"2022-05-20T17:02:09.547512Z","iopub.status.idle":"2022-05-20T17:02:09.552769Z","shell.execute_reply.started":"2022-05-20T17:02:09.547483Z","shell.execute_reply":"2022-05-20T17:02:09.551727Z"},"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-20T17:02:09.554605Z","iopub.execute_input":"2022-05-20T17:02:09.555038Z","iopub.status.idle":"2022-05-20T17:02:09.669238Z","shell.execute_reply.started":"2022-05-20T17:02:09.554992Z","shell.execute_reply":"2022-05-20T17:02:09.668602Z"},"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-20T17:02:09.670655Z","iopub.execute_input":"2022-05-20T17:02:09.671124Z","iopub.status.idle":"2022-05-20T17:04:17.189794Z","shell.execute_reply.started":"2022-05-20T17:02:09.671073Z","shell.execute_reply":"2022-05-20T17:04:17.18889Z"},"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-05-20T17:04:17.192015Z","iopub.execute_input":"2022-05-20T17:04:17.192855Z","iopub.status.idle":"2022-05-20T17:04:17.203048Z","shell.execute_reply.started":"2022-05-20T17:04:17.192785Z","shell.execute_reply":"2022-05-20T17:04:17.202166Z"},"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-20T17:04:17.204235Z","iopub.execute_input":"2022-05-20T17:04:17.204723Z","iopub.status.idle":"2022-05-20T17:26:55.170711Z","shell.execute_reply.started":"2022-05-20T17:04:17.204689Z","shell.execute_reply":"2022-05-20T17:26:55.169061Z"},"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-20T17:26:55.175323Z","iopub.execute_input":"2022-05-20T17:26:55.175976Z","iopub.status.idle":"2022-05-20T17:29:41.021982Z","shell.execute_reply.started":"2022-05-20T17:26:55.175933Z","shell.execute_reply":"2022-05-20T17:29:41.02078Z"},"trusted":true},"execution_count":null,"outputs":[]}]}