{"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)\n\n# Input data files are available in the read-only \"../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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nos.chdir(\"/kaggle/input/pycoco\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install pycocotools-2.0.1.tar","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(\"/kaggle/input/efficientdet-repo\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nimport os\nimport inference\nimport tensorflow.compat.v1 as tf\nimport numpy as np\ntf.reset_default_graph()\n\ntf.disable_v2_behavior()\n\nMODEL='efficientdet-d3'\nMODEL_DIR='/kaggle/input/effiecientdetd3-10k-epoch-checkpoints'\ndriver = inference.ServingDriver(MODEL, MODEL_DIR)\ndriver.build()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocess_predictions(prediction):\n    boxes = prediction[0][:, 1:5].astype(int)\n    classes = prediction[0][:, 6].astype(int)\n    scores = prediction[0][:, 5]\n    final_cords=[]\n    \n    for ind in range(len(boxes)):\n        try:\n            ymin, xmin ,ymax ,xmax = boxes[ind]\n\n            w,h=int(ymax-ymin), int(xmax-xmin)\n\n            final_cords.append(\"{:.2f} {} {} {} {}\".format(scores[ind],xmin,ymin,w,h))\n            \n                               \n        except:\n            \n            final_cords.append(\"\")\n            \n            \n    return final_cords\n                        \n\n        \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nfrom PIL import Image\nimport time\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\n\ndata_dir = '/kaggle/input/global-wheat-detection/test'\n\nsubmission = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv')\n\n\nroot_image = \"/kaggle/input/global-wheat-detection/test/\"\ntest_images = [root_image + f\"{img}.jpg\" for img in submission.image_id]\n\n\nsubmission = []\n\nfor imagepath in test_images:\n    start=time.time()\n    image = np.array(Image.open(imagepath))\n    predictions = driver.serve_images([image])\n    \n    boxes = predictions[0][:, 1:5].astype(int)\n    \n    for box in boxes:\n        ymin, xmin, ymax , xmax=box\n        cv2.rectangle(image, (xmin, ymin), (xmax,ymax), (0,0,255),3)\n    plt.figure(figsize=(10,10))\n    plt.imshow(image)\n    plt.show()\n        \n    \n  \n    cords=preprocess_predictions(predictions)\n#     print(time.time()-start)\n    \n    prediction_string = \" \".join(cords)\n    \n    submission.append([os.path.basename(imagepath)[:-4],prediction_string])\n\nsample_submission = pd.DataFrame(submission, columns=[\"image_id\",\"PredictionString\"])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission.to_csv(\"/kaggle/working/submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission[\"PredictionString\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}