{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":92399,"databundleVersionId":11038207,"sourceType":"competition"}],"dockerImageVersionId":30886,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The provided videos are too large to fit into memory for training. \n\nThis notebook provides the code and dataset to resize the videos. \n\nThe default setting is to resize into 224x224, but it can be conveniently changed to any shape or size.\n\n*Upvote if you like this work :) Thanks!*","metadata":{}},{"cell_type":"code","source":"%%writefile resize_videos.sh\n#!/bin/bash\n\n# arguments check\nif [ \"$#\" -ne 3 ]; then\n    echo \"Usage: $0 <input_folder> <output_folder> <resolution>\"\n    echo \"Example: $0 ./input ./output 1920x1080\"\n    exit 1\nfi\n\n# inputs \nINPUT_FOLDER=$1\nOUTPUT_FOLDER=$2\nRESOLUTION=$3\n\n# file check\nif [ ! -d \"$INPUT_FOLDER\" ]; then\n    echo \"Error: Input folder '$INPUT_FOLDER' does not exist.\"\n    exit 1\nfi\n\n# create folder\nmkdir -p \"$OUTPUT_FOLDER\"\n\n# get width and height (must be even numbers)\nWIDTH=$(echo \"$RESOLUTION\" | cut -d'x' -f1)\nHEIGHT=$(echo \"$RESOLUTION\" | cut -d'x' -f2)\n\n# loop all .mp4 files\nfor input_file in \"$INPUT_FOLDER\"/*.mp4; do\n    \n    if [ ! -e \"$input_file\" ]; then\n        echo \"No .mp4 files found in '$INPUT_FOLDER'.\"\n        exit 1\n    fi\n\n    filename=$(basename -- \"$input_file\")\n    filename_no_ext=\"${filename%.*}\"\n\n    output_file=\"$OUTPUT_FOLDER/${filename_no_ext}_${RESOLUTION}.mp4\"\n\n    # 调用 ffmpeg 进行视频大小调整\n    echo \"Resizing $input_file to $RESOLUTION...\"\n    ffmpeg -i \"$input_file\" \\\n           -vf \"scale=$WIDTH:$HEIGHT\" \\\n           -loglevel error \\\n           -c:a copy \\\n           \"$output_file\"\n\n    if [ $? -eq 0 ]; then\n        echo \"Successfully resized $input_file to $output_file\"\n    else\n        echo \"Failed to resize $input_file\"\n    fi\ndone\n\necho \"All videos processed.\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!chmod +x resize_videos.sh","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!./resize_videos.sh /kaggle/input/nexar-collision-prediction/train train_resized 224x224","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!./resize_videos.sh /kaggle/input/nexar-collision-prediction/test test_resized 224x224","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T19:46:22.422705Z","iopub.execute_input":"2025-02-20T19:46:22.423070Z","iopub.status.idle":"2025-02-20T19:46:27.901720Z","shell.execute_reply.started":"2025-02-20T19:46:22.423041Z","shell.execute_reply":"2025-02-20T19:46:27.900421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import Video\nimport glob\nimport random\n\nvideos_resized = glob.glob(\"train_resized/*.mp4\")\nprint(len(videos_resized))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T19:45:31.215629Z","iopub.execute_input":"2025-02-20T19:45:31.216001Z","iopub.status.idle":"2025-02-20T19:45:31.223976Z","shell.execute_reply.started":"2025-02-20T19:45:31.215971Z","shell.execute_reply":"2025-02-20T19:45:31.222983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Video(videos_resized[random.randint(0, len(videos_resized))], embed=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T19:45:32.303359Z","iopub.execute_input":"2025-02-20T19:45:32.303755Z","iopub.status.idle":"2025-02-20T19:45:32.357331Z","shell.execute_reply.started":"2025-02-20T19:45:32.303724Z","shell.execute_reply":"2025-02-20T19:45:32.355775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"videos_resized = glob.glob(\"test_resized/*.mp4\")\nprint(len(videos_resized))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T19:46:31.333660Z","iopub.execute_input":"2025-02-20T19:46:31.334049Z","iopub.status.idle":"2025-02-20T19:46:31.341074Z","shell.execute_reply.started":"2025-02-20T19:46:31.334021Z","shell.execute_reply":"2025-02-20T19:46:31.339818Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Video(videos_resized[random.randint(0, len(videos_resized))], embed=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-20T19:46:32.492229Z","iopub.execute_input":"2025-02-20T19:46:32.492609Z","iopub.status.idle":"2025-02-20T19:46:32.508257Z","shell.execute_reply.started":"2025-02-20T19:46:32.492577Z","shell.execute_reply":"2025-02-20T19:46:32.507198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}