{"cells":[{"metadata":{},"cell_type":"markdown","source":"Show how to draw bbox and crop them as train datas."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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)\nfrom PIL import Image\nimport glob \nimport matplotlib.pyplot as plt\nimport json\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nos.listdir('/kaggle/input/iwildcam-2020-fgvc7')\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# EDA"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"test=pd.read_csv('../input/iwildcam-2020-fgvc7/sample_submission.csv')\ntest.shape                  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_jpeg = glob.glob('../input/iwildcam-2020-fgvc7/train/*')\ntest_jpeg = glob.glob('../input/iwildcam-2020-fgvc7/test/*')\n\nprint(\"number of train jpeg data:\", len(train_jpeg))\nprint(\"number of test jpeg data:\", len(test_jpeg))\n'''\nnumber of train jpeg data: 217959\nnumber of test jpeg data: 62894\n\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(25, 16))\nfor i,im_path in enumerate(train_jpeg[:16]):\n    ax = fig.add_subplot(4, 4, i+1, xticks=[], yticks=[])\n    im = Image.open(im_path)\n    im = im.resize((480,270))\n    plt.imshow(im)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Draw bbox and Save"},{"metadata":{},"cell_type":"markdown","source":"## One sample"},{"metadata":{},"cell_type":"markdown","source":"* This picture has over 3 bboxes. We will choose three bboxes which conf are greater than 0.9."},{"metadata":{},"cell_type":"markdown","source":"{'detections': [{'category': '1', 'bbox': [0.0, 0.4669, 0.1853, 0.4238], 'conf': 1.0}, {'category': '1', 'bbox': [0.2406, 0.4672, 0.0309, 0.1105], 'conf': 0.998}, {'category': '1', 'bbox': [0.5058, 0.4577, 0.06, 0.1043], 'conf': 0.911}, {'category': '1', 'bbox': [0.9902, 0.4283, 0.0098, 0.0487], 'conf': 0.697}, {'category': '1', 'bbox': [0.9956, 0.4284, 0.0044, 0.049], 'conf': 0.505}, {'category': '1', 'bbox': [0.9974, 0.4293, 0.0026, 0.0481], 'conf': 0.505}, {'category': '1', 'bbox': [0.5078, 0.4574, 0.046, 0.0656], 'conf': 0.316}], 'id': '905a4416-21bc-11ea-a13a-137349068a90', 'max_detection_conf': 1.0}"},{"metadata":{"trusted":true},"cell_type":"code","source":"im = Image.open(\"../input/iwildcam-2020-fgvc7/train/905a4416-21bc-11ea-a13a-137349068a90.jpg\")\n\nplt.imshow(im)\nprint(im.size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('../input/iwildcam-2020-fgvc7/iwildcam2020_megadetector_results.json', encoding='utf-8') as fin:\n    train_df=json.load(fin)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for i,item in enumerate(train_df['images']):\n#     if i>20:break\n#     print('*'*50)\n#     print(item)\n        ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Draw one bbox"},{"metadata":{"trusted":true},"cell_type":"code","source":"box=[0.0, 0.4669, 0.1853, 0.4238]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image, ImageDraw\n\n\nx1, y1,w_box, h_box = box\nymin,xmin,ymax, xmax=y1, x1, y1 + h_box, x1 + w_box\n\n\ndraw = ImageDraw.Draw(im)\nimageWidth=im.size[0]\nimageHeight= im.size[1]\n(left, right, top, bottom) = (xmin * imageWidth, xmax * imageWidth,\n                                      ymin * imageHeight, ymax * imageHeight)\nprint(left, right, top, bottom)\n\ndraw.line([(left, top), (left, bottom), (right, bottom),\n               (right, top), (left, top)], width=4, fill='Red')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plt.imshow(crop_img)\nim","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Crop and save the bbox image"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Image.crop(left, up, right, below)\ncrop_shape=(left,top , right, bottom)\ncrop_img = im.crop(crop_shape)\ncrop_img=crop_img.resize((299,299))\niImage = im.format\ncrop_img.save('dogs1.jpg'.format(iImage))\ncrop_img","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Draw the second bbox"},{"metadata":{"trusted":true},"cell_type":"code","source":"box=[0.2406, 0.4672, 0.0309, 0.1105]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x1, y1,w_box, h_box = box\nymin,xmin,ymax, xmax=y1, x1, y1 + h_box, x1 + w_box\n\n\n(left, right, top, bottom) = (xmin * imageWidth, xmax * imageWidth,\n                                      ymin * imageHeight, ymax * imageHeight)\nprint(left, right, top, bottom)\n\ndraw.line([(left, top), (left, bottom), (right, bottom),\n               (right, top), (left, top)], width=4, fill='Red')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"im","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Image.crop(left, up, right, below)\ncrop_shape=(left,top , right, bottom)\ncrop_img = im.crop(crop_shape)\ncrop_img=crop_img.resize((299,299))\niImage = im.format\ncrop_img.save('dogs2.jpg'.format(iImage))\ncrop_img","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Draw the third bbox"},{"metadata":{"trusted":true},"cell_type":"code","source":"box=[0.5058, 0.4577, 0.06, 0.1043]\nx1, y1,w_box, h_box = box\nymin,xmin,ymax, xmax=y1, x1, y1 + h_box, x1 + w_box\n\n\n(left, right, top, bottom) = (xmin * imageWidth, xmax * imageWidth,\n                                      ymin * imageHeight, ymax * imageHeight)\nprint(left, right, top, bottom)\n\ndraw.line([(left, top), (left, bottom), (right, bottom),\n               (right, top), (left, top)], width=4, fill='Red')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"im","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Image.crop(left, up, right, below)\ncrop_shape=(left,top , right, bottom)\ncrop_img = im.crop(crop_shape)\ncrop_img=crop_img.resize((299,299))\niImage = im.format\ncrop_img.save('dogs3.jpg'.format(iImage))\ncrop_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Done!')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}