{"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":"#import necessary library\nimport os\nimport matplotlib.pyplot as plt\nimport numpy as np \nimport pandas as pd \nimport ast\nfrom matplotlib.patches import Rectangle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-09T10:29:51.06297Z","iopub.execute_input":"2022-01-09T10:29:51.063518Z","iopub.status.idle":"2022-01-09T10:29:51.068008Z","shell.execute_reply.started":"2022-01-09T10:29:51.063483Z","shell.execute_reply":"2022-01-09T10:29:51.067264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#count of total imagea in each folder\nprint(\"Total Number of frames in Video_0:\")\nprint(len(os.listdir(\"../input/tensorflow-great-barrier-reef/train_images/video_0/\")))\nprint(\"Total Number of frames in Video_1:\")\nprint(len(os.listdir(\"../input/tensorflow-great-barrier-reef/train_images/video_1/\")))\nprint(\"Total Number of frames in Video_2:\")\nprint(len(os.listdir(\"../input/tensorflow-great-barrier-reef/train_images/video_2/\")))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T09:06:41.678059Z","iopub.execute_input":"2022-01-09T09:06:41.678313Z","iopub.status.idle":"2022-01-09T09:06:41.998211Z","shell.execute_reply.started":"2022-01-09T09:06:41.678286Z","shell.execute_reply":"2022-01-09T09:06:41.997384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check sample image in frame of video_0\nimg=plt.imread(\"../input/tensorflow-great-barrier-reef/train_images/video_0/\"+os.listdir(\"../input/tensorflow-great-barrier-reef/train_images/video_2/\")[2])\nplt.rcParams[\"figure.figsize\"] = (10,10)\nplt.axis(\"off\")\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T09:11:06.082905Z","iopub.execute_input":"2022-01-09T09:11:06.083302Z","iopub.status.idle":"2022-01-09T09:11:06.544709Z","shell.execute_reply.started":"2022-01-09T09:11:06.083271Z","shell.execute_reply":"2022-01-09T09:11:06.543858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"checking cordinates of frames","metadata":{}},{"cell_type":"code","source":"#load annotation data\ntrain=pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\n#displaying 10 rows from 100 to 110 in dataframe 'train'\ntrain.iloc[100:110]","metadata":{"execution":{"iopub.status.busy":"2022-01-09T09:18:16.907588Z","iopub.execute_input":"2022-01-09T09:18:16.907985Z","iopub.status.idle":"2022-01-09T09:18:16.94947Z","shell.execute_reply.started":"2022-01-09T09:18:16.907956Z","shell.execute_reply":"2022-01-09T09:18:16.948809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking the number of rows and columns in dataframe\nprint(\"Rows:\",train.shape[0])\nprint(\"Columns:\",train.shape[1])","metadata":{"execution":{"iopub.status.busy":"2022-01-09T09:21:03.593547Z","iopub.execute_input":"2022-01-09T09:21:03.593842Z","iopub.status.idle":"2022-01-09T09:21:03.600218Z","shell.execute_reply.started":"2022-01-09T09:21:03.59381Z","shell.execute_reply":"2022-01-09T09:21:03.59934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check if there are more than one annnotation for a frame (concl. there are more than one annotation for a frame)\nannotation_len=[]\nfor i in range(0,len(train),5):\n    ann=ast.literal_eval(train.iloc[i,5])\n    annotation_len.append(len(ann))\nprint(\"First Annotation length\",annotation_len[0])\nprint(\"Minimum annotation in one frame\",min(annotation_len))\nprint(\"Maximum annotation in one frame\",max(annotation_len))\nprint(\"mean of Annotation length\",np.mean(annotation_len))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T10:59:03.771466Z","iopub.execute_input":"2022-01-09T10:59:03.771785Z","iopub.status.idle":"2022-01-09T10:59:04.031317Z","shell.execute_reply.started":"2022-01-09T10:59:03.771739Z","shell.execute_reply":"2022-01-09T10:59:04.030325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#converts the annotation of string format  to list containing x,y,width,height in integer format\ndef get_bboxes(annotations):\n    annotations=ast.literal_eval(annotations)\n    if len(annotations)==0:\n        return [[0,0,0,0]]\n    boxes = pd.DataFrame(annotations, columns=['x', 'y', 'width', 'height']).astype(np.int32).values\n    return boxes \n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T10:43:53.763919Z","iopub.execute_input":"2022-01-09T10:43:53.764217Z","iopub.status.idle":"2022-01-09T10:43:53.76981Z","shell.execute_reply.started":"2022-01-09T10:43:53.764178Z","shell.execute_reply":"2022-01-09T10:43:53.768915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plotting annotation over image\ndef plot_annotation_on_img(video_id,video_frame,annotations):\n    bbox=get_bboxes(annotations)\n    img_url=\"../input/tensorflow-great-barrier-reef/train_images/video_\"+str(video_id)+\"/\"+str(video_frame)+\".jpg\"\n    img=plt.imread(img_url)\n    for i in range(len(bbox)):        \n        plt.gca().add_patch(Rectangle((bbox[i][0] ,bbox[i][1]), bbox[i][2], bbox[i][3],edgecolor='red',\n                        facecolor='none',\n                        lw=4))\n    plt.rcParams[\"figure.figsize\"] = (15,15)\n    plt.axis(\"off\")\n    plt.imshow(img)\n    print(video_id,video_frame,annotations)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T10:51:24.042534Z","iopub.execute_input":"2022-01-09T10:51:24.042822Z","iopub.status.idle":"2022-01-09T10:51:24.050154Z","shell.execute_reply.started":"2022-01-09T10:51:24.04279Z","shell.execute_reply":"2022-01-09T10:51:24.04935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#given imnage number the annotation are draan on image\nimg_no=1345\nvi=train.iloc[img_no,0]\nvf=train.iloc[img_no,2]\nann=train.iloc[img_no,5]\nplot_annotation_on_img(vi,vf,ann)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T10:58:00.368124Z","iopub.execute_input":"2022-01-09T10:58:00.36843Z","iopub.status.idle":"2022-01-09T10:58:00.961615Z","shell.execute_reply.started":"2022-01-09T10:58:00.368391Z","shell.execute_reply":"2022-01-09T10:58:00.960793Z"},"trusted":true},"execution_count":null,"outputs":[]}]}