{"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":"# 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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../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\nimport cv2\nfrom matplotlib import pyplot as plt\n\nfrom tqdm import tqdm\nimport seaborn as sns\nimport warnings\nimport ast\nwarnings.filterwarnings(\"ignore\")\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-05T08:16:58.011853Z","iopub.execute_input":"2021-12-05T08:16:58.012163Z","iopub.status.idle":"2021-12-05T08:16:58.017839Z","shell.execute_reply.started":"2021-12-05T08:16:58.012109Z","shell.execute_reply":"2021-12-05T08:16:58.016962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dir='../input/tensorflow-great-barrier-reef/train_images/video_0'","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:16:58.019616Z","iopub.execute_input":"2021-12-05T08:16:58.020014Z","iopub.status.idle":"2021-12-05T08:16:58.039404Z","shell.execute_reply.started":"2021-12-05T08:16:58.019969Z","shell.execute_reply":"2021-12-05T08:16:58.038672Z"},"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-12-05T08:16:58.040537Z","iopub.execute_input":"2021-12-05T08:16:58.040911Z","iopub.status.idle":"2021-12-05T08:16:58.096335Z","shell.execute_reply.started":"2021-12-05T08:16:58.040881Z","shell.execute_reply":"2021-12-05T08:16:58.095328Z"},"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-12-05T08:16:58.097753Z","iopub.execute_input":"2021-12-05T08:16:58.098033Z","iopub.status.idle":"2021-12-05T08:16:58.106560Z","shell.execute_reply.started":"2021-12-05T08:16:58.098001Z","shell.execute_reply":"2021-12-05T08:16:58.105657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stri=train0.loc[100,'annotations'][1:-1]\ndicti = ast.literal_eval(stri)\nprint(dicti)\nprint(dicti['width'])","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:16:58.109439Z","iopub.execute_input":"2021-12-05T08:16:58.109917Z","iopub.status.idle":"2021-12-05T08:16:58.118152Z","shell.execute_reply.started":"2021-12-05T08:16:58.109874Z","shell.execute_reply":"2021-12-05T08:16:58.117193Z"},"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-12-05T08:16:58.120178Z","iopub.execute_input":"2021-12-05T08:16:58.120520Z","iopub.status.idle":"2021-12-05T08:16:58.262867Z","shell.execute_reply.started":"2021-12-05T08:16:58.120477Z","shell.execute_reply":"2021-12-05T08:16:58.261887Z"},"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":{"execution":{"iopub.status.busy":"2021-12-05T08:16:58.264254Z","iopub.execute_input":"2021-12-05T08:16:58.264570Z","iopub.status.idle":"2021-12-05T08:16:58.282329Z","shell.execute_reply.started":"2021-12-05T08:16:58.264529Z","shell.execute_reply":"2021-12-05T08:16:58.281327Z"},"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":{"execution":{"iopub.status.busy":"2021-12-05T08:16:58.283975Z","iopub.execute_input":"2021-12-05T08:16:58.284317Z","iopub.status.idle":"2021-12-05T08:17:03.679649Z","shell.execute_reply.started":"2021-12-05T08:16:58.284274Z","shell.execute_reply":"2021-12-05T08:17:03.679102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(DICT0[16]))\nDICT0[16]","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:17:03.680662Z","iopub.execute_input":"2021-12-05T08:17:03.680987Z","iopub.status.idle":"2021-12-05T08:17:03.688181Z","shell.execute_reply.started":"2021-12-05T08:17:03.680951Z","shell.execute_reply":"2021-12-05T08:17:03.687173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(DICT0[50]))\nDICT0[50]","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:17:03.689403Z","iopub.execute_input":"2021-12-05T08:17:03.689616Z","iopub.status.idle":"2021-12-05T08:17:03.701147Z","shell.execute_reply.started":"2021-12-05T08:17:03.689593Z","shell.execute_reply":"2021-12-05T08:17:03.699999Z"},"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":{"execution":{"iopub.status.busy":"2021-12-05T08:17:03.702379Z","iopub.execute_input":"2021-12-05T08:17:03.702629Z","iopub.status.idle":"2021-12-05T08:17:03.715261Z","shell.execute_reply.started":"2021-12-05T08:17:03.702602Z","shell.execute_reply":"2021-12-05T08:17:03.714423Z"},"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":{"execution":{"iopub.status.busy":"2021-12-05T08:17:03.716427Z","iopub.execute_input":"2021-12-05T08:17:03.716736Z","iopub.status.idle":"2021-12-05T08:17:08.965095Z","shell.execute_reply.started":"2021-12-05T08:17:03.716696Z","shell.execute_reply":"2021-12-05T08:17:08.964244Z"},"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-12-05T08:17:08.966373Z","iopub.execute_input":"2021-12-05T08:17:08.966703Z","iopub.status.idle":"2021-12-05T08:17:09.385410Z","shell.execute_reply.started":"2021-12-05T08:17:08.966661Z","shell.execute_reply":"2021-12-05T08:17:09.384542Z"},"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-12-05T08:17:09.388004Z","iopub.execute_input":"2021-12-05T08:17:09.388268Z","iopub.status.idle":"2021-12-05T08:17:09.802596Z","shell.execute_reply.started":"2021-12-05T08:17:09.388237Z","shell.execute_reply":"2021-12-05T08:17:09.801772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images1)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:17:09.803706Z","iopub.execute_input":"2021-12-05T08:17:09.803918Z","iopub.status.idle":"2021-12-05T08:17:09.809396Z","shell.execute_reply.started":"2021-12-05T08:17:09.803894Z","shell.execute_reply":"2021-12-05T08:17:09.808646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 动画","metadata":{}},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:17:09.810456Z","iopub.execute_input":"2021-12-05T08:17:09.810650Z","iopub.status.idle":"2021-12-05T08:17:09.821763Z","shell.execute_reply.started":"2021-12-05T08:17:09.810626Z","shell.execute_reply":"2021-12-05T08:17:09.820866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):  \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":{"execution":{"iopub.status.busy":"2021-12-05T08:17:09.823553Z","iopub.execute_input":"2021-12-05T08:17:09.823852Z","iopub.status.idle":"2021-12-05T08:17:09.834147Z","shell.execute_reply.started":"2021-12-05T08:17:09.823813Z","shell.execute_reply":"2021-12-05T08:17:09.833178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images1[20:70])","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:17:09.835377Z","iopub.execute_input":"2021-12-05T08:17:09.835684Z","iopub.status.idle":"2021-12-05T08:17:18.607441Z","shell.execute_reply.started":"2021-12-05T08:17:09.835657Z","shell.execute_reply":"2021-12-05T08:17:18.606583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}