{"cells":[{"metadata":{},"cell_type":"markdown","source":"### Data Description :\nMuch of the Text was taken from the official page \nThis dataset contains photos of streets, taken from the roof of a car. We're attempting to predict the position and orientation of all un-masked cars in the test images. You should also provide a confidence score indicating how sure you are of your prediction.\n\nPose Information (train.csv) Note that rotation values are angles expressed in radians, relative to the camera.\n\nThe primary data is images of cars and related pose information. The pose information is formatted as strings, as follows: model type, yaw, pitch, roll, x, y, z\n\nA concrete example with two cars in the photo: 5 0.5 0.5 0.5 0.0 0.0 0.0 32 0.25 0.25 0.25 0.5 0.4 0.7\n\nSubmissions (per sample_submission.csv) are very similar, with the addition of a confidence score, and the removal of the model type. You are not required to predict the model type of the vehicle in question.\n\nID, PredictionString ID_1d7bc9b31,0.5 0.5 0.5 0.0 0.0 0.0 1.0 indicating that this prediction has a confidence score of 1.0.\n\nOther Data:\n\nImage Masks (test_masks.zip / train_masks.zip) Some cars in the images are not of interest (too far away, etc.). Binary masks are provided to allow competitors to remove them from consideration.\n\nCar Models 3D models of all cars of interest are available for download as pickle files - they can be compared against cars in images, used as references for rotation, etc."},{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom skimage import io\n# Input data files are available in the \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"trainData =  pd.read_csv(\"../input/pku-autonomous-driving/train.csv\")\ntrainData.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In above dataframe trainData, let us visualize the first five image. We are having all the image in train_images.zip directory."},{"metadata":{},"cell_type":"markdown","source":"### How many image do we have in train data ?"},{"metadata":{"trusted":true},"cell_type":"code","source":"trainData.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainpath = \"../input/pku-autonomous-driving/train_images/\" ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Image ID_8a6e65317.jpg"},{"metadata":{"trusted":true},"cell_type":"code","source":"img0 = io.imread(trainpath+\"ID_8a6e65317.jpg\")\nio.imshow(img0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Image ID_337ddc495.jpg"},{"metadata":{"trusted":true},"cell_type":"code","source":"img1 = io.imread(trainpath+\"ID_337ddc495.jpg\")\nio.imshow(img1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"img2 = io.imread(trainpath+\"ID_a381bf4d0.jpg\")\nio.imshow(img2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"img3 = io.imread(trainpath+\"ID_7c4a3e0aa.jpg\")\nio.imshow(img3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### What is inside masks "},{"metadata":{"trusted":true},"cell_type":"code","source":"maskpath = \"../input/pku-autonomous-driving/train_masks/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img0 = io.imread(trainpath+\"ID_8a6e65317.jpg\")\nfig = plt.figure(figsize=(20,10))\n\nax1 = fig.add_subplot(1,2,1)\nax1.imshow(img0)\nimg0m = io.imread(maskpath+\"ID_8a6e65317.jpg\")\nax2 = fig.add_subplot(1,2,2)\nax2.imshow(img0m)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### For ID_8a6e65317.jpg How many car are in picture."},{"metadata":{"trusted":true},"cell_type":"code","source":"trainData.PredictionString[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### Every string has data of more than vehicles in Image ID_8a6e65317.jpg. \nnumOfCars = len(trainData.PredictionString[0].split(\" \"))/7\nprint(\"Number of cars in image ID_8a6e65317.jpg is : \", numOfCars)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"It seems that, list of dictionary will be best to possecces this sort of data as follows."},{"metadata":{"trusted":true},"cell_type":"code","source":"data = trainData.PredictionString[0]\ndataList = data.split(\" \")\nnumOfVehicles = len(dataList) /7\nvariables = [\"modeltype\", \"yaw\", \"pitch\", \"roll\", \"x\", \"y\", \"z\"]\nlistOfData = []\nfor i in range(0,len(dataList),7) :\n    \n    lastIndex = i+7\n    dt = dataList[i:lastIndex:1]\n    dct = dict(zip(variables,dt))\n    listOfData.append(dct)\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"listOfData","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let us do the same analysis for second image data in list "},{"metadata":{"trusted":true},"cell_type":"code","source":"img1 = io.imread(trainpath+\"ID_337ddc495.jpg\")\nfig = plt.figure(figsize=(20,10))\n\nax1 = fig.add_subplot(1,2,1)\nax1.imshow(img0)\nimg1m = io.imread(maskpath+\"ID_337ddc495.jpg\")\nax2 = fig.add_subplot(1,2,2)\nax2.imshow(img0m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img1m.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### All layers in mask image has same data. That we can visualize from following code line"},{"metadata":{"trusted":true},"cell_type":"code","source":"(img1m[0,:,:] == img1m[1,:,:]).all()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"numOfCars = len(trainData.PredictionString[1].split(\" \"))/7\nprint(\"Number of cars in image ID_337ddc495.jpg is : \", numOfCars)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"### Let us create a new column in our dataframe trainData, which consists of dictionaries of vehicle data."},{"metadata":{"trusted":true},"cell_type":"code","source":"data = trainData.PredictionString[0]\ndef addDictionary(data) :\n\n    dataList = data.split(\" \")\n    numOfVehicles = len(dataList) /7\n    variables = [\"modeltype\", \"yaw\", \"pitch\", \"roll\", \"x\", \"y\", \"z\"]\n    listOfData = []\n    for i in range(0,len(dataList),7) :\n    \n        lastIndex = i+7\n        dt = dataList[i:lastIndex:1]\n        dct = dict(zip(variables,dt))\n        listOfData.append(dct)\n    return listOfData\naddDictionary(data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainData[\"dictVal\"] = trainData.PredictionString.apply(lambda x : addDictionary(x))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainData.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Calculating number of vehicles in each pictures."},{"metadata":{"trusted":true},"cell_type":"code","source":"trainData[\"noOfVehicle\"] = trainData.dictVal.apply(lambda x : len(x))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainData.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### What is average number of vehicles in each picture "},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Average number of vehicles in pictures : \",trainData[\"noOfVehicle\"].mean())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# To be continued ...."},{"metadata":{},"cell_type":"markdown","source":"# Kindly upvote if you like it :)"},{"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":1}