Chrysalis Cloud Python SDK for Computer Vision in the Cloud

Why should I use Chrysalis Python SDK?

Chrysalis Python SDK is designed to easily consume Chrysalis Video Streams and aims to provide easy and powerful control over live media streaming consumption and ingestion into various machine learning frameworks.

Python Chrysalis Cloud SDK

Things get a lot more complicated if the webcam you want to stream from is not attached to a computer.

OpenCV provides an alternative way to call VideoCapture() method such thay it captures from a remote RTMP stream, but it’s incredibly fragile and impractical from a product development perspective. It breaks if you stop aggresively servicing the video stream.

For example: I you need to do some minimal machine learning (500ms) per frame on the incoming data. Things quickly fall apart.

OpenCV VideoCapture

Chrysalis Cloud Python SDK handles latency, disconnections and threading so you don’t have to. 

chrysalis cloud python sdk

What Chrysalis Python SDK does?

In the real world, you have to handle cases where the webcam stream disconnects, or speeds up, or slows down because of latency. You could spawn a new thread and use OpenCV, but our SDK is simply practical and handles real-life use cases.

Computer vision is difficult enough without having to deal with the complexities of stream management.

Check here how Chrysalis Cloud enables development of scalable, real-time computer vision products with a few lines of code.

Installation prerequisite

Create your Anaconda environment

Create an environment.yml file. The file includes couple of dependencies and additional image manipulation libraries such as Pillow and OpenCV.

If you need GPU support, you can check how to work with Anaconda and GPU packages here


name: chryssdktest
channels:
  - conda-forge
dependencies:
  - ca-certificates=2020.1.1=0
  - certifi=2020.4.5.1=py37_0
  - pip=20.0.2=py37_1
  - wheel=0.34.2=py37_0
  - python=3.7.7=hcf32534_0_cpython
  - opencv=4.2.0
  - av=7.0.1
  - numpy=1.18.1
  - redis-py=3.4.1
  - pip:
    - Cython
    - chrysalis==1.0.0

Create new conda environment:

conda env create -f environment.yml

Usage

Probing the live stream

Probing returns information about the streaming media. It gives you a sense if the camera is streaming, when it was last seen, what is the frame cache duration stored on the Chrysalis streaming server.

import chrysalis
# connection to Chrysalis Cloud
chrys = chrysalis.Connect(host="https://myserver.at.chrysvideo.com", port="1234", password="mypassword", ssl_ca_cert="mycert.cer")
# returns ProbeInfo object
probe = chrys.Probe()
print("start {}, end {}, duration {} s, assessed fps {}".format(probe.start_timestamp, probe.end_timestamp, probe.duration, probe.fps))

The ProbeInfo object returns the information about cached frames as well as assessment of FPS (frames per second) streamed from the camera.

start_timestamp and end_timestamp are UTC times in milliseconds since epoch.

duration returns duration in seconds of the cached stream. By default Chrysalis Cloud holds latest 500 frames in the cache for each streaming camera. Duration is simply: end_timestamp – start_timestamp. 

ProbeInfo Attributes
    ----------
    start_timestamp : int
        Earlies contained media data in video stream cache
    end_timestamp : int
        Latest contained media data in video stream cache
    duration : int
        Duration of the buffered media stream in seconds
    fps : int
        Approximation of Frames per Second of source stream
    """

Retrieve latest video image from a live stream

As mention before Chrysalis Cloud Python SDK takes care of delivering crisp and clear images from your live video stream, regardless of the processing speeds, speed ups or slow downs because of the latency or even if your camera disconnects from the network.

Read more why this is important in this article.

import chrysalis

# connection to Chrysalis Cloud
chrys = chrysalis.Connect(host="https://myserver.at.chrysvideo.com", port="1234", password="mypassword", ssl_ca_cert="mycert.cer")

# Perpetual reading of the stream
while True:
    # VideoLatestImage returns ChImage object
    img = chrys.VideoLatestImage()

ChImage object returned from VideoLatestImage has a following structure:


 ChImage Attributes
    ----------
    start_timestamp : int
        Earlies contained media data in video stream cache
    end_timestamp : int
        Latest contained media data in video stream cache
    duration : int
        Duration of the buffered media stream in seconds
    fps : int
        Approximation of Frames per Second of source stream
    """

VideoLatestImage returns None image when frame not available

VideoLatestImage might return None in cases when querying for the next frame is faster than the camera stream produces them.

The SDK will not return already consumed frames (images) in the perpetual reading of the stream.

You can also consume live stream images from mutliple sinks in case when you need to run the same live stream (e.g. the same image) through multiple Computer Vision algorithms. Not returning already consumed frames applies per SDK instance basis.

Retrieve video images from the past

Based on what is available in the frame cache on Chrysalis streaming nodes you can also query video images from the past. Use Probing in case you need more information how much back in time you can query the video stream.

import chrysalis

# connection to Chrysais Cloud
chrys = chrysalis.Connect(host="https://myserver.at.chrysvideo.com", prt="1234", password="mypassword", ssl_ca_cert="mycert.cer")

probe = ch.Probe()
start = probe.end_timestamp - (1000 * 30) # 30 seconds in the past
end = probe.end_timestamp - (1000 * 15) # until 15 seconds in the past (end > start)

# Perpetual reading of the stream until end is reached
while True:
    # VideoLatestImage returns ChImage object
    img = ch.VideoPastImage(start, end)

 

Turn Storage On and Off (Chrysalis Video Storage)

Based on video analysis you can decide to store a stream into the permanent Chrysalis Cloud storage. Since live video form a webcam might be streaming 24/7 we don’t necessarily need to store everything, but rather we can perform simple analysis (e.g. movement detection, face recognition, …) to decide when and for how long we want to permanently store that video segment.

Coming soon

Example: Display live stream with OpenCV

import chrysalis

# connection to Chrysalis Cloud
chrys = chrysalis.Connect(host="https://myserver.at.chrysalis.com", port="1234", password="mypassword", ssl_ca_cert="mycert.cer")

# Perpetual reading of the stream
while True:
    # VideoLatestImage returns ChImage object
    img = chrys.VideoLatestImage()
    if img is not None:
        cv2.imshow("live video", img.data)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

What’s next?

  • Check our Machine Learning examples