{"cells":[{"metadata":{"_uuid":"12731f025a5d8cc37a6e68aa01458d376993c56a"},"cell_type":"markdown","source":"# Recovering the Videos\nIn this notebook we will show how to view the videos contained in the [CVPR 2018 WAD Video Segmentation Challenge](https://www.kaggle.com/c/cvpr-2018-autonomous-driving) dataset. \n### Sources and Resources \n* http://tiao.io/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/\n* https://matplotlib.org/gallery/animation/dynamic_image2.html"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom skimage.io import imread \nimport matplotlib.pyplot as plt\nfrom matplotlib import animation\nfrom IPython.display import HTML\nimport os","execution_count":5,"outputs":[]},{"metadata":{"_uuid":"1752fbd8e2cd4c8d495f13594d733e2d26a4c306"},"cell_type":"markdown","source":"Here we take a peak at what files are in our training set folder. We notice here that the name of the files contain some information delimited by \" \\_\". There seems to be two ids followed by a camera indicator. "},{"metadata":{"trusted":true,"_uuid":"06039482868bc50f340c79302b9cc7333a666624"},"cell_type":"code","source":" os.listdir(\"../input/train_color\")[0:10]","execution_count":6,"outputs":[]},{"metadata":{"_uuid":"bd1520853205e9c4001362399cc71b8b26488b88"},"cell_type":"markdown","source":"Here we load the filenames into a dataframe and split the parts of the filename up so we can investigate what they are easily.  "},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"training_img_names = os.listdir(\"../input/train_color\") \ntrainingImgNameDF = pd.DataFrame( list(map(lambda s : [s]+s.split(\"_\") , training_img_names )) ).drop(3, axis=1)\ntrainingImgNameDF.head()","execution_count":2,"outputs":[]},{"metadata":{"_uuid":"ef218451f2ff50d1768fb2481ad25419b8dca481"},"cell_type":"markdown","source":"In respect to the leading id, column 1 in our dataframe, how many unique values are there? "},{"metadata":{"trusted":true,"_uuid":"c20712a240aef119d9c7dc9c217027bdba5754c7"},"cell_type":"code","source":"trainingImgNameDF[1].unique().shape","execution_count":8,"outputs":[]},{"metadata":{"_uuid":"d1ad22b9fe60c650a0533ae39e6566ca95b33296"},"cell_type":"markdown","source":"The first id seems to represent three different cars or video sessions. How many unique camera angles are there? "},{"metadata":{"trusted":true,"_uuid":"ee12771ec9f1af8f68f01bdd115bc42d72f72ca8"},"cell_type":"code","source":"trainingImgNameDF[4].unique().shape","execution_count":46,"outputs":[]},{"metadata":{"_uuid":"55914e7147d32ce3ba5b22840b2ab9f0d37fe74c"},"cell_type":"markdown","source":"We now define a function for creating little html videos from the video frames in the dataset. "},{"metadata":{"_cell_guid":"9876c47c-4405-49ba-99e8-67f161afb9e7","_uuid":"5c40543b6bca827774a6cf995c39aa223b2b9247","trusted":true},"cell_type":"code","source":"def visVid(trainingImgNameDF,sessionID,cameraID,startFrame=0,endFrame=10,dataRoot=\"../input/train_color/\"):\n    res = trainingImgNameDF[ (trainingImgNameDF[1] == sessionID) & (trainingImgNameDF[4] == cameraID)].sort_values(by=2)\n    imgURIs = list(res[0])\n    \n    fig = plt.figure(figsize=(10,10))\n    frames = []\n    for uri in imgURIs[startFrame:endFrame]:\n        im = plt.imshow( imread(dataRoot+uri), animated=True)\n        frames.append([im])\n    \n    plt.close(fig)\n    return animation.ArtistAnimation(fig,frames,interval=50, blit=True,repeat_delay=1000)\n    ","execution_count":47,"outputs":[]},{"metadata":{"_uuid":"f202e7ecfc4d52ff2b03279c0b57a92004857f21"},"cell_type":"markdown","source":"Here we create a video of 10 frames from the first session id's left camera ( 5.jpg ).  \n\n*Make sure you press play on the video.*"},{"metadata":{"_cell_guid":"fb2d8647-0da6-478a-9e5a-e41a53788371","_uuid":"4fb0ff383571c1fad07b03b1053403c18167b4c8","trusted":true},"cell_type":"code","source":"uniqueVideoID = trainingImgNameDF[1].unique()\nani = visVid(trainingImgNameDF,uniqueVideoID[0],\"5.jpg\")\nHTML(ani.to_jshtml(default_mode=\"reflect\"))","execution_count":48,"outputs":[]},{"metadata":{"_uuid":"fc6a82b55dd81f93e2c8f4217099b80a85c0446d"},"cell_type":"markdown","source":"Here we create another video with the frames of the right camera ( 6.jpg ). "},{"metadata":{"_cell_guid":"30b5af6a-617d-4356-aae0-649e25722afd","_uuid":"3cbfd04e7acf3c6f03521cec48c1429af8967a4e","trusted":true},"cell_type":"code","source":"uniqueVideoID = trainingImgNameDF[1].unique()\nani = visVid(trainingImgNameDF,uniqueVideoID[0],\"6.jpg\")\nHTML(ani.to_jshtml(default_mode=\"reflect\"))","execution_count":50,"outputs":[]},{"metadata":{"_uuid":"34a15425d25ddd25c5293671f32ba3cb760ee406"},"cell_type":"markdown","source":"As we can see there seems to be three recorded driving sessions in the dataset with each frame of video having a left camera (5.jpg) and right camera(6.jpg) output. Fork this notebook and play with the parameters to view different parts of the videos and give this kernel an upvote if you found this useful. "}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}