{"id":41863,"date":"2017-08-15T06:59:48","date_gmt":"2017-08-15T06:59:48","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=41863"},"modified":"2019-11-01T18:14:34","modified_gmt":"2019-11-01T18:14:34","slug":"video-streaming-with-raspberry-pi-camera","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/video-streaming-with-raspberry-pi-camera\/","title":{"rendered":"Video Streaming with Raspberry Pi Camera"},"content":{"rendered":"<p>In this post we&#8217;re going to show you how you can do video streaming with a Raspberry Pi and a Raspberry Pi Camera &#8211; how to stream live video into a web page that you can access in any device that has a browser and is connected to the same network the Pi is. This is useful to apply to a home surveillance camera, for example.<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-41881\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/featured-image-2.jpg?resize=700%2C425&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"425\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/featured-image-2.jpg?w=1183&amp;quality=100&amp;strip=all&amp;ssl=1 1183w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/featured-image-2.jpg?resize=300%2C183&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/featured-image-2.jpg?resize=768%2C467&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/featured-image-2.jpg?resize=1024%2C623&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<h2>Prerequisites:<\/h2>\n<ul>\n<li>You should already be familiar with the <a href=\"https:\/\/makeradvisor.com\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Raspberry Pi<\/a> board &#8211; <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\" target=\"_blank\" rel=\"noopener noreferrer\">read Getting Started with Raspberry Pi<\/a><\/li>\n<li>You should have the Raspbian or Raspbian Lite operating system installed in your Raspberry Pi<\/li>\n<li>You can read this post for an <a href=\"https:\/\/randomnerdtutorials.com\/guide-to-raspberry-pi-camera-v2-module\/\">introduction to the Raspberry Pi Camera V2 module<\/a><\/li>\n<\/ul>\n<h2>Enable the Rasperry Pi Camera Module<\/h2>\n<p>If you&#8217;re using the<a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-camera-v2-module\/\" target=\"_blank\" rel=\"noopener noreferrer\"> Raspberry Pi Camera Module<\/a>, you need to enable the camera software in your Raspberry Pi in order to use it. In the Desktop environment, go to the <strong>Raspberry Pi Configuration<\/strong>\u00a0window under the <strong>Preferences<\/strong> menu, open the <strong>Interfaces<\/strong> tab and enable the <strong>Camera<\/strong> as shown in figure below.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-41866\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Figure-13-3.png?resize=508%2C429&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"508\" height=\"429\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Figure-13-3.png?w=508&amp;quality=100&amp;strip=all&amp;ssl=1 508w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Figure-13-3.png?resize=300%2C253&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 508px) 100vw, 508px\" \/><\/p>\n<p>Or, in the <strong>Terminal<\/strong> window, type the following command:<\/p>\n<pre>pi@raspberry:~ $ <strong>sudo raspi-config<\/strong><\/pre>\n<p>You should see the Raspberry Pi software configuration tool. Select the <strong>Interfacing Options<\/strong>:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-41873\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Interfacing-options.png?resize=671%2C189&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"671\" height=\"189\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Interfacing-options.png?w=959&amp;quality=100&amp;strip=all&amp;ssl=1 959w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Interfacing-options.png?resize=300%2C84&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Interfacing-options.png?resize=768%2C216&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/p>\n<p>Enable the camera and reboot your Pi:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-41874\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/enable-camera-raspi-config.png?resize=772%2C217&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"772\" height=\"217\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/enable-camera-raspi-config.png?w=960&amp;quality=100&amp;strip=all&amp;ssl=1 960w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/enable-camera-raspi-config.png?resize=300%2C84&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/enable-camera-raspi-config.png?resize=768%2C216&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 772px) 100vw, 772px\" \/><\/p>\n<h2>Find the Raspberry Pi IP address<\/h2>\n<p>To access your video streaming web server, you need to know your Raspberry Pi IP address. For that, use the following command:<\/p>\n<pre>pi@raspberry:~ $ <strong>ifconfig<\/strong><\/pre>\n<p>You&#8217;ll be given a bunch of information, including your Raspberry Pi IP address. In my case, the RPi IP address is <strong>192.168.1.112<\/strong>.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-41875\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/raspberry-pi-ip-address.png?resize=627%2C447&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"627\" height=\"447\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/raspberry-pi-ip-address.png?w=627&amp;quality=100&amp;strip=all&amp;ssl=1 627w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/raspberry-pi-ip-address.png?resize=300%2C214&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><\/p>\n<h2>Connect the camera<\/h2>\n<p>Connecting the Raspberry Pi Camera Module is easy. With the Pi shutdown, connect the camera to the Pi CSI port as shown in the following figure. Make sure the camera is connected in the right orientation with the ribbon blue letters facing up as shown in the next figure.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-41868\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/connectcamera-1.jpg?resize=516%2C406&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"516\" height=\"406\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/connectcamera-1.jpg?w=516&amp;quality=100&amp;strip=all&amp;ssl=1 516w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/connectcamera-1.jpg?resize=300%2C236&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 516px) 100vw, 516px\" \/><\/p>\n<h2>Writing the script<\/h2>\n<p>The script for video streaming is shown below. You can find this script at the official PiCamera package <a href=\"http:\/\/picamera.readthedocs.io\/en\/latest\/recipes2.html\" target=\"_blank\" rel=\"noopener noreferrer\">documentation<\/a>.<\/p>\n<p>Create a new file called\u00a0<strong>rpi_camera_surveillance_system.py<\/strong>:<\/p>\n<pre>pi@raspberrypi:~ $ <strong>nano rpi_camera_surveillance_system.py<\/strong><\/pre>\n<p>Copy the following code to your newly created file:<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"># Web streaming example\n# Source code from the official PiCamera package\n# http:\/\/picamera.readthedocs.io\/en\/latest\/recipes2.html#web-streaming\n\nimport io\nimport picamera\nimport logging\nimport socketserver\nfrom threading import Condition\nfrom http import server\n\nPAGE=&quot;&quot;&quot;\\\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Raspberry Pi - Surveillance Camera&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;center&gt;&lt;h1&gt;Raspberry Pi - Surveillance Camera&lt;\/h1&gt;&lt;\/center&gt;\n&lt;center&gt;&lt;img src=&quot;stream.mjpg&quot; width=&quot;640&quot; height=&quot;480&quot;&gt;&lt;\/center&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n&quot;&quot;&quot;\n\nclass StreamingOutput(object):\n    def __init__(self):\n        self.frame = None\n        self.buffer = io.BytesIO()\n        self.condition = Condition()\n\n    def write(self, buf):\n        if buf.startswith(b'\\xff\\xd8'):\n            # New frame, copy the existing buffer's content and notify all\n            # clients it's available\n            self.buffer.truncate()\n            with self.condition:\n                self.frame = self.buffer.getvalue()\n                self.condition.notify_all()\n            self.buffer.seek(0)\n        return self.buffer.write(buf)\n\nclass StreamingHandler(server.BaseHTTPRequestHandler):\n    def do_GET(self):\n        if self.path == '\/':\n            self.send_response(301)\n            self.send_header('Location', '\/index.html')\n            self.end_headers()\n        elif self.path == '\/index.html':\n            content = PAGE.encode('utf-8')\n            self.send_response(200)\n            self.send_header('Content-Type', 'text\/html')\n            self.send_header('Content-Length', len(content))\n            self.end_headers()\n            self.wfile.write(content)\n        elif self.path == '\/stream.mjpg':\n            self.send_response(200)\n            self.send_header('Age', 0)\n            self.send_header('Cache-Control', 'no-cache, private')\n            self.send_header('Pragma', 'no-cache')\n            self.send_header('Content-Type', 'multipart\/x-mixed-replace; boundary=FRAME')\n            self.end_headers()\n            try:\n                while True:\n                    with output.condition:\n                        output.condition.wait()\n                        frame = output.frame\n                    self.wfile.write(b'--FRAME\\r\\n')\n                    self.send_header('Content-Type', 'image\/jpeg')\n                    self.send_header('Content-Length', len(frame))\n                    self.end_headers()\n                    self.wfile.write(frame)\n                    self.wfile.write(b'\\r\\n')\n            except Exception as e:\n                logging.warning(\n                    'Removed streaming client %s: %s',\n                    self.client_address, str(e))\n        else:\n            self.send_error(404)\n            self.end_headers()\n\nclass StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):\n    allow_reuse_address = True\n    daemon_threads = True\n\nwith picamera.PiCamera(resolution='640x480', framerate=24) as camera:\n    output = StreamingOutput()\n    #Uncomment the next line to change your Pi's Camera rotation (in degrees)\n    #camera.rotation = 90\n    camera.start_recording(output, format='mjpeg')\n    try:\n        address = ('', 8000)\n        server = StreamingServer(address, StreamingHandler)\n        server.serve_forever()\n    finally:\n        camera.stop_recording()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/rpi_camera_surveillance_system.py\" target=\"_blank\">View raw code<\/a><\/p>\n<p>To save your file press Ctrl+X, type Y and Enter.<\/p>\n<h2>Accessing the video streaming<\/h2>\n<p>After writing the scrip, you can run it using Python 3. Run the next command:<\/p>\n<pre>pi@raspberrypi:~ $ <strong>python3 rpi_camera_surveillance_system.py<\/strong><\/pre>\n<p>Once the script is running, you can access your video streaming web server at: http:\/\/<strong><span style=\"color: #ff0000;\">&lt;Your_Pi_IP_Address&gt;<\/span><\/strong>:8000. Replace with your own Raspberry Pi IP address, in my case <strong>http:\/\/192.168.1.112:8000<\/strong>.<\/p>\n<p>You can access the video streaming through any device that has a browser and is connected to the same network that your Pi.<\/p>\n<p>You can use your Pi to monitor your home as a surveillance camera:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-41878\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/rpi-streaming-web.png?resize=642%2C393&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"642\" height=\"393\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/rpi-streaming-web.png?w=642&amp;quality=100&amp;strip=all&amp;ssl=1 642w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/rpi-streaming-web.png?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/p>\n<h2>Wrapping up<\/h2>\n<p>I hope this project was useful! You could easily upgrade this home surveillance device to record video or notify you when motion is detected.<\/p>\n<p>We also have a project on how to build a <a href=\"https:\/\/randomnerdtutorials.com\/cctv-raspberry-pi-based-system-storage-motioneyeos\/\" target=\"_blank\" rel=\"noopener noreferrer\">complete CCTV system with the Raspberry Pi using MotionEyeOS<\/a>. Feel free to take a look.<\/p>\n<p>Like home automation? Learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino\u00a0with\u00a0my course:\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/build-a-home-automation-system-for-100\/\" target=\"_blank\" rel=\"noopener noreferrer\">Build a Home Automation System for $100<\/a>.<\/p>\n<p><strong>Do you have any questions?\u00a0<\/strong><strong>Leave a comment down below!<\/strong><\/p>\n<p>Thanks for reading. If you like this post probably you might like my next ones, so please support me by\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/download\">subscribing my blog<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we&#8217;re going to show you how you can do video streaming with a Raspberry Pi and a Raspberry Pi Camera &#8211; how to stream live video into &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Video Streaming with Raspberry Pi Camera\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/video-streaming-with-raspberry-pi-camera\/#more-41863\" aria-label=\"Read more about Video Streaming with Raspberry Pi Camera\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":41881,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301,269,264,190,268,192],"tags":[],"class_list":["post-41863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-raspberrypi","category-guide-project","category-project","category-raspberry-pi","category-raspberry-pi-project","category-r-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/featured-image-2.jpg?fit=1183%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/41863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=41863"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/41863\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/41881"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=41863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=41863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=41863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}