Created
August 9, 2016 13:16
-
-
Save phsamuel/796479655f2becaa848c0898e9a97bea to your computer and use it in GitHub Desktop.
I/O examples with OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Capture from web cam and write to file" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import cv2\n", | |
"import platform\n", | |
"\n", | |
"cap = cv2.VideoCapture(0)\n", | |
"\n", | |
"# work for linux and mac\n", | |
"# Define the codec and create VideoWriter object\n", | |
"fourcc = cv2.VideoWriter_fourcc(*'XVID')\n", | |
"if platform.system()=='Darwin':\n", | |
" out = cv2.VideoWriter('output.avi',-1, 20.0, (1280,720)) # for mac to select codec\n", | |
"else:\n", | |
" out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) # for linux\n", | |
"\n", | |
"\n", | |
"while(cap.isOpened()):\n", | |
" ret, frame = cap.read()\n", | |
" if ret==True:\n", | |
"# frame = cv2.flip(frame,0)\n", | |
"\n", | |
" # write the flipped frame\n", | |
" out.write(frame)\n", | |
"\n", | |
" cv2.imshow('frame',frame)\n", | |
" if cv2.waitKey(1) & 0xFF == ord('q'):\n", | |
" break\n", | |
" else:\n", | |
" break\n", | |
"\n", | |
"# Release everything if job is finished\n", | |
"cap.release()\n", | |
"out.release()\n", | |
"cv2.destroyAllWindows()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## capture from web cam and display in full screen" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"import platform\n", | |
"\n", | |
"cap=cv2.VideoCapture(0)\n", | |
"\n", | |
"while (True):\n", | |
" \n", | |
" ret,frame=cap.read()\n", | |
" \n", | |
" if platform.system()!=\"Darwin\":\n", | |
" cv2.namedWindow(\"camera\",cv2.WND_PROP_FULLSCREEN)\n", | |
" cv2.setWindowProperty(\"camera\",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)\n", | |
" cv2.imshow('camera',frame)\n", | |
"# out.write(frame)\n", | |
" \n", | |
" if cv2.waitKey(1) &0xFF == ord('q'):\n", | |
" break\n", | |
"\n", | |
" \n", | |
"cap.release()\n", | |
"# out.release()\n", | |
"cv2.destroyAllWindows()\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# capture from ip cam and display full screen" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"import platform\n", | |
"\n", | |
"# cap=cv2.VideoCapture(\"atrium.avi\") # capture from file\n", | |
"cap=cv2.VideoCapture(\"http://192.168.0.88/mjpg/video.mjpg\")\n", | |
"\n", | |
"# fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')\n", | |
"# fourcc = cv2.VideoWriter_fourcc(*'XVID')\n", | |
"\n", | |
"# out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))\n", | |
"# out = cv2.VideoWriter('output.avi',-1, 20.0, (640,480))\n", | |
"\n", | |
"while (True):\n", | |
" \n", | |
" ret,frame=cap.read()\n", | |
"\n", | |
" if platform.system()!=\"Darwin\":\n", | |
" cv2.namedWindow(\"camera\",cv2.WND_PROP_FULLSCREEN)\n", | |
" cv2.setWindowProperty(\"camera\",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)\n", | |
" cv2.imshow('camera',frame)\n", | |
"# out.write(frame)\n", | |
" \n", | |
" if cv2.waitKey(1) &0xFF == ord('q'):\n", | |
" break\n", | |
"\n", | |
" \n", | |
"cap.release()\n", | |
"# out.release()\n", | |
"cv2.destroyAllWindows()\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(720, 1280, 3)" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"frame.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment