{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"Reference\n\n- Awesome Kernel, thanks @martinpiotte : https://www.kaggle.com/martinpiotte/bounding-box-model\n- cropping.model: https://www.kaggle.com/martinpiotte/bounding-box-model/output\n- Data: https://www.kaggle.com/c/humpback-whale-identification/data"},{"metadata":{"trusted":true,"_uuid":"53e265d56719498b6c971b5cf8ac4c5b727bbd19"},"cell_type":"code","source":"import os\n\nimport PIL\nfrom PIL import Image\nfrom PIL.ImageDraw import Draw\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom keras.models import load_model\nfrom keras.preprocessing import image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e148a566becba960aaaf89e31f2d17fac06f9c7e"},"cell_type":"code","source":"ls ../input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a432aea33948e321cf25617d67928097df0c317"},"cell_type":"code","source":"MODEL_BASE = '../input/bbox-model-whale-recognition'\nDATA = '../input/humpback-whale-identification'\nTRAIN_DATA = os.path.join(DATA, 'train')\nTEST_DATA = os.path.join(DATA, 'test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a7af444edd754740b2beb1fe32116a3aae6035c"},"cell_type":"code","source":"model = load_model(os.path.join(MODEL_BASE, 'cropping.model'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"336067fa029e3e1762155d89c85d857f60659e72"},"cell_type":"code","source":"# # input: (128, 128, 1)\n# model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b03b5b50d2c4787d9d6c065a160316ddecd45fc"},"cell_type":"code","source":"train_paths = [os.path.join(TRAIN_DATA, img) for img in os.listdir(TRAIN_DATA)]\ntest_paths = [os.path.join(TEST_DATA, img) for img in os.listdir(TEST_DATA)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15a23f3c1f03dad156e61714c2fe415a851ee5ab"},"cell_type":"code","source":"train_paths[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8cba095fd0c833d4adab66b477f5b92927988bb8"},"cell_type":"code","source":"img = image.load_img(train_paths[10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1cf9409b495e1bdf3a5e357b51eae53092f7ef75"},"cell_type":"code","source":"img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c22096e7671d5f1f1ee0d9ed27cdeafcde446f7b"},"cell_type":"code","source":"img_arr = image.img_to_array(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6fdb038bb73ed6fc37b97736108f8458c62e8a60"},"cell_type":"code","source":"img_arr.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3c952b10461702f974e4cf81a77df6acadebb01"},"cell_type":"code","source":"rimg = img.resize((128, 128), PIL.Image.ANTIALIAS)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ec0af84aa3a4e03b2e993a7f94bd77e4a921b6e"},"cell_type":"code","source":"rimg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bfd434146e8e8a4e0f620a82b91e9c93ff6a2198"},"cell_type":"code","source":"rimg_arr = image.img_to_array(rimg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de196bbf7b56e9ca22c822a836cd720661125ad9"},"cell_type":"code","source":"rimg_ = rimg.convert('L')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03f36f9eed18c8696a507a76b179f6299658acb5"},"cell_type":"code","source":"rimg_arr_ = image.img_to_array(rimg_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3af4a6b9d00a8e6034acac566640073ca8ef1a1"},"cell_type":"code","source":"rimg_arr_.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f9241afe2363c3c5e38b51312a85e70e4485fa7"},"cell_type":"code","source":"bbox = model.predict(np.expand_dims(rimg_arr_, axis=0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"196ca7341557bb05fec5a430c9828dc4c41e7349"},"cell_type":"code","source":"bbox","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebcb7fd33d111640d70f9fdaa56a8c588222a5a2"},"cell_type":"code","source":"draw = Draw(rimg_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"111bb6f63c59d927caa82da7360d11e8f9d2991f"},"cell_type":"code","source":"draw.rectangle(bbox, outline='red')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a771408490ab44e633749919f1ac66302f4c708"},"cell_type":"code","source":"rimg_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a099c336c760d2279961e8ba3f675e17b6ad9cbf"},"cell_type":"code","source":"rimg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4afc22b5f530e5bdd063a257544edca8b4eae111"},"cell_type":"code","source":"img_crop = rimg_.crop(tuple(bbox[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8d3310441a1c2583486d3ad42a2b6047c36a7b3"},"cell_type":"code","source":"img_crop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b45eea2cd93933ea852d08a30c846440eeb6376"},"cell_type":"code","source":"def make_bbox_image(img_path):\n    \"\"\"\n    :param img: path to image\n    \"\"\"\n    main_img = image.load_img(img_path)\n    r_img = main_img.resize((128, 128), PIL.Image.ANTIALIAS)\n    # convert to 1d image\n    rb_img = r_img.convert('L')\n    rb_img_arr = image.img_to_array(rb_img)\n    bbox = model.predict(np.expand_dims(rb_img_arr, axis=0))\n    \n    # draw rectangle\n    # draw = Draw(rimg)\n    # draw.rectangle(bbox, outline='red')\n    \n    img_crop = r_img.crop(tuple(bbox[0]))\n    img_arr = image.img_to_array(img_crop)\n    return img_crop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62a0ba9bf1847bd77233176d5178b18312e5d2ad"},"cell_type":"code","source":"train_paths[10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"990904302314d275a688ad1f77bd9344b4034a5a"},"cell_type":"code","source":"img = make_bbox_image(train_paths[10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68517177507dcdec511ecdd4f0069b785148dbdb"},"cell_type":"code","source":"plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6bbddf36e5cbc397e7baa1b87635063f6da8cbef"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}