How to watch your multiple camera in your server

Erwin Ardias
3 min readFeb 22, 2020

--

https://www.allcoastcableguys.com.au/

How do you access multiple cameras in one service and can be displayed in a browser? okay many ways you can do to access multiple cameras in one service. this time we will use zeromq or 0mq. Alright, before entering the tutorial we must know what zeromq is.

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker. The library’s API is designed to resemble Berkeley sockets.

ZeroMQ is developed by a large community of contributors, founded by iMatix, which holds the domain name and trademarks. There are third-party bindings for many popular programming languages.

Can we receive the camera frame simultaneously? Absolutely, we can make 1 service to take multiple camera frames using python, opencv and zeromq.

what needs to be understood in using zeromq is The ZeroMQ API provides sockets (a kind of generalization over the traditional IP and Unix domain sockets), each of which can represent a many-to-many connection between endpoints. Operating with a message-wise granularity, they require that a messaging pattern be used, and are particularly optimized for that kind of pattern.

The basic ZeroMQ patterns are:

  1. Request–reply

Connects a set of clients to a set of services. This is a remote procedure call and task distribution pattern.

2. Publish–subscribe

Connects a set of publishers to a set of subscribers. This is a data distribution pattern.

3. Push–pull (pipeline)

Connects nodes in a fan-out / fan-in pattern that can have multiple steps, and loops. This is a parallel task distribution and collection pattern.

4. Exclusive pair

Connects two sockets in an exclusive pair. (This is an advanced low-level pattern for specific use cases.)

OK, this time we will use the custom zeromq library to send images. using this library is very easy we only need to import and run programs with REQ / REP Messaging Patterns.

The first we need to install imagezmq

pip install imagezmq

if you want to clone in your directory

git clone https://github.com/jeffbass/imagezmq.git

if you need all of library for this tutorial you can follow this way

ok it’s time to coding..

so after that we will make 2 files, in this tutorial I will name the file receive.py for the recipient and send.py for the sender:

dont forget to

receive.py

import cv2
import imagezmq
image_hub = imagezmq.ImageHub()
while True:
rpi_name, image = image_hub.recv_image()
cv2.imshow(rpi_name, image)
cv2.waitKey(1)
image_hub.send_reply(b’OK’)

for the sender if the camera is more than one, the sender file must follow the number of the cameras. to get the RSTP camera link, follow the steps from the previous tutorial here.

send.py

import cv2
import imagezmq
import time
sender = imagezmq.ImageSender()
image_window_name = ‘camera_1’
previous = time.time()
delta = 0
vsc = cv2.VideoCapture(“rtsp://admin:admin123456@10.**.**.15”)
vs = cv2.VideoCapture(vsc)
while True:
ret,frame = vs.read()
current = time.time()
delta += current — previous
previous = current
if delta > 3 :
grabbed, frame = vs.read()
frame = imutils.resize(frame,500)
delta = 0
text1 = sender.send_image(image_window_name, frame)

the script above has a delay every 3 seconds to send the image so that there are no frames that accumulate if there is a delay in sending the image.

this following way to run the program

for receive.py, this program must running in server

for send.py, this program must running in client. for example if you have 3 raspberry pi yous must running this program in each raspberry with different window name.

this program can be modified as needed, if you want to add algorithms such as object detection or face recognition will be better.

to add the object detection feature I will explain in the next tutorial

happy coding..

--

--

Erwin Ardias
Erwin Ardias

Written by Erwin Ardias

software engineer | data scientist

No responses yet