Skip to main content

Viewtron Python SDK

The Viewtron Python SDK (pip install viewtron) is the recommended way to work with the Viewtron IP Camera API. It handles three things:

  1. Event server — built-in HTTP server that receives camera webhook events, handles persistent connections and keepalives
  2. Event parsing — automatically detects IPC v1.x vs NVR v2.0 format and returns typed Python objects
  3. Camera control — send commands to cameras and manage plate databases programmatically

The SDK handles all XML formatting, API version differences, HTTP connection management, and authentication automatically.

Install

pip install viewtron

Requires Python 3.8+. Dependencies: requests, xmltodict.

Source code: github.com/mikehaldas/viewtron-python-sdk | Full API reference: Python SDK Reference

Receiving Events — ViewtronServer

The ViewtronServer class is a complete HTTP server that receives webhook events from Viewtron cameras. It handles HTTP/1.1 persistent connections, keepalive messages, multi-threaded request handling, and XML response — you just write the callback.

from viewtron import ViewtronServer

def on_event(event, client_ip):
print(f"[{event.category}] from {client_ip}")

if event.category == "lpr":
print(f" Plate: {event.get_plate_number()}")
print(f" Group: {event.get_plate_group()}")

# Save images
overview = event.get_source_image_bytes() # decoded JPEG bytes
if overview:
with open("overview.jpg", "wb") as f:
f.write(overview)

server = ViewtronServer(port=5002, on_event=on_event)
server.serve_forever()

Point your camera's HTTP POST at <your-server-ip>:5002 and events start flowing. No XML parsing, no base64 decoding, no keepalive handling — the SDK does it all.

Server Options

ParameterDefaultDescription
port5002HTTP listener port
on_eventrequiredCallback for alarm events — receives (event, client_ip)
on_trajectNoneCallback for real-time tracking data — receives (traject, client_ip)

ViewtronEvent — Parse Any Event

If you're building your own HTTP server or processing saved XML, use ViewtronEvent directly:

from viewtron import ViewtronEvent

event = ViewtronEvent(xml_body)
if event is None:
return # keepalive, alarm status, or unrecognized

print(event.category) # "lpr", "intrusion", "face", "counting", "metadata"
print(event.get_plate_number()) # "ABC1234"
print(event.get_plate_group()) # "whiteList" (IPC) or NVR group name

# Images as decoded JPEG bytes — ready for saving, MQTT, notifications
overview = event.get_source_image_bytes() # full scene
plate_crop = event.get_target_image_bytes() # plate closeup

ViewtronEvent automatically detects the API version (IPC v1.x vs NVR v2.0), identifies the event type from the smartType field, and returns the correct typed object. Returns None for keepalives, alarm status messages, and unrecognized event types.

Event Classes

The SDK provides separate classes for each detection type and API version. Version detection is automatic — IPC v1.x and NVR v2.0 use different XML structures, but the classes share common methods.

IPC v1.x (Direct from Camera)

ClasssmartTypeDetectionApplication Guide
LPRVEHICELicense plate recognition with whitelist/blacklistLPR
FaceDetectionFACEFace detection with crop imageFace Detection
IntrusionDetectionPEAPerimeter intrusion (person/vehicle/motorcycle)Human Detection
IntrusionEntryAVDZone entry detectionPerimeter Security
IntrusionExitAVDZone exit detectionPerimeter Security
LoiteringDetectionLOITERINGLoitering in a zoneLoitering
IllegalParkingPVDParking violationVehicle/Parking
VideoMetadataVIDEO_METADATAContinuous object detectionReal-Time Tracking

NVR v2.0 (Forwarded via NVR)

ClasssmartTypeDetectionApplication Guide
VehicleLPRvehicleLPR with vehicle brand, color, type, modelLPR
FaceDetectionV2faceDetectionFace detection with age, sex, glasses, maskFace Detection
RegionIntrusionregionIntrusionPerimeter intrusionHuman Detection
LineCrossinglineCrossingTripwire line crossingPerimeter Security
TargetCountingByLinetargetCountingByLinePeople/vehicle counting by linePeople Counting
TargetCountingByAreatargetCountingByAreaPeople/vehicle counting by areaPeople Counting
VideoMetadataV2videoMetadataContinuous object detectionReal-Time Tracking

Common Methods

All event classes share these methods:

MethodReturnsDescription
categorystrEvent category: "lpr", "intrusion", "face", "counting", "metadata"
get_ip_cam()strCamera IP address or device name
get_alarm_type()strRaw detection type from camera (e.g., VEHICE, PEA, vehicle)
get_alarm_description()strHuman-readable description
get_time_stamp_formatted()strFormatted date/time string
source_image_exists()boolWhether a full-frame image is included
get_source_image_bytes()bytesDecoded JPEG of the full scene (ready to save or send)
target_image_exists()boolWhether a target crop image is included
get_target_image_bytes()bytesDecoded JPEG of the detected target
get_source_image()strBase64-encoded full scene (use _bytes() for most cases)
get_target_image()strBase64-encoded target crop

LPR-Specific Methods

MethodReturnsDescription
get_plate_number()strDetected plate text
get_plate_group()strPlate database group: "whiteList", "blackList", "temporaryList" (IPC), or user-defined group name (NVR). Empty string if plate is not in the database.
get_car_brand()strVehicle brand (NVR v2.0 only)
get_car_model()strVehicle model (NVR v2.0 only)
get_car_type()strVehicle type: sedan, suv, mpv, etc. (NVR v2.0 only)
get_car_color()strVehicle color (NVR v2.0 only)

Face Detection Methods (NVR v2.0)

MethodReturnsDescription
get_face_age()strAge range: young, youth, middleAged, elderly, unknown
get_face_sex()strmale, female, unknown
get_face_glasses()stryes, no, unknown
get_face_mask()stryes, no, unknown

See each application guide for complete working examples with image saving and CSV logging.

Outbound API — Control the Camera

The ViewtronCamera class sends commands to cameras using Basic HTTP authentication.

Quick Example

from viewtron import ViewtronCamera

camera = ViewtronCamera("192.168.0.20", "admin", "password")

# Device info
info = camera.get_device_info()
print(info["model"]) # "LPR-IP4"

# Manage the license plate database
camera.add_plate("ABC1234")
plates = camera.get_plates()
camera.modify_plate("ABC1234", owner="Mike H.", telephone="555-1234")
camera.delete_plate("ABC1234")

Or use as a context manager:

with ViewtronCamera("192.168.0.20", "admin", "password") as cam:
plates = cam.get_plates()
for plate in plates:
print(plate)

Camera Client Methods

MethodDescription
get_device_info()Get camera model, firmware version, device name
add_plate(plate_number, group_id="1")Add a plate to the camera database
add_plates(plate_numbers, group_id="1")Add multiple plates in one request
get_plates(max_results=50, offset=1, group_id="1")Query plates with pagination (offset is 1-based)
modify_plate(plate_number, group_id="1", owner=None, telephone=None)Update plate details
delete_plate(plate_number, group_id="1")Remove a plate from the database

See the LPR Config API reference for the underlying XML endpoints.

Projects Built with This SDK

ProjectDescription
Viewtron Home Assistant IntegrationCamera AI events as native HA sensors and images via MQTT auto-discovery
IP Camera API ServerAlarm server with CSV logging and image saving — uses ViewtronServer
ESP8266 Relay ControllerIoT relay triggered by camera detection events

Supported Cameras

The SDK works with any Viewtron AI security camera or NVR that supports HTTP POST webhooks. For license plate recognition, use the Viewtron LPR cameras. All Viewtron products are NDAA compliant.

Video Guides

Questions & Development Inquiries

Mike Haldas is available for questions, consultation, and custom software development for Viewtron API related projects. Email details about your project to mike@viewtron.com.