{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"05943777-3037-b166-acc4-4e0363c477a4"},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"5b75b271-03f0-b891-a4f5-4f6dfcf33dc9"},"outputs":[],"source":"import numpy as np\nimport cv2\nimport os\nimport glob\nnp.random.seed(313)\n\n\ndef sub_func_load_img(abspath_img):\n    img_rgb = cv2.cvtColor(cv2.imread(abspath_img), cv2.COLOR_BGR2RGB)\n    return img_rgb\n\n\ndef sub_func_rotate_img_if_need(img_rgb):\n    if img_rgb.shape[0] >= img_rgb.shape[1]:\n        return img_rgb\n    else:\n        return np.rot90(img_rgb)\n\n\ndef sub_func_resize_img_same_ratio(img_rgb):\n    if img_rgb.shape[0] / 640.0 >= img_rgb.shape[1] / 480.0:\n        img_resized_rgb = cv2.resize(img_rgb, (int(640.0 * img_rgb.shape[1] / img_rgb.shape[0]), 640)) # (640, *, 3)\n    else:\n        img_resized_rgb = cv2.resize(img_rgb, (480, int(480.0 * img_rgb.shape[0] / img_rgb.shape[1]))) # (*, 480, 3)\n    return img_resized_rgb\n\n\ndef sub_func_resample_img(abspath_img):\n    img = sub_func_load_img(abspath_img)\n    img = sub_func_rotate_img_if_need(img)\n    img = sub_func_resize_img_same_ratio(img)\n    # img = sub_func_fill_img(img)\n    img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_LINEAR)\n    return img\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"30a2fdf4-33f8-abad-cd32-fd3501e216df"},"outputs":[],"source":"data_path = \"../input/additional/\""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"74977049-37ea-3c95-6401-0db45e4e2719"},"outputs":[],"source":"classes = ['Type_1', 'Type_2', 'Type_3']\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"17e1aa00-be0b-fd05-109f-a13d7ff7d958"},"outputs":[],"source":"imgs = []\n# ids = []\nlbls = []\ncounter = 0\n\nfor cl in classes:\n    base_path = os.path.join(data_path, cl, \"*.*\")\n    files = glob.glob(base_path)\n    print(\"Num files:\", len(files))\n    for f in files:\n        try:\n            if os.path.isfile(f):\n                img = sub_func_resample_img(f)\n                imgs.append(img)\n                # ids.append(f[f.rfind(\"/\")+1:])\n                if cl == 'Type_1':\n                    lbls.append(0)\n                elif cl == 'Type_2':\n                    lbls.append(1)\n                else:\n                    lbls.append(2)\n                counter += 1\n            else:\n                print(f)\n        except:\n            print(f)\n            continue\n        if counter % 100 == 0:\n            print(\"processed {0} images\".format(str(counter)))\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f7cd9d0a-ed7a-7859-8c7b-de1dd1024693"},"outputs":[],"source":""}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}