{"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":"!pip install moviepy\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nsns.set_theme()\nimport matplotlib.pyplot as plt\n\nimport os\nimport glob\nimport cv2\n\nfrom moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip\nfrom IPython.display import Video\nfrom sklearn.preprocessing import MinMaxScaler","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-09T19:14:43.721484Z","iopub.execute_input":"2022-08-09T19:14:43.722140Z","iopub.status.idle":"2022-08-09T19:15:22.067836Z","shell.execute_reply.started":"2022-08-09T19:14:43.722000Z","shell.execute_reply":"2022-08-09T19:15:22.066511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 0. Setup","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/dfl-bundesliga-data-shootout/train.csv\")\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.070651Z","iopub.execute_input":"2022-08-09T19:15:22.072319Z","iopub.status.idle":"2022-08-09T19:15:22.134258Z","shell.execute_reply.started":"2022-08-09T19:15:22.072174Z","shell.execute_reply":"2022-08-09T19:15:22.132995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info(verbose=True, show_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.136825Z","iopub.execute_input":"2022-08-09T19:15:22.137737Z","iopub.status.idle":"2022-08-09T19:15:22.171421Z","shell.execute_reply.started":"2022-08-09T19:15:22.137686Z","shell.execute_reply":"2022-08-09T19:15:22.170318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Data Exploration","metadata":{}},{"cell_type":"code","source":"event_attributes = train.event_attributes.unique()\nevent_attributes","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.174036Z","iopub.execute_input":"2022-08-09T19:15:22.174967Z","iopub.status.idle":"2022-08-09T19:15:22.184010Z","shell.execute_reply.started":"2022-08-09T19:15:22.174921Z","shell.execute_reply":"2022-08-09T19:15:22.182820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = train.video_id.unique()\ncnt_ids = len(ids)\nprint(f\"Average number of events in one clip: {round(len(train)/(3*cnt_ids), 2)}\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.187613Z","iopub.execute_input":"2022-08-09T19:15:22.189694Z","iopub.status.idle":"2022-08-09T19:15:22.199555Z","shell.execute_reply.started":"2022-08-09T19:15:22.189639Z","shell.execute_reply":"2022-08-09T19:15:22.198405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def play_event(row_index, before=1, after=1):\n    \n    row = train.iloc[row_index]\n    \n    if row.event == 'start':\n        start = row.time - before\n        attribute = train.iloc[row_index+1].event_attributes\n        end = train.iloc[row_index+2].time + after\n        \n    elif row.event == 'end':\n        start = train.iloc[row_index-2].time - before\n        attribute = train.iloc[row_index-1].event_attributes\n        end = row.time + after\n    \n    else:\n        start = train.iloc[row_index-1].time - before\n        attribute = row.event_attributes\n        end = train.iloc[row_index+1].time + after\n    \n    print(attribute)\n    filename = f'../input/dfl-bundesliga-data-shootout/train/{row.video_id}.mp4'\n    target = f'{row.video_id}.mp4'\n    \n    ffmpeg_extract_subclip(\n        filename, \n        start, \n        end,\n        targetname=target\n    )\n    \n    return Video(target, width=800)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.201369Z","iopub.execute_input":"2022-08-09T19:15:22.202900Z","iopub.status.idle":"2022-08-09T19:15:22.214034Z","shell.execute_reply.started":"2022-08-09T19:15:22.202851Z","shell.execute_reply":"2022-08-09T19:15:22.212715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_event(3)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.215808Z","iopub.execute_input":"2022-08-09T19:15:22.216660Z","iopub.status.idle":"2022-08-09T19:15:22.553286Z","shell.execute_reply.started":"2022-08-09T19:15:22.216613Z","shell.execute_reply":"2022-08-09T19:15:22.551734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_frame_count(filename):\n    \n    video = cv2.VideoCapture(filename)\n    frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)\n\n    return frame_count","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.556126Z","iopub.execute_input":"2022-08-09T19:15:22.557204Z","iopub.status.idle":"2022-08-09T19:15:22.565067Z","shell.execute_reply.started":"2022-08-09T19:15:22.557155Z","shell.execute_reply":"2022-08-09T19:15:22.563366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_cnt_df = pd.DataFrame(columns=['filename', 'frame_count'])\n\nfor trainfile in glob.glob('../input/dfl-bundesliga-data-shootout/train/*'):\n    cnt_entry = {\n        'filename': trainfile.split('/')[-1],\n        'frame_count': get_frame_count(trainfile)\n    }\n    frame_cnt_df = frame_cnt_df.append(cnt_entry, ignore_index=True)\n\nfig = plt.figure(figsize=(10,6))\nsns.barplot(x=\"frame_count\", y=\"filename\", data=frame_cnt_df)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:15:22.567260Z","iopub.execute_input":"2022-08-09T19:15:22.567982Z","iopub.status.idle":"2022-08-09T19:15:23.821654Z","shell.execute_reply.started":"2022-08-09T19:15:22.567943Z","shell.execute_reply":"2022-08-09T19:15:23.819629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_actions = train.dropna(axis=0).drop(['time', 'event'], axis=1)\nevent_attributes = train_actions.event_attributes.unique()\nevents = pd.DataFrame(0, index=train_actions.video_id.unique(), columns=event_attributes)\n\nfor _, row in train_actions.iterrows():\n    events.loc[row['video_id'], row['event_attributes']] += 1\n\nevents","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:20:03.557600Z","iopub.execute_input":"2022-08-09T19:20:03.558910Z","iopub.status.idle":"2022-08-09T19:20:05.009403Z","shell.execute_reply.started":"2022-08-09T19:20:03.558846Z","shell.execute_reply":"2022-08-09T19:20:05.007289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events.plot(kind='barh', stacked=True, figsize=(16,8))\nplt.legend(loc='upper right')\nplt.title(\"Event distribution among videos\", fontsize=18)\nplt.xlabel(\"Number of frames\", fontsize=12)\nplt.ylabel(\"Video Id\", fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:25:58.695886Z","iopub.execute_input":"2022-08-09T19:25:58.696379Z","iopub.status.idle":"2022-08-09T19:25:59.399865Z","shell.execute_reply.started":"2022-08-09T19:25:58.696342Z","shell.execute_reply":"2022-08-09T19:25:59.397166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_rare = events.drop(\"['pass', 'openplay']\", axis=1)\nevents_rare.plot(kind='barh', stacked=True, figsize=(16,8))\nplt.legend(loc='upper right', prop={'size': 8})\nplt.title(\"Event distribution among videos (Rare events)\", fontsize=18)\nplt.xlabel(\"Number of frames, rare events\", fontsize=12)\nplt.ylabel(\"Video Id\", fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T19:30:01.393097Z","iopub.execute_input":"2022-08-09T19:30:01.394038Z","iopub.status.idle":"2022-08-09T19:30:02.418215Z","shell.execute_reply.started":"2022-08-09T19:30:01.393998Z","shell.execute_reply":"2022-08-09T19:30:02.416234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To Be Continued ...","metadata":{}}]}