<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: SUMIT</title>
    <description>The latest articles on Forem by SUMIT (@sumitnce).</description>
    <link>https://forem.com/sumitnce</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F365145%2F87995563-e65b-418f-8849-b79526186952.png</url>
      <title>Forem: SUMIT</title>
      <link>https://forem.com/sumitnce</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sumitnce"/>
    <language>en</language>
    <item>
      <title>How to Capture Live Video Stream from an IP Camera Using Python</title>
      <dc:creator>SUMIT</dc:creator>
      <pubDate>Thu, 16 Jan 2025 10:55:21 +0000</pubDate>
      <link>https://forem.com/sumitnce/how-to-capture-live-video-stream-from-an-ip-camera-using-python-5475</link>
      <guid>https://forem.com/sumitnce/how-to-capture-live-video-stream-from-an-ip-camera-using-python-5475</guid>
      <description>&lt;p&gt;&lt;em&gt;Connect with me on &lt;a href="https://www.linkedin.com/in/sumitnce/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; to network, and visit my &lt;a href="https://github.com/sumitnce1/" rel="noopener noreferrer"&gt;Github&lt;/a&gt;  for my latest projects.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this article, we will walk through how to capture and display a live video feed from an IP camera using Python. We’ll use libraries like &lt;code&gt;requests&lt;/code&gt; for fetching images from a URL, &lt;code&gt;OpenCV&lt;/code&gt; for processing the images, and &lt;code&gt;imutils&lt;/code&gt; for resizing the images. We’ll write a Python script to continuously retrieve the video feed from a camera and display it on the screen.&lt;br&gt;
By the end of this article, you’ll understand how to capture and display video frames from a network camera in real-time, and how to adapt this setup for various IP camera streams.&lt;/p&gt;
&lt;h2&gt;
  
  
  Objective
&lt;/h2&gt;

&lt;p&gt;The goal of this tutorial is to demonstrate how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetch video frames from an &lt;code&gt;IP camera&lt;/code&gt; using &lt;code&gt;HTTP&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Process the frames using &lt;code&gt;OpenCV&lt;/code&gt; for display.&lt;/li&gt;
&lt;li&gt;Continuously retrieve and display images in real-time from the camera feed.&lt;/li&gt;
&lt;li&gt;Set up a loop to display the stream until the user decides to exit by pressing a key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The final result will be a live video stream displayed on your screen, with the ability to exit by pressing the &lt;code&gt;Esc&lt;/code&gt; key.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we dive into the code, you need to have the following libraries installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;requests:&lt;/strong&gt; To send HTTP requests and retrieve the image data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;opencv-python:&lt;/strong&gt; To process and display the images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;imutils:&lt;/strong&gt; A set of convenience functions to make &lt;code&gt;OpenCV&lt;/code&gt; easier to use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can install these libraries using the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install requests
pip3 install opencv-python
pip3 install imutils
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, you’ll need access to an IP camera or any device streaming video via an HTTP feed (for example, a webcam streaming via &lt;code&gt;MJPEG&lt;/code&gt; on port &lt;code&gt;8080&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;p&gt;Using IP Webcam App on Your Mobile Device:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Download and install the &lt;a href="https://play.google.com/store/apps/details?id=com.pas.webcam" rel="noopener noreferrer"&gt;IP Webcam application&lt;/a&gt; on your mobile phone.&lt;/strong&gt; This app turns your phone into an IP camera.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ensure that both your PC and phone are connected to the same network.
**3. **Launch the IP Webcam application&lt;/strong&gt; on your phone. Tap on the “Start Server” option. This will start the camera stream on your phone.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;URL will appear&lt;/strong&gt; on your phone screen. It will look something like &lt;code&gt;http://192.168.0.101:8080/video&lt;/code&gt;. You’ll use this URL to fetch the video stream in your Python script.&lt;/li&gt;
&lt;li&gt;On your PC, &lt;strong&gt;open the URL in your web browser&lt;/strong&gt; to verify that the camera feed is working. Under the &lt;code&gt;Video Renderer&lt;/code&gt; section, select &lt;code&gt;Javascript&lt;/code&gt; to render the stream in the browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cz3o1566jrazg2ry959.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cz3o1566jrazg2ry959.png" alt="https://192.168.0.101:8080 Browser on Video Renderer" width="720" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have set up the camera, let’s dive into the Python code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Explanation
&lt;/h2&gt;

&lt;p&gt;Here’s the complete Python script that retrieves and displays the video stream from an IP camera:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Import essential libraries 
import requests 
import cv2 
import numpy as np 
import imutils 

# Replace the below URL with your own. Make sure to add "/shot.jpg" at last. 
url = "http://&amp;lt;your-phone-ip&amp;gt;:8080/shot.jpg"
url = "http://192.168.0.101:8080/shot.jpg"

# While loop to continuously fetch data from the URL
while True:
    # Request the image from the camera URL
    img_resp = requests.get(url) 

    # Convert the content of the response to a NumPy array
    img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8) 

    # Decode the image from the NumPy array into an image format that OpenCV can display
    img = cv2.imdecode(img_arr, -1)

    # Resize the image for better display (optional)
    img = imutils.resize(img, width=1000, height=1800)

    # Display the image in a window named "Android_cam"
    cv2.imshow("Android_cam", img) 

    # Wait for a key press to exit the loop (Esc key code is 27)
    if cv2.waitKey(1) == 27: 
        break

# Close all OpenCV windows after the loop ends
cv2.destroyAllWindows()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step-by-Step Explanation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Importing Libraries:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;requests: For making HTTP requests to fetch the image data from the camera.&lt;/li&gt;
&lt;li&gt;cv2: OpenCV, used for image processing and displaying the frames.&lt;/li&gt;
&lt;li&gt;numpy: For handling arrays, which is required for converting the image data into a format that OpenCV can use.&lt;/li&gt;
&lt;li&gt;imutils: A library to simplify common OpenCV tasks, like resizing images.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Setting the Camera URL:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The URL of the camera stream is defined. In this example, the URL &lt;code&gt;http://&amp;lt;your-phone-ip&amp;gt;:8080/shot.jpg&lt;/code&gt; represents the endpoint for the camera feed. Replace this with your phone’s actual IP address and the correct endpoint for the video feed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Fetching the Image:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inside a &lt;code&gt;while True&lt;/code&gt; loop, we continuously request the image from the specified URL using &lt;code&gt;requests.get(url)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The content of the response &lt;code&gt;(img_resp.content)&lt;/code&gt; is converted into a &lt;code&gt;NumPy&lt;/code&gt; array using &lt;code&gt;np.array()&lt;/code&gt;. This allows us to pass the raw image bytes into &lt;code&gt;OpenCV&lt;/code&gt; for decoding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Decoding and Resizing:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cv2.imdecode()&lt;/code&gt; is used to decode the image into a format that OpenCV can process (essentially converting the raw byte array into an image object).&lt;/li&gt;
&lt;li&gt;The image is resized using &lt;code&gt;imutils.resize()&lt;/code&gt;, which ensures the image fits nicely into the window. This is particularly helpful for large images that may otherwise not fit on the screen.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Displaying the Image:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We use &lt;code&gt;cv2.imshow()&lt;/code&gt; to display the image in a window called "&lt;code&gt;Android_cam&lt;/code&gt;".&lt;/li&gt;
&lt;li&gt;The loop continues indefinitely, fetching and displaying frames from the camera.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. Exiting the Loop:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cv2.waitKey(1)&lt;/code&gt; function waits for a key press. If the user presses the &lt;code&gt;Esc&lt;/code&gt; key &lt;code&gt;(key code 27)&lt;/code&gt;, the loop breaks, and the video display window is closed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Cleaning Up:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, &lt;code&gt;cv2.destroyAllWindows()&lt;/code&gt; ensures that all &lt;code&gt;OpenCV&lt;/code&gt; windows are closed when the loop ends.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Run the Script
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Ensure your IP camera (or mobile device) is up and running.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update the URL in the script&lt;/strong&gt; to point to your camera’s image stream (from your IP Webcam app).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Save the script as &lt;code&gt;video.py&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Run the script using the following command:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python video.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The video stream should start displaying in a new window.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we’ve successfully implemented a Python script that fetches and displays a live video stream from an IP camera. By using libraries like &lt;code&gt;requests&lt;/code&gt;, &lt;code&gt;OpenCV&lt;/code&gt;, and &lt;code&gt;imutils&lt;/code&gt;, we were able to easily handle HTTP requests, process image data, and display it in real-time.&lt;br&gt;
This script can be adapted for various use cases, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time video surveillance systems.&lt;/li&gt;
&lt;li&gt;IP camera integration with other Python-based applications.&lt;/li&gt;
&lt;li&gt;Live video streaming analysis or processing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By modifying the script, you can integrate more advanced functionalities, such as motion detection, image processing, and saving the video stream for later use.&lt;/p&gt;

</description>
      <category>python</category>
      <category>camera</category>
      <category>opencvvideostream</category>
      <category>androiddev</category>
    </item>
  </channel>
</rss>
