{"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":"markdown","source":"## How to create VIDEO files from frame sequences","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport ast\n\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-12T03:30:38.447966Z","iopub.execute_input":"2021-12-12T03:30:38.448420Z","iopub.status.idle":"2021-12-12T03:30:38.560902Z","shell.execute_reply.started":"2021-12-12T03:30:38.448310Z","shell.execute_reply":"2021-12-12T03:30:38.559577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load the train file\n","metadata":{}},{"cell_type":"code","source":"PATH = '../input/tensorflow-great-barrier-reef/train_images/video_'\n    \ndef filepaths(row):\n    v = str(row.video_id)\n    f = str(row.video_frame)\n    row['filepath'] = PATH+v+'/'+f+'.jpg'\n    return row\n\ntrain = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ntrain = train.progress_apply(filepaths, axis =1)\n\ntrain['len_ann'] = [ len(a) for a in train.annotations ]\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:30:38.563329Z","iopub.execute_input":"2021-12-12T03:30:38.563697Z","iopub.status.idle":"2021-12-12T03:30:55.974017Z","shell.execute_reply.started":"2021-12-12T03:30:38.563653Z","shell.execute_reply":"2021-12-12T03:30:55.972968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Verify the length of sequences\n\nIn video 1, sequence 18048, is the shorted video with 71 frames..","metadata":{}},{"cell_type":"code","source":"for video in train.video_id.unique():\n    for seq in train[train.video_id==video].sequence.unique():\n        print('Video:', video, '| seq:', seq,'| len:', len(train[train.sequence==seq]  ) )        ","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:30:55.975370Z","iopub.execute_input":"2021-12-12T03:30:55.975590Z","iopub.status.idle":"2021-12-12T03:30:56.023489Z","shell.execute_reply.started":"2021-12-12T03:30:55.975551Z","shell.execute_reply":"2021-12-12T03:30:56.022595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shortest Video\n# Video: 1 | seq: 18048 | len: 71\nvideo_short = train[(train.video_id ==1) & (train.sequence==18048)]\nvideo_short.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:30:56.025024Z","iopub.execute_input":"2021-12-12T03:30:56.025228Z","iopub.status.idle":"2021-12-12T03:30:56.040651Z","shell.execute_reply.started":"2021-12-12T03:30:56.025204Z","shell.execute_reply":"2021-12-12T03:30:56.039392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check if the rectangle is properly placed in the image.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport ast\n\ni = 0\n\nrec_dict=ast.literal_eval(video_short.iloc[i].annotations[1:-1])\nx,y,w,h=rec_dict['x'],rec_dict['y'],rec_dict['width'],rec_dict['height']\n\nfig, ax = plt.subplots(figsize=(20,10))\nimg = cv2.imread(video_short.filepath.tolist()[i], -1)\nimg = cv2.rectangle(img,(x,y),(x+w,y+h), (252, 3, 3), 2 )\n\nplt.imshow(img)\nplt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:30:56.041905Z","iopub.execute_input":"2021-12-12T03:30:56.042152Z","iopub.status.idle":"2021-12-12T03:30:57.016766Z","shell.execute_reply.started":"2021-12-12T03:30:56.042122Z","shell.execute_reply":"2021-12-12T03:30:57.016048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Following, create a video ('*project.avi*') from the sequence above (18048) where the rectangles are drawn in the video. \n\n","metadata":{}},{"cell_type":"code","source":"import cv2 \nimport numpy as np\nimport ast\n\nimg_array = []\n\nfor i, filename in enumerate(video_short.filepath.tolist()):\n\n    img = cv2.imread(filename)\n    rec_dict=ast.literal_eval(video_short.iloc[i].annotations) #[1:-1])\n    \n    for j in range(len(rec_dict)):\n        x,y,w,h=rec_dict[j]['x'],rec_dict[j]['y'],rec_dict[j]['width'],rec_dict[j]['height']\n\n        img = cv2.rectangle(img,(x,y),(x+w,y+h), (252, 3, 3), 2 )\n    \n    height, width, layers = img.shape\n    size = (width,height)\n    img_array.append(img)\n\nout = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)\n \nfor i in range(len(img_array)):\n    out.write(img_array[i])\nout.release()    \n    ","metadata":{"execution":{"iopub.status.busy":"2021-12-12T03:30:57.018110Z","iopub.execute_input":"2021-12-12T03:30:57.018421Z","iopub.status.idle":"2021-12-12T03:31:00.462163Z","shell.execute_reply.started":"2021-12-12T03:30:57.018382Z","shell.execute_reply":"2021-12-12T03:31:00.461416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You can download the video file from output folder and watch the sequence in your computer.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}