{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Displaying Video in IPython Notebook"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"# Import the required modules\n",
"%pylab inline \n",
"import cv2\n",
"from IPython.display import clear_output"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Released Video Resource\n"
]
}
],
"source": [
"# Grab the input device, in this case the webcam\n",
"# You can also give path to the video file\n",
"vid = cv2.VideoCapture(\"../data/deepdream/deepdream.mp4\")\n",
"\n",
"# Put the code in try-except statements\n",
"# Catch the keyboard exception and \n",
"# release the camera device and \n",
"# continue with the rest of code.\n",
"try:\n",
" while(True):\n",
" # Capture frame-by-frame\n",
" ret, frame = vid.read()\n",
" if not ret:\n",
" # Release the Video Device if ret is false\n",
" vid.release()\n",
" # Message to be displayed after releasing the device\n",
" print \"Released Video Resource\"\n",
" break\n",
" # Convert the image from OpenCV BGR format to matplotlib RGB format\n",
" # to display the image\n",
" frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
" # Turn off the axis\n",
" axis('off')\n",
" # Title of the window\n",
" title(\"Input Stream\")\n",
" # Display the frame\n",
" imshow(frame)\n",
" show()\n",
" # Display the frame until new frame is available\n",
" clear_output(wait=True)\n",
"except KeyboardInterrupt:\n",
" # Release the Video Device\n",
" vid.release()\n",
" # Message to be displayed after releasing the device\n",
" print \"Released Video Resource\"\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Below is a video of the above code in action. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%html\n",
"\n",
""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}