ZeroMQ Notifications
ZeroMQ is asychronous messaging library used as one of the API provided by the SmartFace Platform. For more information about the ZeroMQ please visit it’s homepage.
The SmartFace Platform provides realtime information about the events happening. The event’s can be listened to on the default ZeroMQ port 2406
. There are plenty of languages supporting the ZeroMQ. Please see below few samples in the Python script language:
Example A
The script below allows you to subscribe to all ZeroMQ topics provided by the SmartFace platform on the set IP Address.
#!/bin/python3
import zmq
host = "127.0.0.1"
port = "2406"
# Creates a socket instance
context = zmq.Context()
socket = context.socket(zmq.SUB)
# Connects to a bound socket
socket.connect("tcp://{}:{}".format(host, port))
# Subscribes to all topics
socket.subscribe("")
# Receives a string format message
while True:
print(socket.recv_string())
Example B
The script below filters out the messages incoming and processes only the Match and NoMatch notifications. It shows you how you can easily grab the provided data, change it into JSON and print the data in a structured format in the console.
#!/bin/python3
import zmq
from datetime import datetime
from collections.abc import Iterable
import json
from pprint import pprint
host = "127.0.0.1"
port = "2406"
# Creates a socket instance
context = zmq.Context()
socket = context.socket(zmq.SUB)
# Connects to a bound socket
socket.connect("tcp://{}:{}".format(host, port))
# Subscribes to all topics
socket.subscribe("")
while True:
#print(socket.recv_string())
try:
json_data = json.loads(socket.recv_string())
data_StreamID = json_data['StreamId']
now = datetime.now()
data_CurrentTimeStap = now.strftime("%H:%M:%S:%f")
data_Type = json_data['Type']
if(data_Type == "Match"):
data_Id = json_data['Id']
data_FrameId = json_data['FrameId']
data_ProcessedAt = json_data['ProcessedAt']
data_CreatedAt = json_data['CreatedAt']
data_TrackletId = json_data['TrackletId']
data_FaceId = json_data['FaceId']
if(json_data['WatchlistMemberId'] is not None):
data_WatchlistMemberId = json_data['WatchlistMemberId']
if(json_data['WatchlistMemberFullName'] is not None):
data_WatchlistMemberFullName = json_data['WatchlistMemberFullName']
print(
"TIMESTAMP: " + data_CurrentTimeStap + "> " + data_StreamIDText +
"|" + "Type: " + data_Type +
"|" + "StreamID: " + data_StreamID +
"|" + "FrameId: " + data_FrameId +
"|" + "TrackletId: " + data_TrackletId +
"|" + "ProcessedAt: " + data_ProcessedAt +
"|" + "CreatedAt: " + data_CreatedAt +
"|" + "WatchlistMemberId: " + data_WatchlistMemberId +
"|" + "WatchlistMemberFullName: " + data_WatchlistMemberFullName
)
elif(data_Type == "NoMatch"):
data_FrameId = json_data['FrameId']
data_ProcessedAt = json_data['ProcessedAt']
data_CreatedAt = json_data['CreatedAt']
data_TrackletId = json_data['TrackletId']
data_FaceId = json_data['FaceId']
print(
"TIMESTAMP: " + data_CurrentTimeStap + "> " + data_StreamIDText +
"|" + "Type: " + data_Type +
"|" + "StreamID: " + data_StreamID +
"|" + "FrameId: " + data_FrameId +
"|" + "TrackletId: " + data_TrackletId +
"|" + "ProcessedAt: " + data_ProcessedAt +
"|" + "CreatedAt: " + data_CreatedAt
)
except:
next