Python SDK

Official Python library for integrating with Perimi's video intelligence platform

Easy Installation

Install via pip with all dependencies included

Type Hints

Full type annotation support for better IDE experience

Async Support

Built-in async/await support for high-performance applications

Authentication

Automatic API key management and token refresh

Quick Installation

Install Command
pip install perimi-python

Requirements

Python 3.8+ required
Works with pip, conda, poetry
No additional dependencies needed

Optional Features

[async]Async/await support with aiohttp
[dev]Development tools and testing
[all]All optional dependencies

Code Examples

1

Installation

Install the Perimi Python SDK via pip

Python
# Install from PyPI
pip install perimi-python

# Or install with async support
pip install perimi-python[async]
2

Basic Usage

Initialize the client and start making API calls

Python
from perimi import Perimi

# Initialize with API key
client = Perimi(api_key="your-api-key")

# List all streams
streams = client.streams.list()
print(f"Found {len(streams)} streams")

# Get stream details
stream = client.streams.get("stream-id")
print(f"Stream: {stream.name} - Status: {stream.status}")
3

Stream Management

Create and manage video streams

Python
# Create a new stream
stream = client.streams.create(
    name="Warehouse Camera 1",
    location="Building A - Entrance",
    connection_url="rtsp://192.168.1.100:554/stream",
    credentials={
        "username": "admin",
        "password": "password123"
    },
    tags=["entrance", "high-priority"]
)

# Update stream configuration
stream.update(
    detection_zones=[
        {
            "name": "Entry Zone",
            "coordinates": [[100, 100], [400, 100], [400, 300], [100, 300]]
        }
    ]
)

# Delete a stream
stream.delete()
4

Anomaly Detection

Retrieve and process anomaly detections

Python
# Get recent anomalies
anomalies = client.anomalies.list(
    stream_id="stream-id",
    since="2024-01-14T00:00:00Z",
    limit=100
)

# Process each anomaly
for anomaly in anomalies:
    print(f"Anomaly detected: {anomaly.type}")
    print(f"Confidence: {anomaly.confidence:.2f}")
    print(f"Timestamp: {anomaly.timestamp}")

    if anomaly.confidence > 0.8:
        # High confidence - send alert
        client.alerts.send(
            stream_id=anomaly.stream_id,
            message=f"High confidence {anomaly.type} detected"
        )
5

Async Usage

Use async client for better performance

Python
import asyncio
from perimi import AsyncPerimi

async def main():
    # Initialize async client
    client = AsyncPerimi(api_key="your-api-key")

    # Concurrent API calls
    streams, anomalies = await asyncio.gather(
        client.streams.list(),
        client.anomalies.list(limit=50)
    )

    print(f"Streams: {len(streams)}, Anomalies: {len(anomalies)}")

    # Clean up
    await client.close()

# Run async function
asyncio.run(main())
6

Error Handling

Robust error handling and retry logic

Python
from perimi import Perimi, PerimiError, RateLimitError

client = Perimi(api_key="your-api-key")

try:
    stream = client.streams.get("invalid-id")
except PerimiError as e:
    if e.status_code == 404:
        print("Stream not found")
    elif isinstance(e, RateLimitError):
        print(f"Rate limited. Retry after: {e.retry_after} seconds")
    else:
        print(f"API error: {e.message}")

# Built-in retry with exponential backoff
from perimi.retry import with_retry

@with_retry(max_attempts=3, backoff=2.0)
def create_stream_with_retry():
    return client.streams.create(
        name="Retry Test Stream",
        connection_url="rtsp://example.com/stream"
    )

API Reference

Client Methods

client.streams.list()

List all streams

client.streams.get(id)

Get stream by ID

client.streams.create(**kwargs)

Create new stream

client.anomalies.list(**filters)

List anomalies with filters

Configuration

PERIMI_API_KEY

Environment variable for API key

PERIMI_BASE_URL

Custom API endpoint (optional)

PERIMI_TIMEOUT

Request timeout in seconds

PERIMI_DEBUG

Enable debug logging

Ready to Build?

Start integrating Perimi into your Python applications today

Perim - AI-Powered Anomaly Detection for Critical Infrastructure