How To Get Video Stream From An IP Camera Using Opencv In Python?


Recently, I was working on a project in which my client wanted me to incorporate live video streaming from an IP camera installed in his office. He desired the ability to remotely monitor his office's activities and enhance its security measures.

In my Python project, I successfully accessed the video stream from the IP camera using OpenCV. I integrated the live video feed from the IP camera into our application seamlessly.

In this blog post will discuuess "How to get video stream from an ip camera using opencv in python?", I will look into  the process of accessing a video stream from an IP camera using OpenCV in Python. We'll discuss and understand the underlying mechanisms involved in retrieving the stream, and explore possible solutions and best practices to successfully integrate IP camera feeds into our Python projects.

Accessing a video stream from an IP camera using OpenCV in Python, you can follow these steps :

  1. Install OpenCV: In first step , we need to ensure that OpenCV is installed in our Python environment. We can install it using pip:
  2. pip install opencv-python
  3. Import OpenCV: Next step involves, importing the OpenCV library into our Python script:
  4. import cv2
  5. Set up Video Capture: We initialize a VideoCapture object to connect to the IP camera's video stream. We pass the URL of the camera stream as an argument:
  6. cap = cv2.VideoCapture('http://92.458.22.114/video_feed')
  7. Read and Display Frames: Here we continuously read frames from the video stream using the read() method and display them using imshow():
  8. 
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        cv2.imshow('IP Camera Feed Url', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()
    cap.release()
            

Using above code we can successfully access the video stream from an IP camera using OpenCV in Python, above code enables us to integrate live video feed from the IP camera into our Python applications for various purposes such as surveillance, monitoring, or computer vision tasks.

We need to locate the exact URL for the video stream of our IP camera, which is best accomplished using a web browser. For instance, if we are using the Hikvision IP camera, the stream URL typically follows this format:

rtsp://username:[email protected]:554/Streaming/Channels/102

Open that URL in a web browser should display the live stream. It's important to ensure that only the video stream is displayed.

Before proceeding with OpenCV, it's advisable to open Video feed URL in a web browser to ensure it functions correctly.

For example, if we want to access video from a Hikvision camera, we can refer to the following Python code snippet using OpenCV:


        import numpy as np 
        import cv2

        cap = cv2.VideoCapture() 
        cap.open("rtsp://username:password@IPAddres:PORT/Streaming/Channels/2")

        while(True):
            # Capture frame-by-frame
            ret, frame = cap.read()

            # Display the resulting frame
            cv2.imshow('Output',frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        # Release the capture when everything is done
        cap.release()
        cv2.destroyAllWindows()
    

Above show you how to access a video stream from a Hikvision camera using OpenCV in Python. You just need to replace Useraname, password, IPAddress, and PORT with the your credentials and network details.