"This tutorial teaches the user how to input a video file, such a mp4 and convert each frame of the video into a jpeg image using python, primarily in a Jupyter notebook."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uey1neRTkRwd"
},
"source": [
"[](https://colab.research.google.com/github/pathakis/DataTools_Tutorial_Demo/blob/main/Video-Image-Data-Tutorial/Ford_Video_Analysis.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GQIJxk_kdjT1"
},
"source": [
"<b> Environment Setup (Makefile):</b>\n",
"- Use the command 'make innit' automatically set up the environment for you.\n",
"\n",
"<b> Environment Setup (Manual):</b>\n",
"- Set up new environment using pip/conda (Conda Recommended). Use command \n",
"- The example call shows the format in which this func may be used.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f0aUW4PLdobE"
},
"source": [
"This process uses 2 packages called [os](https://docs.python.org/3/library/os.html) and [cv2](https://pypi.org/project/opencv-python/). Os provides miscellaneous operating system interfaces such as opening and reading the files."
"1. To download a sample avi file that you want to work with, use the following code `urllib.request.urlretrieve('https://www.engr.colostate.edu/me/facil/dynamics/files/drop.avi', 'testing.mp4')` \n",
"\n",
"2. After the video has been downloaded `avi_frames(./testing.mp4, path_to_where_you_want_the_frames, False)` this will create a folder with frames from the video."
" video = cv2.VideoWriter('video.avi', fourcc, fps, (width, height))\n",
" num_frames = len([name for name in os.listdir(directory_path) if os.path.isfile(name)])\n",
"\n",
" for j in range(num_frames):\n",
" img = cv2.imread(str(j) + '.jpg')\n",
" video.write(img)\n",
"\n",
" cv2.destroyAllWindows()\n",
" video.release()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sIhvc4DplLkE"
},
"source": [
"DEMO FOR THE **frames_to_video** \n",
"\n",
"1. We will be working with the frames that we created using `avi_to_frames`. If you have not created those frames feel free to look at the steps above.\n",
"\n",
"2. Run the following command (**make the necessary changes in the function call**)`frames_to_video(where_the_frames_are, fps, width, height)`\n",
"\n",
"3. The video will show up in the current directory."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bnfzxPNJeZVS"
},
"source": [
"**The code below will put the image arrays into a list.** This snippet of code utilizes glob but packages like os can also be used."