Images
Overview
Most Viewtron camera events include snapshot images — a source image (full camera view at the time of detection) and optionally a target image (cropped area of the detected object). Images are JPEG format, embedded as base64 in the XML payload.
Image Methods
Available on all event objects (LPR, Face, Intrusion, Counting):
| Method | Returns | Description |
|---|---|---|
source_image_exists() | bool | Whether a source (overview) image is attached |
target_image_exists() | bool | Whether a target (cropped) image is attached |
images_exist() | bool | Whether any images are attached |
get_source_image() | str or None | Base64-encoded source image |
get_target_image() | str or None | Base64-encoded target image |
get_source_image_bytes() | bytes or None | Decoded JPEG bytes for source image |
get_target_image_bytes() | bytes or None | Decoded JPEG bytes for target image |
Source vs Target Images
| Image Type | What It Contains | Typical Use |
|---|---|---|
| Source image | Full camera view at the moment of detection | Scene context, recording, human review |
| Target image | Cropped region of the detected object | Face crops, plate crops, object identification |
When Images Are Included
| Event Type | Source Image | Target Image |
|---|---|---|
| LPR | Yes — full scene | Yes — cropped plate |
| Face Detection | Yes — full scene | Yes — cropped face |
| Intrusion | Yes — full scene | Sometimes |
| Counting | Sometimes | Rarely |
| Traject | No | No |
Image availability depends on camera model and configuration. Always check source_image_exists() / target_image_exists() before accessing.
Code Examples
Saving Images to Disk
def handle_event(event, client_ip):
timestamp = event.get_time_stamp()
category = event.category
if event.source_image_exists():
with open(f"{category}_{timestamp}_source.jpg", "wb") as f:
f.write(event.get_source_image_bytes())
if event.target_image_exists():
with open(f"{category}_{timestamp}_target.jpg", "wb") as f:
f.write(event.get_target_image_bytes())
Sending via MQTT
import paho.mqtt.client as mqtt
mqtt_client = mqtt.Client()
mqtt_client.connect("localhost", 1883)
def handle_event(event, client_ip):
if event.category == "lpr" and event.source_image_exists():
# Publish raw JPEG bytes
mqtt_client.publish(
"camera/lpr/image",
event.get_source_image_bytes()
)
Base64 for Web/API
import json
def handle_event(event, client_ip):
if event.category == "face" and event.target_image_exists():
# Base64 string ready for JSON or HTML <img> tags
face_b64 = event.get_target_image()
payload = json.dumps({
"camera": event.get_ip_cam(),
"face_image": face_b64
})
# Send to your API, embed in HTML as data:image/jpeg;base64,...
Checking Before Accessing
def handle_event(event, client_ip):
if not event.images_exist():
print(f"No images in {event.category} event")
return
if event.source_image_exists():
source_bytes = event.get_source_image_bytes()
print(f"Source image: {len(source_bytes)} bytes")
if event.target_image_exists():
target_bytes = event.get_target_image_bytes()
print(f"Target image: {len(target_bytes)} bytes")
Image Sizes
Typical image sizes vary by camera resolution and model:
- Source images: 50KB-500KB (full resolution camera snapshot)
- Target images: 5KB-50KB (cropped region)
These are embedded in the XML payload, so events with images are significantly larger than events without.
Related Pages
- LPR Events — plate number with plate crop images
- Face Detection Events — face detection with face crop images
- ViewtronEvent — common event properties