{"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":"## 导包","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport ast\nimport numpy as np\nimport pandas as pd\nimport matplotlib\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:09.986668Z","iopub.execute_input":"2021-12-19T08:35:09.987240Z","iopub.status.idle":"2021-12-19T08:35:10.247450Z","shell.execute_reply.started":"2021-12-19T08:35:09.987127Z","shell.execute_reply":"2021-12-19T08:35:10.246684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 导入数据集","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/tensorflow-great-barrier-reef/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.249180Z","iopub.execute_input":"2021-12-19T08:35:10.249397Z","iopub.status.idle":"2021-12-19T08:35:10.257784Z","shell.execute_reply.started":"2021-12-19T08:35:10.249371Z","shell.execute_reply":"2021-12-19T08:35:10.257112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\ntest_data = pd.read_csv(path+'test.csv')\nsamp_subm = pd.read_csv(path+'example_sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.258997Z","iopub.execute_input":"2021-12-19T08:35:10.259326Z","iopub.status.idle":"2021-12-19T08:35:10.328893Z","shell.execute_reply.started":"2021-12-19T08:35:10.259299Z","shell.execute_reply":"2021-12-19T08:35:10.327871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.330222Z","iopub.execute_input":"2021-12-19T08:35:10.330562Z","iopub.status.idle":"2021-12-19T08:35:10.350132Z","shell.execute_reply.started":"2021-12-19T08:35:10.330502Z","shell.execute_reply":"2021-12-19T08:35:10.349030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 设置Box","metadata":{}},{"cell_type":"code","source":"video_frame = 621\nfile_name = str(video_frame)+'.jpg'\ntrain_data[train_data['video_frame']==video_frame]","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.354123Z","iopub.execute_input":"2021-12-19T08:35:10.354364Z","iopub.status.idle":"2021-12-19T08:35:10.372814Z","shell.execute_reply.started":"2021-12-19T08:35:10.354333Z","shell.execute_reply":"2021-12-19T08:35:10.372059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_folder_0 = cv2.imread(path+'train_images/video_2/'+file_name)\nimage_folder_1 = cv2.imread(path+'train_images/video_1/'+file_name)\nimage_folder_2 = cv2.imread(path+'train_images/video_0/'+file_name)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.374082Z","iopub.execute_input":"2021-12-19T08:35:10.374298Z","iopub.status.idle":"2021-12-19T08:35:10.470890Z","shell.execute_reply.started":"2021-12-19T08:35:10.374272Z","shell.execute_reply":"2021-12-19T08:35:10.469998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[(train_data['video_frame']==video_frame)&(train_data['video_id']==1)]['annotations']","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.472276Z","iopub.execute_input":"2021-12-19T08:35:10.472710Z","iopub.status.idle":"2021-12-19T08:35:10.482058Z","shell.execute_reply.started":"2021-12-19T08:35:10.472664Z","shell.execute_reply":"2021-12-19T08:35:10.481254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 7329","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.483285Z","iopub.execute_input":"2021-12-19T08:35:10.483486Z","iopub.status.idle":"2021-12-19T08:35:10.489791Z","shell.execute_reply.started":"2021-12-19T08:35:10.483461Z","shell.execute_reply":"2021-12-19T08:35:10.489004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxes = ast.literal_eval(train_data.loc[row, 'annotations'])","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.491045Z","iopub.execute_input":"2021-12-19T08:35:10.491258Z","iopub.status.idle":"2021-12-19T08:35:10.506884Z","shell.execute_reply.started":"2021-12-19T08:35:10.491233Z","shell.execute_reply":"2021-12-19T08:35:10.505933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 绘图","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(20, 8))\nax.imshow(image_folder_1)\nfor box in boxes:\n    p = matplotlib.patches.Rectangle((box['x'], box['y']), box['width'], box['height'],\n                                     ec='r', fc='none', lw=2.)\n    ax.add_patch(p)\nax.set_xticklabels([])\nax.set_yticklabels([])\nplt.savefig(\"oh_no_one_reef_eater.png\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:10.508697Z","iopub.execute_input":"2021-12-19T08:35:10.509030Z","iopub.status.idle":"2021-12-19T08:35:11.373588Z","shell.execute_reply.started":"2021-12-19T08:35:10.508988Z","shell.execute_reply":"2021-12-19T08:35:11.372606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 2845\nfile_name = str(train_data.loc[row, 'video_frame'])+'.jpg'\nvideo_folder = 'video_'+str(train_data.loc[row, 'video_id'])\nboxes = ast.literal_eval(train_data.loc[row, 'annotations'])","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:11.374962Z","iopub.execute_input":"2021-12-19T08:35:11.375281Z","iopub.status.idle":"2021-12-19T08:35:11.381893Z","shell.execute_reply.started":"2021-12-19T08:35:11.375241Z","shell.execute_reply":"2021-12-19T08:35:11.380996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('video folder:', video_folder)\nprint('file name:', file_name)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:11.383309Z","iopub.execute_input":"2021-12-19T08:35:11.383516Z","iopub.status.idle":"2021-12-19T08:35:11.393896Z","shell.execute_reply.started":"2021-12-19T08:35:11.383490Z","shell.execute_reply":"2021-12-19T08:35:11.393113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(path+'train_images/'+video_folder+'/'+file_name)\nimage.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:11.395161Z","iopub.execute_input":"2021-12-19T08:35:11.395558Z","iopub.status.idle":"2021-12-19T08:35:11.435919Z","shell.execute_reply.started":"2021-12-19T08:35:11.395500Z","shell.execute_reply":"2021-12-19T08:35:11.435105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 绘图2","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(20, 8))\nax.imshow(image)\nfor box in boxes:\n    p = matplotlib.patches.Rectangle((box['x'], box['y']), box['width'], box['height'], ec='r', fc='none', lw=2.)\n    ax.add_patch(p)\nax.set_xticklabels([])\nax.set_yticklabels([])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T08:35:11.438320Z","iopub.execute_input":"2021-12-19T08:35:11.438587Z","iopub.status.idle":"2021-12-19T08:35:12.089981Z","shell.execute_reply.started":"2021-12-19T08:35:11.438550Z","shell.execute_reply":"2021-12-19T08:35:12.088134Z"},"trusted":true},"execution_count":null,"outputs":[]}]}