|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<a href=\"https://www.pieriandata.com\"><img src=\"../DATA/Logo.jpg\"></a>\n", |
| 8 | + "\n", |
| 9 | + "<em text-align:center>Copyright Pierian Data Inc.</em>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# OpenCV Connecting to a USB Camera or a Laptop Camera\n", |
| 17 | + "\n", |
| 18 | + "OpenCV can automatically connect to your laptop's built in camera or your USB camera if you've installed that specific USB camera drivers. Please keep in mind, its almost impossible for us to help troubleshoot this sort of physical connection on our end, so if you are not able to connect, please check out the troubleshooting tips here:\n", |
| 19 | + "\n", |
| 20 | + "* https://github.com/opencv/opencv/issues/8471\n", |
| 21 | + "\n", |
| 22 | + "### Notebook Users may need to restart the kernel after closing the camera window!" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 5, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "## PUT THIS ALL IN ONE CELL!\n", |
| 32 | + "\n", |
| 33 | + "import cv2\n", |
| 34 | + "\n", |
| 35 | + "\n", |
| 36 | + "# Connects to your computer's default camera\n", |
| 37 | + "cap = cv2.VideoCapture(0)\n", |
| 38 | + "\n", |
| 39 | + "\n", |
| 40 | + "# Automatically grab width and height from video feed\n", |
| 41 | + "# (returns float which we need to convert to integer for later on!)\n", |
| 42 | + "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n", |
| 43 | + "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", |
| 44 | + "\n", |
| 45 | + "while True:\n", |
| 46 | + " \n", |
| 47 | + " # Capture frame-by-frame\n", |
| 48 | + " ret, frame = cap.read()\n", |
| 49 | + "\n", |
| 50 | + " # Our operations on the frame come here\n", |
| 51 | + " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", |
| 52 | + "\n", |
| 53 | + " # Display the resulting frame\n", |
| 54 | + " cv2.imshow('frame',gray)\n", |
| 55 | + " \n", |
| 56 | + " # This command let's us quit with the \"q\" button on a keyboard.\n", |
| 57 | + " # Simply pressing X on the window won't work!\n", |
| 58 | + " if cv2.waitKey(1) & 0xFF == ord('q'):\n", |
| 59 | + " break\n", |
| 60 | + "\n", |
| 61 | + "# When everything done, release the capture and destroy the windows\n", |
| 62 | + "cap.release()\n", |
| 63 | + "cv2.destroyAllWindows()" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "markdown", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "## Writing a Video Stream to File" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "metadata": {}, |
| 76 | + "source": [ |
| 77 | + "### Notebook Users: Make sure its all in the same cell!" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found in fourcc.org. It is platform dependent. \n", |
| 85 | + "\n", |
| 86 | + "MORE INFO ON CODECS: https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#saving-a-video" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 3, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "import cv2\n", |
| 96 | + "\n", |
| 97 | + "cap = cv2.VideoCapture(0)\n", |
| 98 | + "\n", |
| 99 | + "# Automatically grab width and height from video feed\n", |
| 100 | + "# (returns float which we need to convert to integer for later on!)\n", |
| 101 | + "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n", |
| 102 | + "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", |
| 103 | + "\n", |
| 104 | + "\n", |
| 105 | + "# MACOS AND LINUX: *'XVID' (MacOS users may want to try VIDX as well just in case)\n", |
| 106 | + "# WINDOWS *'VIDX'\n", |
| 107 | + "writer = cv2.VideoWriter('../DATA/student_capture.mp4', cv2.VideoWriter_fourcc(*'XVID'),25, (width, height))\n", |
| 108 | + "\n", |
| 109 | + "\n", |
| 110 | + "## This loop keeps recording until you hit Q or escape the window\n", |
| 111 | + "## You may want to instead use some sort of timer, like from time import sleep and then just record for 5 seconds.\n", |
| 112 | + "\n", |
| 113 | + "while True:\n", |
| 114 | + " # Capture frame-by-frame\n", |
| 115 | + " ret, frame = cap.read()\n", |
| 116 | + "\n", |
| 117 | + " \n", |
| 118 | + " # Write the video\n", |
| 119 | + " writer.write(frame)\n", |
| 120 | + "\n", |
| 121 | + " # Display the resulting frame\n", |
| 122 | + " cv2.imshow('frame',frame)\n", |
| 123 | + " \n", |
| 124 | + " # This command let's us quit with the \"q\" button on a keyboard.\n", |
| 125 | + " # Simply pressing X on the window won't work!\n", |
| 126 | + " if cv2.waitKey(1) & 0xFF == ord('q'):\n", |
| 127 | + " break\n", |
| 128 | + "\n", |
| 129 | + " \n", |
| 130 | + "cap.release()\n", |
| 131 | + "writer.release()\n", |
| 132 | + "cv2.destroyAllWindows()" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "Make sure to check StackOverflow if you are encountering issues, as this is particularly tricky to help debug through online QA support." |
| 140 | + ] |
| 141 | + } |
| 142 | + ], |
| 143 | + "metadata": { |
| 144 | + "kernelspec": { |
| 145 | + "display_name": "Python 3", |
| 146 | + "language": "python", |
| 147 | + "name": "python3" |
| 148 | + }, |
| 149 | + "language_info": { |
| 150 | + "codemirror_mode": { |
| 151 | + "name": "ipython", |
| 152 | + "version": 3 |
| 153 | + }, |
| 154 | + "file_extension": ".py", |
| 155 | + "mimetype": "text/x-python", |
| 156 | + "name": "python", |
| 157 | + "nbconvert_exporter": "python", |
| 158 | + "pygments_lexer": "ipython3", |
| 159 | + "version": "3.6.6" |
| 160 | + } |
| 161 | + }, |
| 162 | + "nbformat": 4, |
| 163 | + "nbformat_minor": 2 |
| 164 | +} |
0 commit comments