{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Convert video to gray scale using OpenCV"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"from IPython.display import HTML\n",
"from scripts.jupyter.helpers import play_video"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-12-31 12:23:42 URL:http://techslides.com/demos/sample-videos/small.mp4 [383631/383631] -> \"../downloads/small.mp4\" [1]\r\n"
]
}
],
"source": [
"# Download some sample movie\n",
"!wget -nc -nv http://techslides.com/demos/sample-videos/small.mp4 -P ../downloads"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's play it\n",
"play_video('downloads/small.mp4')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Width=560 Height=320 FPS=30\n"
]
}
],
"source": [
"# Load the video with OpenCV and check it's properties\n",
"cap = cv2.VideoCapture('downloads/small.mp4')\n",
"width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
"height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
"fps = int(cap.get(cv2.CAP_PROP_FPS))\n",
"print(\"Width={} Height={} FPS={}\".format(width, height, fps))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Processed 166 frames\n"
]
}
],
"source": [
"# Convert video to grayscale and save it to file\n",
"\n",
"# Seek to first frame in input video\n",
"cap.set(cv2.CAP_PROP_POS_FRAMES, 0)\n",
"\n",
"# Open video writer\n",
"vid = cv2.VideoWriter('../output/small_bw.mp4',\n",
" cv2.VideoWriter_fourcc(*'H264'),\n",
" fps,\n",
" (width, height),\n",
" isColor=False)\n",
"\n",
"# Initialize number of frames counter\n",
"frames_count = 0\n",
"\n",
"while(True):\n",
" ret, frame = cap.read()\n",
" frames_count += 1 if ret else 0\n",
" if ret:\n",
" # Convert frame from color to grayscale and write it\n",
" frame_bw = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" vid.write(frame_bw)\n",
" else:\n",
" vid.release()\n",
" break\n",
"\n",
"print(\"Processed {} frames\".format(frames_count))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check the result\n",
"play_video('output/small_bw.mp4')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}