Python3 Zeep ONVIF Client
Introduction
ONVIF (Open Network Video Interface Forum) is a global standard for the interface of IP-based physical security products. It allows different devices from different manufacturers to communicate with each other seamlessly. In this article, we will explore how to use the zeep
library in Python to create an ONVIF client.
Prerequisites
To follow along with the examples in this article, you will need the following:
- Python 3 installed on your machine.
- The
zeep
library installed. You can install it using pip:pip install zeep
. - An ONVIF-compatible IP camera or device that you can connect to.
Understanding the ONVIF Protocol
Before we dive into the code, let's take a moment to understand the ONVIF protocol and how it works.
ONVIF uses the Simple Object Access Protocol (SOAP) to communicate with devices. It defines a set of web service interfaces that allow clients to perform various operations such as retrieving device information, controlling cameras, and managing recordings.
To interact with an ONVIF device, we need to create a SOAP client that can send requests and receive responses. The zeep
library provides a convenient way to generate a Python client based on an ONVIF WSDL (Web Services Description Language) file.
Setting up the ONVIF Client
To create an ONVIF client using the zeep
library, we need to follow these steps:
- Import the necessary modules.
from zeep import Client
from zeep.wsse.username import UsernameToken
- Create a client object by providing the URL of the ONVIF WSDL file.
wsdl_url = '
client = Client(wsdl=wsdl_url)
- Set up authentication by adding a username token to the client.
username = 'admin'
password = 'password'
client.wsse = UsernameToken(username, password)
Interacting with the ONVIF Device
Once we have set up the ONVIF client, we can start interacting with the device. Here are some common operations we can perform:
Getting Device Information
To retrieve basic information about the device, we can use the GetDeviceInformation
method. This method returns details such as the manufacturer, model, and serial number of the device.
device_info = client.service.GetDeviceInformation()
print(device_info)
Controlling the PTZ (Pan-Tilt-Zoom) Functionality
If the device supports PTZ functionality, we can control the camera's movements using the ContinuousMove
method.
# Specify the PTZ configuration token
ptz_token = 'PTZ-1'
# Set the velocity of the pan, tilt, and zoom movements
velocity = {
'PanTilt': {
'x': 1.0,
'y': 0.5
},
'Zoom': {
'x': 0.0
}
}
# Start continuous movement
client.service.ContinuousMove(ptz_token, velocity)
Capturing a Snapshot
We can capture a snapshot from the camera using the GetSnapshotUri
method. This method returns the URL of the snapshot image.
snapshot_uri = client.service.GetSnapshotUri()
print(snapshot_uri)
Managing Recordings
To manage recordings on the device, we can use the CreateRecording
and DeleteRecording
methods.
# Create a new recording with the specified recording configuration token
recording_token = 'REC-1'
client.service.CreateRecording(recording_token)
# Delete the recording with the specified recording token
client.service.DeleteRecording(recording_token)
Putting it All Together
Let's put everything we've learned so far into a complete example that retrieves device information and captures a snapshot from an ONVIF device.
from zeep import Client
from zeep.wsse.username import UsernameToken
# Set up the ONVIF client
wsdl_url = '
client = Client(wsdl=wsdl_url)
username = 'admin'
password = 'password'
client.wsse = UsernameToken(username, password)
# Get device information
device_info = client.service.GetDeviceInformation()
print(device_info)
# Capture a snapshot
snapshot_uri = client.service.GetSnapshotUri()
print(snapshot_uri)
Conclusion
In this article, we explored how to create an ONVIF client using the zeep
library in Python. We learned how to set up the client, authenticate with the device, and perform various operations such as retrieving device information, controlling PTZ functionality, capturing snapshots, and managing recordings. By understanding the ONVIF protocol and using the zeep
library, we can easily interact with ONVIF-compatible devices and build powerful applications for physical security.