{"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\nimport random\nfrom tqdm import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2022-05-21T17:17:24.442663Z","iopub.execute_input":"2022-05-21T17:17:24.442967Z","iopub.status.idle":"2022-05-21T17:17:24.448100Z","shell.execute_reply.started":"2022-05-21T17:17:24.442937Z","shell.execute_reply":"2022-05-21T17:17:24.446964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PAD = True\nWIDTH = 256\nHEIGHT = 256","metadata":{"execution":{"iopub.status.busy":"2022-05-21T17:12:38.190668Z","iopub.execute_input":"2022-05-21T17:12:38.191083Z","iopub.status.idle":"2022-05-21T17:12:38.194498Z","shell.execute_reply.started":"2022-05-21T17:12:38.191053Z","shell.execute_reply":"2022-05-21T17:12:38.193678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_folder = \"/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/\"\nmask_folder = \"/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_masks/\"\ntrain_folder = os.path.join(data_folder, 'train_images')\nchain_names = os.listdir(train_folder)\nmask_lis = os.listdir(mask_folder)\nprint(len(mask_lis))\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-21T17:12:38.195821Z","iopub.execute_input":"2022-05-21T17:12:38.196283Z","iopub.status.idle":"2022-05-21T17:12:38.485891Z","shell.execute_reply.started":"2022-05-21T17:12:38.196244Z","shell.execute_reply":"2022-05-21T17:12:38.485052Z"},"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-21T17:12:42.307785Z","iopub.execute_input":"2022-05-21T17:12:42.308300Z","iopub.status.idle":"2022-05-21T17:14:39.018682Z","shell.execute_reply.started":"2022-05-21T17:12:42.308265Z","shell.execute_reply":"2022-05-21T17:14:39.017777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef 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    mask_name = random.choice(mask_lis)\n    if PAD:\n        img = pad_image(img)\n    \n    img = cv2.resize(img, (WIDTH, HEIGHT))\n    image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    mask_p = os.path.join(mask_folder, mask_name)\n    mask = cv2.imread(mask_p)\n    mask = cv2.resize(mask, (WIDTH, HEIGHT))\n    mask_g = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)\n    image[mask_g>0] = mask[mask_g>0]\n    return image\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-21T17:18:25.023563Z","iopub.execute_input":"2022-05-21T17:18:25.024373Z","iopub.status.idle":"2022-05-21T17:18:25.037287Z","shell.execute_reply.started":"2022-05-21T17:18:25.024310Z","shell.execute_reply":"2022-05-21T17:18:25.036419Z"},"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-21T17:18:25.526049Z","iopub.execute_input":"2022-05-21T17:18:25.526356Z"},"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":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}