{"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":"# Great Barrier Reef Animation\nhttps://www.kaggle.com/stpeteishii/wind-turbines-yolo-annotation-view","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nfrom matplotlib import pyplot as plt\n\nfrom tqdm import tqdm\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.902299,"end_time":"2021-09-16T12:39:03.430784","exception":false,"start_time":"2021-09-16T12:39:02.528485","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:41.963026Z","iopub.execute_input":"2021-11-23T14:21:41.963489Z","iopub.status.idle":"2021-11-23T14:21:41.973888Z","shell.execute_reply.started":"2021-11-23T14:21:41.963452Z","shell.execute_reply":"2021-11-23T14:21:41.969356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dir='../input/tensorflow-great-barrier-reef/train_images/video_0'","metadata":{"papermill":{"duration":0.017802,"end_time":"2021-09-16T12:39:03.462414","exception":false,"start_time":"2021-09-16T12:39:03.444612","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:41.975496Z","iopub.execute_input":"2021-11-23T14:21:41.977398Z","iopub.status.idle":"2021-11-23T14:21:41.986424Z","shell.execute_reply.started":"2021-11-23T14:21:41.977360Z","shell.execute_reply":"2021-11-23T14:21:41.985058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:41.990254Z","iopub.execute_input":"2021-11-23T14:21:41.991415Z","iopub.status.idle":"2021-11-23T14:21:42.042434Z","shell.execute_reply.started":"2021-11-23T14:21:41.991366Z","shell.execute_reply":"2021-11-23T14:21:42.041360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0=train[train['video_id']==0]\nprint(train0['annotations'].unique().tolist()[3])\nprint(len(train0))","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:42.043946Z","iopub.execute_input":"2021-11-23T14:21:42.045602Z","iopub.status.idle":"2021-11-23T14:21:42.058913Z","shell.execute_reply.started":"2021-11-23T14:21:42.045556Z","shell.execute_reply":"2021-11-23T14:21:42.057037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ast\nstri=train0.loc[100,'annotations'][1:-1]\ndicti = ast.literal_eval(stri)\nprint(dicti)\nprint(dicti['width'])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:42.063617Z","iopub.execute_input":"2021-11-23T14:21:42.064453Z","iopub.status.idle":"2021-11-23T14:21:42.074942Z","shell.execute_reply.started":"2021-11-23T14:21:42.064404Z","shell.execute_reply":"2021-11-23T14:21:42.073717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DICT0=[]\nfor i in range(len(train0)):\n    if train0.loc[i,'annotations']!='[]':\n        stri=train0.loc[i,'annotations'][1:-1]\n        DICT0+= [ast.literal_eval(stri)]\n    else:\n        DICT0+=[{}]\nprint(DICT0[50:55])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:42.076802Z","iopub.execute_input":"2021-11-23T14:21:42.077444Z","iopub.status.idle":"2021-11-23T14:21:42.328063Z","shell.execute_reply.started":"2021-11-23T14:21:42.077382Z","shell.execute_reply":"2021-11-23T14:21:42.327008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"impaths=[]\nfor i in range(len(train0)):\n    impaths+=[os.path.join(img_dir,str(i)+'.jpg')]","metadata":{"papermill":{"duration":0.506804,"end_time":"2021-09-16T12:39:03.980541","exception":false,"start_time":"2021-09-16T12:39:03.473737","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:42.330204Z","iopub.execute_input":"2021-11-23T14:21:42.330670Z","iopub.status.idle":"2021-11-23T14:21:42.355972Z","shell.execute_reply.started":"2021-11-23T14:21:42.330628Z","shell.execute_reply":"2021-11-23T14:21:42.354868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images0=[]\n#for i in tqdm(range(len(impaths))):\nfor i in tqdm(range(200)):\n    images0+=[cv2.imread(impaths[i])]","metadata":{"papermill":{"duration":53.920513,"end_time":"2021-09-16T12:39:57.912867","exception":false,"start_time":"2021-09-16T12:39:03.992354","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:42.358232Z","iopub.execute_input":"2021-11-23T14:21:42.358919Z","iopub.status.idle":"2021-11-23T14:21:47.005762Z","shell.execute_reply.started":"2021-11-23T14:21:42.358864Z","shell.execute_reply":"2021-11-23T14:21:47.004763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!mkdir source0","metadata":{"papermill":{"duration":0.137892,"end_time":"2021-09-16T12:40:19.374917","exception":false,"start_time":"2021-09-16T12:40:19.237025","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:47.007446Z","iopub.execute_input":"2021-11-23T14:21:47.007977Z","iopub.status.idle":"2021-11-23T14:21:47.012724Z","shell.execute_reply.started":"2021-11-23T14:21:47.007936Z","shell.execute_reply":"2021-11-23T14:21:47.011429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(DICT0[16]))\nDICT0[16]","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:47.016553Z","iopub.execute_input":"2021-11-23T14:21:47.017286Z","iopub.status.idle":"2021-11-23T14:21:47.030660Z","shell.execute_reply.started":"2021-11-23T14:21:47.017216Z","shell.execute_reply":"2021-11-23T14:21:47.029445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(DICT0[50]))\nDICT0[50]","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:47.032127Z","iopub.execute_input":"2021-11-23T14:21:47.032850Z","iopub.status.idle":"2021-11-23T14:21:47.043251Z","shell.execute_reply.started":"2021-11-23T14:21:47.032803Z","shell.execute_reply":"2021-11-23T14:21:47.041805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_box(num0):\n    \n    impath=impaths[num0]\n    image=cv2.imread(impath)\n    #H,W=image.shape[0],image.shape[1]\n    #file=impath[0:-4].split('/')[-1]\n    #print(file, DICT0[num0])\n\n    item = DICT0[num0]       \n    if item!={} and type(item)==dict:\n        x=item['x']\n        y=item['y']\n        w=item['width'] \n        h=item['height']\n        x0=int(x-w/2)\n        y0=int(y-h/2)\n        x1=int(x+w/2)\n        y1=int(y+h/2)\n\n        cv2.rectangle(image,(x0,y0),(x1,y1),(0,0,255),4) \n        #print('dict')\n\n    elif item!={} and type(item)==tuple:\n        for item2 in item:\n            x=item2['x']\n            y=item2['y']\n            w=item2['width'] \n            h=item2['height']\n            x0=int(x-w/2)\n            y0=int(y-h/2)\n            x1=int(x+w/2)\n            y1=int(y+h/2)\n\n            cv2.rectangle(image,(x0,y0),(x1,y1),(0,0,255),4)\n            #print('tuple')\n\n        #cv2.imwrite('/kaggle/working/source0/'+file+'.png',image)        \n        #plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n        #plt.show() \n        \n    return image","metadata":{"papermill":{"duration":0.144845,"end_time":"2021-09-16T12:40:19.652307","exception":false,"start_time":"2021-09-16T12:40:19.507462","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:47.045520Z","iopub.execute_input":"2021-11-23T14:21:47.046266Z","iopub.status.idle":"2021-11-23T14:21:47.059061Z","shell.execute_reply.started":"2021-11-23T14:21:47.046220Z","shell.execute_reply":"2021-11-23T14:21:47.057974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images1=[]\n#for i in tqdm(range(len(impaths))):\nfor i in tqdm(range(200)):\n    images1+=[draw_box(i)]","metadata":{"papermill":{"duration":37.939256,"end_time":"2021-09-16T12:40:57.723479","exception":false,"start_time":"2021-09-16T12:40:19.784223","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:47.061123Z","iopub.execute_input":"2021-11-23T14:21:47.061901Z","iopub.status.idle":"2021-11-23T14:21:51.796355Z","shell.execute_reply.started":"2021-11-23T14:21:47.061856Z","shell.execute_reply":"2021-11-23T14:21:51.795273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(10,10))\n#plt.axis('off')\nplt.imshow(cv2.cvtColor(images0[16],cv2.COLOR_BGR2RGB))","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:51.798178Z","iopub.execute_input":"2021-11-23T14:21:51.798535Z","iopub.status.idle":"2021-11-23T14:21:52.294069Z","shell.execute_reply.started":"2021-11-23T14:21:51.798480Z","shell.execute_reply":"2021-11-23T14:21:52.293213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(10,10))\n#plt.axis('off')\nplt.imshow(cv2.cvtColor(images1[16],cv2.COLOR_BGR2RGB))","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:52.295644Z","iopub.execute_input":"2021-11-23T14:21:52.296033Z","iopub.status.idle":"2021-11-23T14:21:52.778727Z","shell.execute_reply.started":"2021-11-23T14:21:52.295995Z","shell.execute_reply":"2021-11-23T14:21:52.777385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images1)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T14:21:52.780329Z","iopub.execute_input":"2021-11-23T14:21:52.781063Z","iopub.status.idle":"2021-11-23T14:21:52.788821Z","shell.execute_reply.started":"2021-11-23T14:21:52.781023Z","shell.execute_reply":"2021-11-23T14:21:52.787703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Animation\nhttps://matplotlib.org/stable/api/animation_api.html","metadata":{"papermill":{"duration":0.218793,"end_time":"2021-09-16T12:40:58.339175","exception":false,"start_time":"2021-09-16T12:40:58.120382","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"papermill":{"duration":0.227271,"end_time":"2021-09-16T12:40:58.805788","exception":false,"start_time":"2021-09-16T12:40:58.578517","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:52.790581Z","iopub.execute_input":"2021-11-23T14:21:52.791562Z","iopub.status.idle":"2021-11-23T14:21:52.800456Z","shell.execute_reply.started":"2021-11-23T14:21:52.791302Z","shell.execute_reply":"2021-11-23T14:21:52.799136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):\n    \n    fig=plt.figure(figsize=(12,6))\n    plt.axis('off')\n    im=plt.imshow(cv2.cvtColor(ims[0],cv2.COLOR_BGR2RGB))\n    \n    def animate_func(i):\n        im.set_array(cv2.cvtColor(ims[i],cv2.COLOR_BGR2RGB))\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), \n                                   interval=1000//8)","metadata":{"papermill":{"duration":0.228922,"end_time":"2021-09-16T12:40:59.254781","exception":false,"start_time":"2021-09-16T12:40:59.025859","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:52.802955Z","iopub.execute_input":"2021-11-23T14:21:52.803315Z","iopub.status.idle":"2021-11-23T14:21:52.823393Z","shell.execute_reply.started":"2021-11-23T14:21:52.803256Z","shell.execute_reply":"2021-11-23T14:21:52.819969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images1[10:70])","metadata":{"papermill":{"duration":72.073408,"end_time":"2021-09-16T12:42:11.547137","exception":false,"start_time":"2021-09-16T12:40:59.473729","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T14:21:52.828845Z","iopub.execute_input":"2021-11-23T14:21:52.829455Z","iopub.status.idle":"2021-11-23T14:22:01.626228Z","shell.execute_reply.started":"2021-11-23T14:21:52.829411Z","shell.execute_reply":"2021-11-23T14:22:01.625362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.840008,"end_time":"2021-09-16T12:42:13.225946","exception":false,"start_time":"2021-09-16T12:42:12.385938","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":1.080246,"end_time":"2021-09-16T12:42:15.151430","exception":false,"start_time":"2021-09-16T12:42:14.071184","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.98322,"end_time":"2021-09-16T12:42:16.981442","exception":false,"start_time":"2021-09-16T12:42:15.998222","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}