{"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_minor":4,"nbformat":4,"cells":[{"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\nfor 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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-29T07:33:26.972971Z","iopub.execute_input":"2021-11-29T07:33:26.973621Z","iopub.status.idle":"2021-11-29T07:33:35.884099Z","shell.execute_reply.started":"2021-11-29T07:33:26.973527Z","shell.execute_reply":"2021-11-29T07:33:35.882506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport os\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\nplt.rcParams['figure.figsize'] = 30, 30","metadata":{"execution":{"iopub.status.busy":"2021-11-29T07:33:35.887730Z","iopub.execute_input":"2021-11-29T07:33:35.888125Z","iopub.status.idle":"2021-11-29T07:33:36.143349Z","shell.execute_reply.started":"2021-11-29T07:33:35.888076Z","shell.execute_reply":"2021-11-29T07:33:36.142490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T07:33:36.144499Z","iopub.execute_input":"2021-11-29T07:33:36.144728Z","iopub.status.idle":"2021-11-29T07:33:36.219702Z","shell.execute_reply.started":"2021-11-29T07:33:36.144700Z","shell.execute_reply":"2021-11-29T07:33:36.218858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ast import literal_eval\n\n\ndef load_image(video_id, video_frame, image_dir):\n    img_path = f'{image_dir}/video_{video_id}/{video_frame}.jpg'\n    assert os.path.exists(img_path), f'{img_path} does not exist.'\n    img = cv2.imread(img_path)\n    return img\n\n\ndef decode_annotations(annotaitons_str):\n    \"\"\"decode annotations in string to list of dict\"\"\"\n    return literal_eval(annotaitons_str)\n\ndef load_image_with_annotations(video_id, video_frame, image_dir, annotaitons_str):\n    img = load_image(video_id, video_frame, image_dir)\n    annotations = decode_annotations(annotaitons_str)\n    if len(annotations) > 0:\n        for ann in annotations:\n            cv2.rectangle(img, (ann['x'], ann['y']),\n                (ann['x'] + ann['width'], ann['y'] + ann['height']),\n                (0, 255, 255), thickness=2,)\n    return img\n\n#test\nindex = 16\nrow = df.iloc[index]\nvideo_id = row.video_id\nvideo_frame = row.video_frame\nannotations_str = row.annotations\nimage_dir = '../input/tensorflow-great-barrier-reef/train_images'\nimg = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str)\nplt.imshow(img[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2021-11-29T07:33:36.221338Z","iopub.execute_input":"2021-11-29T07:33:36.221558Z","iopub.status.idle":"2021-11-29T07:33:38.098204Z","shell.execute_reply.started":"2021-11-29T07:33:36.221531Z","shell.execute_reply":"2021-11-29T07:33:38.096529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.auto import tqdm\nimport subprocess\n\ndef make_video(df, video_id, image_dir):\n    # partly borrowed from https://github.com/RobMulla/helmet-assignment/blob/main/helmet_assignment/video.py\n    fps = 15 # don't know exact value\n    width = 1280\n    height = 720\n    save_path = f'video{video_id}.mp4'\n    tmp_path = \"tmp_\" + save_path\n    output_video = cv2.VideoWriter(tmp_path, cv2.VideoWriter_fourcc(*\"MP4V\"), fps, (width, height))\n    \n    video_df = df.query('video_id == @video_id')\n    for _, row in tqdm(video_df.iterrows(), total=len(video_df)):\n        video_id = row.video_id\n        video_frame = row.video_frame\n        annotations_str = row.annotations\n        img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str)\n        output_video.write(img)\n    \n    output_video.release()\n    # Not all browsers support the codec, we will re-load the file at tmp_output_path\n    # and convert to a codec that is more broadly readable using ffmpeg\n    if os.path.exists(save_path):\n        os.remove(save_path)\n    subprocess.run(\n        [\"ffmpeg\", \"-i\", tmp_path, \"-crf\", \"18\", \"-preset\", \"veryfast\", \"-vcodec\", \"libx264\", save_path]\n    )\n    os.remove(tmp_path)\n\nfor video_id in list(df['video_id'].unique()):\n    make_video(df, video_id, image_dir)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T07:33:38.099422Z","iopub.execute_input":"2021-11-29T07:33:38.099650Z","iopub.status.idle":"2021-11-29T08:01:48.383619Z","shell.execute_reply.started":"2021-11-29T07:33:38.099622Z","shell.execute_reply":"2021-11-29T08:01:48.379863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Video, display\nVideo('video0.mp4')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T08:01:48.385894Z","iopub.execute_input":"2021-11-29T08:01:48.386620Z","iopub.status.idle":"2021-11-29T08:01:48.394850Z","shell.execute_reply.started":"2021-11-29T08:01:48.386582Z","shell.execute_reply":"2021-11-29T08:01:48.393824Z"},"trusted":true},"execution_count":null,"outputs":[]}]}