{"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport imageio\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom PIL import Image, ImageDraw\nfrom ast import literal_eval\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = '/kaggle/input/global-wheat-detection/'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(PATH+'train.csv')\ntrain_images = pd.Series(os.listdir(PATH + '/train/')).sort_values(ascending=True).reset_index(drop=True)\ntest_images = pd.Series(os.listdir(PATH + '/test')).sort_values(ascending=True).reset_index(drop=True)\nsample_submission = pd.read_csv(f'{PATH}/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#unique image ids\nprint(train_df[\"image_id\"].nunique())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### display different sources"},{"metadata":{"trusted":true},"cell_type":"code","source":"x = train_df['source'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x.plot(kind='bar')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Display Images"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(16, 8))\nk=0\nfor i, row in enumerate(ax):\n    for j, col in enumerate(row):\n        img = mpimg.imread(PATH + '/train/' + train_images[k])\n        col.imshow(img)\n        col.set_title(train_images[k])\n        k=k+1\nplt.suptitle('Samples from train Images', fontsize=14)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(16, 8))\nk=0\nfor i, row in enumerate(ax):\n    for j, col in enumerate(row):\n        img = mpimg.imread(PATH + '/test/' + test_images[k])\n        col.imshow(img)\n        col.set_title(test_images[k])\n        k=k+1\nplt.suptitle('Samples from Test Images', fontsize=14)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for folder in os.listdir(PATH):\n    try:\n        print(f\"Folder {folder} contains {len(os.listdir(PATH + '/' + folder))} images.\")\n    except:\n        print(f'{folder}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# let's Apply BBox on single Image ID"},{"metadata":{"trusted":true},"cell_type":"code","source":"display_train_image = train_df.loc[train_df['image_id'] == 'b6ab77fd7']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display_train_image.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id = 'train/b6ab77fd7'\nimage_path = os.path.join(PATH + image_id + \".jpg\")\nimage = Image.open(image_path)\n\nboxes = [literal_eval(box) for box in train_df.loc[train_df['image_id'] == 'b6ab77fd7']['bbox']]\n\nprint(boxes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# visualize them\ndraw = ImageDraw.Draw(image)\nfor box in boxes:    \n    draw.rectangle([box[0], box[1], box[0] + box[2], box[1] + box[3]], width=3)\n\nplt.figure(figsize = (15,15))\nplt.imshow(image)\nplt.show()","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}