{"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":"# Make Image-files for Annotation  \n\n[Objectives]\n- Create image files from video files (.mp4) for annotation.  \n- Capture and save the video of the event detection time as an image.   ","metadata":{}},{"cell_type":"code","source":"!pip install moviepy -q","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:22:45.376414Z","iopub.execute_input":"2022-08-06T01:22:45.376874Z","iopub.status.idle":"2022-08-06T01:23:12.515621Z","shell.execute_reply.started":"2022-08-06T01:22:45.376835Z","shell.execute_reply":"2022-08-06T01:23:12.514149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#==================\n# Import Libralies\n#==================\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\n\nimport cv2\n\nfrom pathlib import Path \nfrom typing import Union, Tuple, List, Iterable, Dict \n\nimport matplotlib.pyplot as plt\nimport seaborn as sn\nplt.style.use(['default']) \n\nimport datetime\n\nfrom moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip\nfrom IPython.display import Video\ndate = str(datetime.date.today()) \ndate = date.replace('-','')\nprint(f'Today is {date}')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-06T01:23:12.518257Z","iopub.execute_input":"2022-08-06T01:23:12.518621Z","iopub.status.idle":"2022-08-06T01:23:12.892934Z","shell.execute_reply.started":"2022-08-06T01:23:12.518588Z","shell.execute_reply":"2022-08-06T01:23:12.891661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#==================\n# Config\n#==================\nclass config:\n    train_dir = \"../input/dfl-bundesliga-data-shootout/train\"\n    video_dir = \"../input/dfl-bundesliga-data-shootout/clips\"\n    image_dir = \"./\"\n    \ncfg = config()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:26:16.991053Z","iopub.execute_input":"2022-08-06T01:26:16.992240Z","iopub.status.idle":"2022-08-06T01:26:16.998670Z","shell.execute_reply.started":"2022-08-06T01:26:16.992187Z","shell.execute_reply":"2022-08-06T01:26:16.997751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read train.csv\ndf = pd.read_csv(\"../input/dfl-bundesliga-data-shootout/train.csv\") \ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:25:28.306103Z","iopub.execute_input":"2022-08-06T01:25:28.307231Z","iopub.status.idle":"2022-08-06T01:25:28.362565Z","shell.execute_reply.started":"2022-08-06T01:25:28.307179Z","shell.execute_reply":"2022-08-06T01:25:28.361344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dataframe only about event   \nperfect_submission = (\n    df\n    .query(\"event not in ['start', 'end']\")\n    .reset_index(drop=True)\n    .assign(score = 1.0)\n)\nperfect_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:25:29.561988Z","iopub.execute_input":"2022-08-06T01:25:29.562694Z","iopub.status.idle":"2022-08-06T01:25:29.592092Z","shell.execute_reply.started":"2022-08-06T01:25:29.562651Z","shell.execute_reply":"2022-08-06T01:25:29.591042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get video file list\nvideo_list = os.listdir(cfg.train_dir)\nvideo_list","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:25:31.759350Z","iopub.execute_input":"2022-08-06T01:25:31.760094Z","iopub.status.idle":"2022-08-06T01:25:31.775600Z","shell.execute_reply.started":"2022-08-06T01:25:31.760056Z","shell.execute_reply":"2022-08-06T01:25:31.774206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_frame_sec(video_path, sec, result_path):\n    \"\"\"\n    Save an image file from a video file.\n    ----------------------------------------\n    video_path(Path): Movie directory\n    sec(float)       : Event detection time\n    result_path(Path): Save directory\n    \"\"\"\n    cap = cv2.VideoCapture(video_path)\n    if not cap.isOpened():\n        return\n    os.makedirs(os.path.dirname(result_path), exist_ok=True)\n    fps = cap.get(cv2.CAP_PROP_FPS)\n\n    cap.set(cv2.CAP_PROP_POS_FRAMES, round(fps * sec))\n\n    ret, frame = cap.read()\n\n    if ret:\n        cv2.imwrite(result_path, frame)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:25:33.586444Z","iopub.execute_input":"2022-08-06T01:25:33.587541Z","iopub.status.idle":"2022-08-06T01:25:33.594436Z","shell.execute_reply.started":"2022-08-06T01:25:33.587493Z","shell.execute_reply":"2022-08-06T01:25:33.593205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Main","metadata":{}},{"cell_type":"code","source":"for video in video_list:\n    video_path = os.path.join(cfg.train_dir, video)\n    vidcap = cv2.VideoCapture(video_path)\n    tmp = perfect_submission[\n        perfect_submission[\"video_id\"] == video[:-4]\n        ].reset_index(drop=True)\n    \n    data_num = tmp.shape[0]\n    for i in tqdm(range(data_num)):\n        row = tmp.iloc[i] \n        video_id = row.video_id\n        event = row.event \n        event_attributes = row.event_attributes\n        time = row.time \n        save_frame_sec(\n        video_path, \n        time, \n        os.path.join(cfg.image_dir, video_id, event, event_attributes, f'{time}.jpg')\n        )","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:26:22.424377Z","iopub.execute_input":"2022-08-06T01:26:22.424821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}