|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "78d92095", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "</br><div align=\"center\"> <b> <font size=\"6\" color='red'>Reading and Writing Videos</font></b> </div>\n", |
| 9 | + "</br>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "id": "f1711405", |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "import cv2\n", |
| 20 | + "import matplotlib.pyplot as plt\n", |
| 21 | + "import numpy as np\n", |
| 22 | + "from IPython.display import Video" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "id": "911afb2f", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "</br><div align=\"center\"> <b> <font size=\"5\" color='red'>Reading Video From a File</font></b> </div>\n" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "id": "408c4499", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "<div align=\"center\"> <b> <font size=\"3\" color='green'>VideoCapture(path, apiPreference)</font></b> </div>\n", |
| 39 | + "<b>path:</b> video path + video name <br>\n", |
| 40 | + "<b>apiPreference:</b> will be explained later, default value is nothing<br>" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 2, |
| 46 | + "id": "a5743f18", |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "vid_capture = cv2.VideoCapture('./videos/traffic.mp4')" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "markdown", |
| 55 | + "id": "6702b4cd", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "Usefull method of VideoCapture:</br>\n", |
| 59 | + "<b>isOpened():</b> return a boolean that indicates whether or not the video stream is valid <br>\n", |
| 60 | + "<b>read():</b> read frame sequentially. This method returns a tuple, where the first element is a boolean and the next element is the actual video frame. <br>\n", |
| 61 | + "<b>release():</b>release vid_capture object</br>\n", |
| 62 | + "<b>get():</b> retrieve important metadata associated with the video stream \n", |
| 63 | + " - get(3): frame width\n", |
| 64 | + " - get(4): frame height\n", |
| 65 | + " - get(5): video frame rate (fps)\n", |
| 66 | + " - get(7): video frame count" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": 3, |
| 72 | + "id": "2f9c630c", |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "name": "stdout", |
| 77 | + "output_type": "stream", |
| 78 | + "text": [ |
| 79 | + "Frame Rate : 29 frames per second\n", |
| 80 | + "Frame count : 599.0\n" |
| 81 | + ] |
| 82 | + } |
| 83 | + ], |
| 84 | + "source": [ |
| 85 | + "if (vid_capture.isOpened() == False):\n", |
| 86 | + " print(\"Error opening the video file\")\n", |
| 87 | + "else:\n", |
| 88 | + " fps = int(vid_capture.get(5))\n", |
| 89 | + " print(\"Frame Rate : \",fps,\"frames per second\")\n", |
| 90 | + " frame_count = vid_capture.get(7)\n", |
| 91 | + " print(\"Frame count : \", frame_count)" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 4, |
| 97 | + "id": "2c4486a8", |
| 98 | + "metadata": { |
| 99 | + "scrolled": false |
| 100 | + }, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "vid_capture = cv2.VideoCapture('./videos/traffic.mp4')\n", |
| 104 | + "ind_frame = 0\n", |
| 105 | + "# ind_pic = 0\n", |
| 106 | + "# fig, axes = plt.subplots(nrows=1, ncols=6, figsize=(20,20))\n", |
| 107 | + "# ret, frame = vid_capture.read()\n", |
| 108 | + "while vid_capture.isOpened():\n", |
| 109 | + " ret, frame = vid_capture.read()\n", |
| 110 | + " if not ret:\n", |
| 111 | + " break\n", |
| 112 | + "# if ind_frame%100==0:\n", |
| 113 | + "# axes[ind_pic].set_title(str(ind_frame))\n", |
| 114 | + "# axes[ind_pic].imshow(frame)\n", |
| 115 | + "# ind_pic+=1\n", |
| 116 | + " pic_name = 'traffic_frame/traffic'+str(ind_frame).zfill(3)+'.jpg'\n", |
| 117 | + " cv2.imwrite(pic_name, frame)\n", |
| 118 | + " ind_frame+=1\n", |
| 119 | + "vid_capture.release()" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "id": "85560405", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "</br><div align=\"center\"> <b> <font size=\"5\" color='red'>Reading an Image Sequence</font></b> </div>\n" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "markdown", |
| 132 | + "id": "e87c0265", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "</br><div align=\"center\"> <b> <font size=\"3\" color='green'>VideoCapture(image_sequence)</font></b> </div>" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": 5, |
| 141 | + "id": "fe00a166", |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "vid_capture = cv2.VideoCapture('traffic_frame/traffic%03d.jpg')" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": 6, |
| 151 | + "id": "4238de3e", |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [ |
| 154 | + { |
| 155 | + "name": "stdout", |
| 156 | + "output_type": "stream", |
| 157 | + "text": [ |
| 158 | + "Frame count : 599.0\n" |
| 159 | + ] |
| 160 | + } |
| 161 | + ], |
| 162 | + "source": [ |
| 163 | + "frame_count = vid_capture.get(7)\n", |
| 164 | + "print(\"Frame count : \", frame_count)" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "markdown", |
| 169 | + "id": "472aa38c", |
| 170 | + "metadata": {}, |
| 171 | + "source": [ |
| 172 | + "</br><div align=\"center\"> <b> <font size=\"5\" color='red'>Writing videos</font></b> </div>" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "markdown", |
| 177 | + "id": "f6b1cd4b", |
| 178 | + "metadata": {}, |
| 179 | + "source": [ |
| 180 | + "To write a video use following method:\n", |
| 181 | + "</br><div align=\"center\"> <b> <font size=\"3\" color='green'>VideoWriter(filename, fourcc, fps, frameSize)</font></b> </div>\n", |
| 182 | + "<b>filename:</b> pathname for the output video file <br>\n", |
| 183 | + "<b>fourcc:</b> 4-character code of codec, used to compress the frames (fourcc) <br>\n", |
| 184 | + "<b>fps:</b> frame rate of the created video stream <br>\n", |
| 185 | + "<b>frameSize:</b> size of the video frames<br>" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "markdown", |
| 190 | + "id": "227cc546", |
| 191 | + "metadata": {}, |
| 192 | + "source": [ |
| 193 | + "<b> about fourcc</b></br>\n", |
| 194 | + "The video codec specifies how the video stream is compressed. It converts uncompressed video to a compressed format or vice versa. To create AVI or MP4 formats, use the following fourcc specifications:\n", |
| 195 | + "\n", |
| 196 | + "AVI: cv2.VideoWriter_fourcc('M','J','P','G')\n", |
| 197 | + "\n", |
| 198 | + "MP4: cv2.VideoWriter_fourcc(*'XVID')" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "code", |
| 203 | + "execution_count": 7, |
| 204 | + "id": "6540c287", |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [], |
| 207 | + "source": [ |
| 208 | + "vid_capture = cv2.VideoCapture('traffic_frame/traffic%03d.jpg')\n", |
| 209 | + "frame_size = (int(vid_capture.get(3)),int(vid_capture.get(4)))\n", |
| 210 | + "# print(frame_size)\n", |
| 211 | + "output = cv2.VideoWriter('traffic_fast.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 60, frame_size)\n", |
| 212 | + "while(vid_capture.isOpened()):\n", |
| 213 | + " ret, frame = vid_capture.read()\n", |
| 214 | + " if ret == True:\n", |
| 215 | + " output.write(frame)\n", |
| 216 | + " else:\n", |
| 217 | + " break\n", |
| 218 | + "\n", |
| 219 | + "vid_capture.release()\n", |
| 220 | + "output.release()" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": null, |
| 226 | + "id": "fda40d13", |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [], |
| 229 | + "source": [] |
| 230 | + } |
| 231 | + ], |
| 232 | + "metadata": { |
| 233 | + "kernelspec": { |
| 234 | + "display_name": "Python 3 (ipykernel)", |
| 235 | + "language": "python", |
| 236 | + "name": "python3" |
| 237 | + }, |
| 238 | + "language_info": { |
| 239 | + "codemirror_mode": { |
| 240 | + "name": "ipython", |
| 241 | + "version": 3 |
| 242 | + }, |
| 243 | + "file_extension": ".py", |
| 244 | + "mimetype": "text/x-python", |
| 245 | + "name": "python", |
| 246 | + "nbconvert_exporter": "python", |
| 247 | + "pygments_lexer": "ipython3", |
| 248 | + "version": "3.8.2" |
| 249 | + } |
| 250 | + }, |
| 251 | + "nbformat": 4, |
| 252 | + "nbformat_minor": 5 |
| 253 | +} |
0 commit comments