How to send your streaming ip camera to your server around the world
computer vision, kafka, opencv
Imagine your camera is able to send what he sees to your server easily in realtime. well, Of course this will make it easier for us to see the events recorded in your CCTV even though your CCTV damaged or been stolen. of course this trick only for IP cameras that don’t need an NVR or DVR to store your CCTV videos. cctv ip camera that went around nowadays is equipped with microSD memory as a video storage. however if someone who has bad intentions then damages or takes your microSD camera, you will lose valuable evidence. Unfortunately, not everyone thinks of storing video backups recorded in the cloud. therefore a little trick from me that might be able to developed using python and opencv.
In this tutorial, we will send image data from an IP camera using Kafka Message Brokers, which is a distributed platform for data streaming.
Basically, Apache Kafka is a publish / subscribe messaging system, where there is one or more systems that generate data for a particular topic in real time in Apache Kafka (referred to as Producers). Then, the topic can be read by one or more systems that need data from the topic in real time (referred to as Consumers)
let’s start..
- Install libraries
Before we get started make sure we are familiar with python and opencv. how to install opencv is quite easy by typing in the terminal.
pip3 install opencv-python
After that we can install Apache Kafka, for those who have never installed before can follow the way as in the following link.
After finishing installing kafka and zookeeper of course we can install the kafka library in python by typing on the terminal.
pip install kafka-python
2. How to set up camera and get the rtsp link
How to set up camera and get the rtsp link? we use onvif. ONVIF is an open industry forum that provides and promotes standardized interfaces
for effective interoperability of IP-based physical security products.
The first we open the app and will shown like this
On the left side we will see name of the ip camera poduct. click video stream and you will see the camera streaming.
On the bottom side we will see the rtsp url of the ip camera. the rtsp url will used to stream ip camera for realtime.
The Real Time Streaming Protocol (RTSP) is a network control protocol designed for use in entertainment and communications systems to control streaming media servers. The protocol is used for establishing and controlling media sessions between end points. Clients of media servers issue VHS-style commands, such as play, record and pause, to facilitate real-time control of the media streaming from the server to a client (Video On Demand) or from a client to the server (Voice Recording).
To check whether rtsp is functioning properly you can use vlc player in the following way.
Insert rtsp url from onvif application to network protocol in VLC player, you will see the stream ip camera frol VLC like this.
Kafka
To use kafka it is necessary to run first by entering the kafka_2.11–1.1.0 folder from the terminal
you can follow this way
~$ cd Documents/kafka_2.11–1.1.0/~/Documents/kafka_2.11–1.1.0$ bin/kafka-server-start.sh config/server.properties
If the display on the terminal looks like this congratulation you can use your Kafka
If all is functioning properly. Ok its time to coding..
3. Coding
When using kafka it is necessary to remember that kafka is a message broker in which there are senders and recipients so there will be two files, which is producer as the sender of the message and the consumer as the recipient of the message.
- producer.py
First we need to import the kafka producer and opencv library in the following way :
from kafka import KafkaProducerimport cv2
Then we need to determine the destination ip in the following way
producer = KafkaProducer(bootstrap_servers=[‘localhost:9092’],api_version=(0,2,0))
The ip used in this tutorial is localhost because we will create a system that runs locally so we run producers and consumers on the same pc, but can be developed after each need.
After that we need to stream the cctv using the opencv library which is entered into a video variable.
video = cv2.VideoCapture(“rtsp://admin:Admin1234@10.48.102.106”)while(video.isOpened()):success, frame = video.read()if not success:print(“bad read!”)break
To run streaming using opencv the same as usual using while. but there is a difference when we send an image from producer to consumer, by changing the image into an array. we first encode the image in base64 format and then send it to the consumer.
ret, buffer = cv2.imencode(‘.jpg’, frame)producer.send(‘frame’, buffer.tobytes())
Remember when sending messages need to be included topics so consumers can receive messages with the same topic. the topic that I use in this tutorial is ‘frame’.
- consumer.py
In the consumer.py python script we must first import the kafka producer and opencv libraries in the following way:
from kafka import KafkaConsumerimport cv2
Then add ip destination
consumer = KafkaConsumer(‘frame’,bootstrap_servers=’localhost:9092', api_version=(0,2,0))
Then to read the message from the producer
like this following way:
for msg in consumer:nparr = np.fromstring(msg.value, np.uint8)img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
To bring back the image form from base 64 it needs to be decoded again so that the image can be displayed using cv2.imshow ()
cv2.imshow(“tes”,img_np)cv2.waitKey(1)
Full coding is shown below
https://github.com/iwinardhyas/streaming_ip_camera
The message sent is unlimited and can be anything. kafka can be used more broadly depending on creativity, needs and usability.
happy coding