{"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 PIL\nfrom typing import Callable\n\ndef resize_image(image: PIL.Image, width: int, height: int):\n    w_and_h = (width, height)\n    resized_image =image.thumbnail(w_and_h)\n    return resized_image\n\n#Takes a list, and modifies it so that all images are resized to the specified size\ndef resize_all_images(hotels: dict, width: int, height: int):\n    for hotel in hotels:\n        images = hotels.get(hotel, None)\n        for i in range(len(images)):\n            images[i] = resize_image(images[i], width, height)\n            \ndef filter_hotels_immutable(hotels: dict, function: Callable[[tuple],bool]):\n    result = dict(filter(function, hotels.items()))\n    return result\n\n\ndef map_all_images(hotels: dict, imageMapFunction: Callable[[any], any]):\n    for hotel in hotels:\n        images = hotels.get(hotel, None)\n        images_mapped = map(imageMapFunction, images)\n        hotels.update(hotel, images_mapped)\n        \ndef map_images_filter_hotels(hotels: dict, hotelFilter: Callable[[tuple],bool], imageMapFunction: Callable[[any],any] ):\n    filtered_hotels = filter_hotels_immutable(hotels, hotelFilter)\n    for hotel in filtered_hotels:\n        images = hotels.get(hotel, None)\n        images_mapped = map(imageMapFunction, images)\n        hotels.update(hotel, images_mapped)\n        \ndef map_images_filter_hotels(hotels: dict, hotelFilter: Callable[[tuple],bool], imageMapFunction: Callable[[any],any] ):\n    filtered_hotels = filter_hotels_immutable(hotels, hotelFilter)\n    for hotel in filtered_hotels:\n        images = hotels.get(hotel, None)\n        images_mapped = map(imageMapFunction, images)\n        hotels.update(hotel, images_mapped)\n\n\n    ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-06T13:32:36.432197Z","iopub.execute_input":"2022-05-06T13:32:36.432619Z","iopub.status.idle":"2022-05-06T13:32:36.444942Z","shell.execute_reply.started":"2022-05-06T13:32:36.432587Z","shell.execute_reply":"2022-05-06T13:32:36.444209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\nimport random\nimport os\nimport shutil\nimport numpy as np\nimport json\n\n#if os.path.isdir('data'):\n#    shutil.rmtree(\"./data\")\n#os.makedirs('./data/images/train/resized')\n#os.makedirs('./data/images/valid/resized')\n\n#n_training_images = 4000\n\n\n\n#returns a dictionary which contains all hotel \"ids\" as keys, and a list of PIL Image files as values\n\ndef getInputHotelImages():\n    result = dict({})\n    total_images = 0\n    n_images_max = 5000\n    for root, dirs, files in os.walk('../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images'):\n        for f in files:\n            hotel_name = os.path.basename(os.path.normpath(root))\n            hotel_image_path = os.path.join(root, f)\n            total_images += 1\n            hotel_images = result.get(hotel_name, None)\n            image = PIL.Image.open(hotel_image_path)\n            \n            #if no dictionary entry exits for the hotel create it, otherwise modify the list it points to\n            if hotel_images == None:\n                result.update({hotel_name : [image]})\n            else:\n                result.update({hotel_name : hotel_images.append(image)})\n            if total_images > n_images_max:\n                break\n    return result\n\nimagesDict = getInputHotelImages()\nprint(imagesDict.get('10010',None))\n\n\n#rotations = []\n#total_images = 0\n#for root, dirs, files in os.walk('../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images'):\n#    for _dir in dirs:\n#        hotel_image_path = os.path.join(root, f)\n#        image = PIL.Image.open(hotel_image_path)\n#        rotated_image, rotation = randomly_rotate_image(image)\n#        rotations.append(rotation)\n#        storage_path = './data/images/train/class'\n#        if total_images >= n_training_images:\n#            storage_path = './data/images/valid/class'\n#        rotated_image.save(os.path.join(storage_path, str(total_images)+'.png'))\n#        total_images += 1\n#    if total_images > n_images_max:#50000:\n#        break\n#print(\"Total images: {}\".format(total_images))","metadata":{"execution":{"iopub.status.busy":"2022-05-06T13:32:36.446147Z","iopub.execute_input":"2022-05-06T13:32:36.446482Z","iopub.status.idle":"2022-05-06T14:45:45.8146Z","shell.execute_reply.started":"2022-05-06T13:32:36.446446Z","shell.execute_reply":"2022-05-06T14:45:45.813226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#import json\n#with open('./data/label.json', 'w') as f:\n#    json.dump(rotations, f, indent=2)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T14:45:45.818013Z","iopub.execute_input":"2022-05-06T14:45:45.81833Z","iopub.status.idle":"2022-05-06T14:45:45.830267Z","shell.execute_reply.started":"2022-05-06T14:45:45.818293Z","shell.execute_reply":"2022-05-06T14:45:45.829583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import shutil\n# shutil.make_archive('data.zip', 'zip', './data')","metadata":{"execution":{"iopub.status.busy":"2022-05-06T14:45:45.83157Z","iopub.execute_input":"2022-05-06T14:45:45.83203Z","iopub.status.idle":"2022-05-06T14:45:45.839025Z","shell.execute_reply.started":"2022-05-06T14:45:45.832Z","shell.execute_reply":"2022-05-06T14:45:45.838168Z"},"trusted":true},"execution_count":null,"outputs":[]}]}