{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# Performance comparison of reading a single image frame from a video using OpenCV (Python)"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"markdown","source":"### Import packages"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport time","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Reading the last frame of a sample video"},{"metadata":{},"cell_type":"markdown","source":"### Path to the sample video"},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\" Path to the sample video \"\"\"\nfilename = \"/kaggle/input/samplevideo/SampleVideo_1280x720_5mb.mp4\"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Let's look at the properties of the video"},{"metadata":{"trusted":true},"cell_type":"code","source":"# capture the video\ncap = cv2.VideoCapture(filename)\n\n# check if capture was successful\nif not cap.isOpened(): \n    print(\"Could not open!\")\nelse:\n    print(\"Video read successful!\")\n    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n    width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n    fps    = cap.get(cv2.CAP_PROP_FPS)\n    print('Total frames: ' + str(total_frames))\n    print('width: ' + str(width))\n    print('height: ' + str(height))\n    print('fps: ' + str(fps))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Approach 1: Loop to the last frame"},{"metadata":{"trusted":true},"cell_type":"code","source":"start = time.time()\ncap = cv2.VideoCapture(filename)\nfor i in range(total_frames):\n    success = cap.grab()\n    if (i == (total_frames-1)):\n        ret, image = cap.retrieve()\n        end = time.time()\n        plt.figure(1)\n        plt.imshow(image)\nprint(\"Total time taken: \" + str(end-start) + \" seconds\") ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Approach 2: Grab exact frame using OpenCV properties"},{"metadata":{"trusted":true},"cell_type":"code","source":"start = time.time()\ncap = cv2.VideoCapture(filename)\ncap.set(1,total_frames-1);\nsuccess = cap.grab()\nret, image = cap.retrieve()\nend = time.time()\nplt.figure(2)\nplt.imshow(image)\nprint(\"Total time taken: \" + str(end-start) + \" seconds\")  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Let's compare Approach 1 and Approach 2 by running each 20 times"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Approach 1\ntimer_approach1 = []\nfor loop in range(20):\n    start = time.time()\n    cap = cv2.VideoCapture(filename)\n    for i in range(total_frames):\n        success = cap.grab()\n        if (i == (total_frames-1)):\n            ret, image = cap.retrieve()\n            end = time.time()\n            timer_approach1.append(end - start)\n            \n# Approach 2\ntimer_approach2 = []\nfor loop in range(20):\n    start = time.time()\n    cap = cv2.VideoCapture(filename)\n    cap.set(1,total_frames-1);\n    success = cap.grab()\n    ret, image = cap.retrieve()\n    end = time.time()\n    timer_approach2.append(end - start)\n    \n# Plot the results\nplt.figure(3)\nplt.plot(timer_approach1)\nplt.title('Approach 1 in seconds')\n\nplt.figure(4)\nplt.plot(timer_approach2)\nplt.title('Approach 2 in seconds')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}