Help Center

Get advice and help from our expert team

Chrysalis Cloud Python SDK for Computer Vision in the Cloud

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

Written by Igor Rendulic
Last updated 2 months ago

Why should I use Chrysalis Python SDK?

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

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 more practical and handles real-life use cases.

Computer vision is difficult enough without having to deal with the complexities of stream management. The SDK handles latency, disconnections and threading so you don’t have to. 

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: chryssdk
channels:
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - ca-certificates=2020.1.1=0
  - certifi=2020.4.5.1=py37_0
  - ld_impl_linux-64=2.33.1=h53a641e_7
  - libedit=3.1.20181209=hc058e9b_0
  - libffi=3.2.1=hd88cf55_4
  - libgcc-ng=9.1.0=hdf63c60_0
  - libstdcxx-ng=9.1.0=hdf63c60_0
  - ncurses=6.2=he6710b0_0
  - openssl=1.1.1f=h7b6447c_0
  - pip=20.0.2=py37_1
  - python=3.7.7=hcf32534_0_cpython
  - readline=8.0=h7b6447c_0
  - setuptools=46.1.3=py37_0
  - tk=8.6.8=hbc83047_0
  - wheel=0.34.2=py37_0
  - xz=5.2.4=h14c3975_4
  - zlib=1.2.11=h7b6447c_3
  - av=7.0.1
  - numpy=1.18.1
  - opencv=4.2.0
  - redis-py=3.4.1
  - chryssdk=1.0.0
  - pip:
    - crc32c==2.0
    - Cython 
    - contextlib2
    - pillow
    - lxml
    - pyee==7.0.1

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.RtmpMedia(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 mentioned before, the Chrysalis Cloud Python SDK takes care of delivering crisp and clear images from your live video stream, regardless of the processing speeds. The SDK speed ups or slow downs due to 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.RtmpMedia(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.RtmpMedia(hos="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 from 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, etc…) 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.RtmpMedia(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
Did you find this article helpful?

0 Comments

Leave a Reply

Similar Articles
How to stream Mac camera to Chrysalis Cloud with FFmpeg

Apple’s iMac, MacBook, MacBook Air, and MacBook Pro computers incorporate a camera at the top of the display. Here are the instructions on how to use it with FFmpeg and Chrysalis Cloud.

How to stream from your laptop camera using Ubuntu to Chrysalis Cloud

Installation These instructions are specifically for installing FFmpeg on Ubuntu 18.04. Skip the installation section…

Get in touch
Leave a message

Sign Up

Arrow-up