Python ip camera stream

OpenCV and Real time streaming protocol (RTSP)

Hello friends, this tutorial is about RTSP stream basics, how to process it, and obtain frames in Python. In general, OpenCV is used with webcams connected to computers or also embedded inside them. However, for the surveillance purpose, we commonly use IP cameras that generate video streams using RTSP protocol. We can use such IP cameras in projects of video processing, like motion detection, etc. So let’s start by knowing what RTSP is.

RTSP

It is a network control protocol to deliver multimedia content across the desired networks. With TCP, it provides a reliable stream, and its structure is similar to the HTTP. It was designed mainly for entertainment and communications systems to access and control streaming media servers. The clients can issue commands to the media server like play, record, pause, etc. to enable real-time control of the media servers. RTSP allows for video-on-demand solutions.

Lets try to open some rtsp and http links:

  1. “rtsp://freja.hiof.no:1935/rtplive/definst/hessdalen03.stream”
  2. “http://wmccpinetop.axiscam.net/mjpg/video.mjpg”

Here is the code to obtain frames of video from the rtsp link.

 import cv2 # vid = cv2.VideoCapture(0) # For webcam vid = cv2.VideoCapture("http://wmccpinetop.axiscam.net/mjpg/video.mjpg") # For streaming links while True: _,frame = vid.read() print(frame) cv2.imshow('Video Live IP cam',frame) key = cv2.waitKey(1) & 0xFF if key ==ord('q'): break vid.release() cv2.destroyAllWindows() 

The general syntax of rtsp stream for IP cameras are like:

According to your settings, give the parameters in the link and use in your opencv projects.

Experiment of live webcam stream on webcam

Issue this command in Terminal:

ffmpeg -f avfoundation -pix_fmt yuyv422 -video_size 640×480 -framerate 30 -i «0:0» -ac 2 -vf format=yuyv422 -vcodec libx264 -maxrate 2000k -bufsize 2000k -acodec aac -ar 44100 -b:a 128k -f rtp_mpegts rtp://127.0.0.1:9999

In another terminal run this python code

import cv2 # vid = cv2.VideoCapture(0) # For webcam vid = cv2.VideoCapture("rtp://127.0.0.1:9999") # For streaming links while True: rdy,frame = vid.read() print(rdy) try: cv2.imshow('Video Live IP cam',frame) key = cv2.waitKey(1) & 0xFF if key ==ord('q'): break except: pass vid.release() cv2.destroyAllWindows() 

Wait for the frames to get appear (may take up 10 seconds) after some messages!

Источник

Capture RTSP Stream from IP Camera using OpenCV

Capture RTSP Stream from IP Camera using OpenCV

Most of the IP cameras supports Real Time Streaming Protocol (RTSP) to control audio and video streaming. This tutorial provides example how to capture RTSP stream from IP camera using OpenCV and Python.

Читайте также:  Php скрипты социальной сети

OpenCV provides the VideoCapture class which allows capturing video from video files, image sequences, webcams, IP cameras, etc. To capture RTSP stream from IP camera, we need to specify RTSP URL as argument. Since RTSP URL is not standardized, different IP camera manufacturers might use different RTSP URLs. Many manufacturers provide RTSP URL on their website or user manual. RTSP URL usually consists of username, password, IP address of the camera, port number (554 is the default RTSP port number), stream name.

Captured frames displayed in the window using imshow function. A window can be closed by pressing the ESC key (represented as ASCII code 27). Reolink E1 Pro camera has been used for testing.

import cv2 import os RTSP_URL = 'rtsp://user:pass@192.168.0.189:554/h264Preview_01_main' os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp' cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG) if not cap.isOpened(): print('Cannot open RTSP stream') exit(-1) while True: _, frame = cap.read() cv2.imshow('RTSP stream', frame) if cv2.waitKey(1) == 27: break cap.release() cv2.destroyAllWindows()
#include #include using namespace cv; int main() < const std::string RTSP_URL = "rtsp://user:pass@192.168.0.189:554/h264Preview_01_main"; #if WIN32 _putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp"); #else setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp", 1); #endif Mat frame; VideoCapture cap(RTSP_URL, CAP_FFMPEG); if (!cap.isOpened()) < std::cout while (true) < cap >> frame; imshow("RTSP stream", frame); if (waitKey(1) == 27) < break; >> cap.release(); destroyAllWindows(); return 0; >
package app; import org.opencv.core.*; import org.opencv.highgui.HighGui; import org.opencv.videoio.VideoCapture; import org.opencv.videoio.Videoio; public class Main < static < System.loadLibrary(Core.NATIVE_LIBRARY_NAME); >public static void main(String[] args) < String RTSP_URL = "rtsp://user:pass@192.168.0.189:554/h264Preview_01_main"; System.setProperty("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp"); Mat frame = new Mat(); VideoCapture cap = new VideoCapture(RTSP_URL, Videoio.CAP_FFMPEG); if (!cap.isOpened()) < System.out.println("Cannot open RTSP stream"); System.exit(-1); >while (true) < cap.read(frame); HighGui.imshow("RTSP stream", frame); if (HighGui.waitKey(1) == 27) < break; >> cap.release(); HighGui.destroyAllWindows(); System.exit(0); > >

Convert Image from RGB to HSV Color Space using OpenCV

Extract Individual Channels From RGB Image using OpenCV

Draw Line on Image using OpenCV

OpenCV provides functionality for drawing geometric shapes such as line, rectangle, circle, etc. The line.

The 9 Comments Found

Just in case you need to close the RTSP window by using «X» button, modify the while loop as follows:

while True: _, frame = cap.read() cv2.imshow('RTSP stream', frame) if cv2.waitKey(1) == 27: break if cv2.getWindowProperty('RTSP stream', cv2.WND_PROP_VISIBLE) < 1: break

I was used RTSP url in local it's working fine, but when I use this url in cloud I didn't get the streaming. Finally it's showing streaming is "0". Please help me out from this problem.

Hi,
If you want to remotely access IP camera by using IP address, you should forward ports in router. Check port forwarding settings in router admin panel. Which ports should be forwarded depends on IP camera. Check documentation in manufacturer website. VLC media player can be used on computer to test if IP camera is accessible remotely.

Thank you for this!
For those of you wanting to use this in WSL, I had to change the
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'
to
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;tcp'

Thanks for this! Can you explain this line:
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'

It is an optional line that instructs FFmpeg library to use UDP as the transport protocol instead of TCP when capturing video over RTSP. It's done for performance reasons.

Читайте также:  designfornet

There are no official OpenCV bindings for PHP programming language. However, there are third-party OpenCV bindings available for PHP, such as php-opencv. You need to check how it actively maintained and what features are provided.

Источник

OpenCV and IP camera streaming with Python

With todays computing power (including embedded and hobby board computers), the commoditisation of web cameras, and the maturity of computer vision software and object detection algorithms, anyone can play around computer vision for negligible cost.

In this guide I'll give you a rough start to streaming content from an IP camera to OpenCV (tested on v2.4.10) for building your own computer vision projects. Although the code in this guide is written in Python there are many other languages supported by OpenCV.

Ai-Ball IP camera

TL;DR

IP camera streaming into OpenCV

As getting vision from an IP camera into OpenCV is an unnecessarily tricky stumbling block, we’ll only concentrate on the code that streams vision from an IP camera to OpenCV which then simply displays that stream.

webcam-opencv-example.py

import numpy as np import cv2 import time import requests import threading from threading import Thread, Event, ThreadError class Cam(): def __init__(self, url): self.stream = requests.get(url, stream=True) self.thread_cancelled = False self.thread = Thread(target=self.run) print "camera initialised" def start(self): self.thread.start() print "camera stream started" def run(self): bytes='' while not self.thread_cancelled: try: bytes+=self.stream.raw.read(1024) a = bytes.find('\xff\xd8') b = bytes.find('\xff\xd9') if a!=-1 and b!=-1: jpg = bytes[a:b+2] bytes= bytes[b+2:] img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR) cv2.imshow('cam',img) if cv2.waitKey(1) ==27: exit(0) except ThreadError: self.thread_cancelled = True def is_running(self): return self.thread.isAlive() def shut_down(self): self.thread_cancelled = True #block while waiting for thread to terminate while self.thread.isAlive(): time.sleep(1) return True if __name__ == "__main__": url = 'http://192.168.2.1/?action=stream' cam = Cam(url) cam.start()

Congratulations, you’re now streaming content into OpenCV. NB: Change the url to suit your particular camera.

Please explain.

I’ve had trouble with OpenCV and mpeg streams (even though OpenCV has support for this) as was the case in this instance with an Ai-Ball Wi-Fi camera. The code here deals with the camera’s mpeg stream directly and passes each image in that stream to OpenCV for consumption. As each (jpeg) image in the stream is binary encoded and each image frame contains a start marker '\xff\xd8' and an end marker '\xff\xd9' we can easily detect those markers and segment our stream into individual images. Many other people seem to have a similar problem so there are many other explanations and examples out there.

Читайте также:  Php create directory index

Examples

Here are a couple of examples of what you might want to do using OpenCV and some very lightweight built-in object detection algorithms (nothing fancy, just some crude knock-ups I’ve made for demo purposes):

First up, with relatively little extra code, and no other equipment, we can use fiducials to track position and orientation of objects:

Feature Matching + Homography to find Objects using OpenCV and the ORB (oriented BRIEF) keypoint detector and descriptor extractor. Determines the (x,y,z) of the centre point of a marker in order to determine where it is in 3D space relative to the camera.

  • OpenCV
  • ORB (oriented BRIEF) keypoint detector and descriptor extractor (one of many OpenCV object detection algorithms)
  • Ai-Ball web camera

Below is a more complex example that utilises an SMI Red 500 eye-tracker and PyViewX. NOTE: Eye-trackers are rapidly becoming a commodity item, and at the time of writing, the Tobii EyeX developer kit was available for $99USD. I have achieved very good results with this particular eye-tracker and the development SDK (C# only at this point in time) provides gaze and fixation event streams out of the box allowing you to build working models pretty quickly.

Feature Matching + Homography + Eye Tracking and Gaze Fixation to identify objects and locate them in space.

Determines fixation start and end points, and for the duration, draws a bounding box around the fixation area of interest (AOI) on the screen. If a recognised marker is within that box (i.e. we’re looking at an object) determine the (x,y,z) of the centre point of that marker in order to determine where it is in 3D space relative to the camera. NOTE: The fixation bounding box is for demonstration purposes only. In a real deployment you would not want to display the fixation bounding box as it distracts the user, which in turn changes their gaze point.

  • OpenCV
  • ORB (oriented BRIEF) keypoint detector and descriptor extractor (one of many OpenCV object detection algorithms)
  • SMI Red 500 eye tracker
  • PyViewX (remote streaming client for SMI eye tracker)
  • Ai-Ball web camera

Opportunities

The purpose of this rough and ready example is to get you started with getting IP camera streams into OpenCV. As shown in the second example in this article, eye-tracking can be easily integrated into computer vision projects and with the present day commoditisation of eye-trackers for the consumer market (including embedded in phones), the application for products combining computer vision and eye-tracking, along with other now commonly available technology like GPS, accelerometers, IMU’s, etc. is opening up many new development opportunities in computer vision.

Источник

Оцените статью