{"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":"markdown","source":"## Setup","metadata":{}},{"cell_type":"code","source":"!pip install --no-index --no-deps --find-links /kaggle/input/requirements timm tf_slim\n!pip install --no-index /kaggle/input/requirements/neuralgym-0.0.1.zip\n!cp -r /kaggle/input/requirements/generative_inpainting /kaggle/generative_inpainting","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:01.840762Z","iopub.execute_input":"2022-06-06T09:41:01.841519Z","iopub.status.idle":"2022-06-06T09:41:20.785476Z","shell.execute_reply.started":"2022-06-06T09:41:01.84142Z","shell.execute_reply":"2022-06-06T09:41:20.784164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nimport random\n\nfrom os import listdir\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:20.788063Z","iopub.execute_input":"2022-06-06T09:41:20.788339Z","iopub.status.idle":"2022-06-06T09:41:21.207688Z","shell.execute_reply.started":"2022-06-06T09:41:20.788301Z","shell.execute_reply":"2022-06-06T09:41:21.206574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import inpaint","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:21.213375Z","iopub.execute_input":"2022-06-06T09:41:21.215907Z","iopub.status.idle":"2022-06-06T09:41:29.318767Z","shell.execute_reply.started":"2022-06-06T09:41:21.215862Z","shell.execute_reply":"2022-06-06T09:41:29.317698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PROJECT_FOLDER = '/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/'\nTRAIN_FOLDER   = PROJECT_FOLDER + 'train_images/'","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:29.322378Z","iopub.execute_input":"2022-06-06T09:41:29.323238Z","iopub.status.idle":"2022-06-06T09:41:29.330541Z","shell.execute_reply.started":"2022-06-06T09:41:29.323181Z","shell.execute_reply":"2022-06-06T09:41:29.329589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sample Image","metadata":{}},{"cell_type":"code","source":"def _add_red_square(img, left, top, width, height):\n    img[top:top+height, left:left+width] = [0, 0, 255]\n    return img\n\ndef add_red_square(img):\n    h, w, _ = img.shape\n    left   = random.randint(int(w / 10), int(w / 2))\n    top    = random.randint(int(h / 10), int(h / 2))\n    width  = random.randint(int(w / 3),  int(w / 2))\n    height = random.randint(int(h / 3),  int(h / 2))\n    return _add_red_square(img, left, top, width, height)","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:29.335578Z","iopub.execute_input":"2022-06-06T09:41:29.336398Z","iopub.status.idle":"2022-06-06T09:41:29.352375Z","shell.execute_reply.started":"2022-06-06T09:41:29.336349Z","shell.execute_reply":"2022-06-06T09:41:29.351122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(PROJECT_FOLDER + 'test_images/abc.jpg')\nimage = cv2.resize(image, (512, 512))\ninp   = inpaint.inpaint(image.copy())\n\nf, (ax1, ax2) = plt.subplots(1,2, figsize=(14,8))\nax1.imshow(image[...,::-1])\nax2.imshow(inp)","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:29.354785Z","iopub.execute_input":"2022-06-06T09:41:29.355484Z","iopub.status.idle":"2022-06-06T09:41:44.435123Z","shell.execute_reply.started":"2022-06-06T09:41:29.355434Z","shell.execute_reply":"2022-06-06T09:41:44.434128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.fromarray(image[...,::-1]).save('sample_image.png')\nImage.fromarray(inp).save('sample_inpaint.png')","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:44.436492Z","iopub.execute_input":"2022-06-06T09:41:44.437191Z","iopub.status.idle":"2022-06-06T09:41:44.627847Z","shell.execute_reply.started":"2022-06-06T09:41:44.437159Z","shell.execute_reply":"2022-06-06T09:41:44.626868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Data","metadata":{}},{"cell_type":"code","source":"for i in range(25):\n    hotel_id = random.choice(listdir(TRAIN_FOLDER))\n    image_id = random.choice(listdir(TRAIN_FOLDER + '/' + hotel_id))\n    image    = cv2.imread(TRAIN_FOLDER + '/' + hotel_id + '/' + image_id)\n    image    = cv2.resize(image, (512, 512))\n    image    = add_red_square(image)\n    inp      = inpaint.inpaint(image.copy())\n\n    f, (ax1, ax2) = plt.subplots(1,2, figsize=(14,8))\n    ax1.imshow(image[...,::-1])\n    ax2.imshow(inp)\n    \n    Image.fromarray(image[...,::-1]).save(f'random_image{i}.png')\n    Image.fromarray(inp).save(f'random_inpaint{i}.png')","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:44.62958Z","iopub.execute_input":"2022-06-06T09:41:44.629924Z","iopub.status.idle":"2022-06-06T09:41:46.852596Z","shell.execute_reply.started":"2022-06-06T09:41:44.629881Z","shell.execute_reply":"2022-06-06T09:41:46.848348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.fromarray(image[...,::-1]).save('random_image.png')\nImage.fromarray(inp).save('random_inpaint.png')","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:46.854511Z","iopub.execute_input":"2022-06-06T09:41:46.855125Z","iopub.status.idle":"2022-06-06T09:41:47.043839Z","shell.execute_reply.started":"2022-06-06T09:41:46.855077Z","shell.execute_reply":"2022-06-06T09:41:47.042874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Nugget Artefact","metadata":{}},{"cell_type":"code","source":"image = cv2.imread(TRAIN_FOLDER + '/199156/000021720.jpg')\nimage = cv2.resize(image, (512, 512))\nimage = _add_red_square(image, 100, 125, 210, 220)\ninp   = inpaint.inpaint(image.copy())\n\nf, (ax1, ax2) = plt.subplots(1,2, figsize=(14,8))\nax1.imshow(image[...,::-1])\nax2.imshow(inp)","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:47.047198Z","iopub.execute_input":"2022-06-06T09:41:47.047526Z","iopub.status.idle":"2022-06-06T09:41:49.061714Z","shell.execute_reply.started":"2022-06-06T09:41:47.04748Z","shell.execute_reply":"2022-06-06T09:41:49.060816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.fromarray(image[...,::-1]).save('nugget_image.png')\nImage.fromarray(inp).save('nugget_inpaint.png')","metadata":{"execution":{"iopub.status.busy":"2022-06-06T09:41:49.063344Z","iopub.execute_input":"2022-06-06T09:41:49.064109Z","iopub.status.idle":"2022-06-06T09:41:49.254773Z","shell.execute_reply.started":"2022-06-06T09:41:49.064064Z","shell.execute_reply":"2022-06-06T09:41:49.253742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}