Skip to main content

Chrysalis Python Cloud SDK for streaming live media

Project description

Python Chrysalis Cloud SDK

This repository houses the official Chrysalis Cloud Python SDK for use with Chryscloud.com cloud, end-to-end media streaming and analytics platform.

Chrysalis Cloud SDK aims to provide easy and powerful control over live media streaming consumption and ingestion into various machine learning libraries in the cloud.

If you're looking for a hybrid edge-cloud solution we recommend you look into our open source project Chrysalis Edge Proxy

Contents

Features

  • Easy integration with numerous machine learning platforms
  • Support for easy access to RTMP live video stream from Chrysalis Cloud (live video/audio streaming)
  • Supporting for any camera that has RTMP streaming abilities
  • Deals with complexities of media stream management
  • Secure access media streams

Prerequisite

Check FFmpeg version:

ffmpeg -version

Installation

Create environment.yml file.

You can easily add to this file 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.

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

  • all returned images are in numpy format.
  • all returned images are in bgr24 pixel format.

Check ChImage attributes for more details

Probe

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.

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

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.

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
    ----------
    data: numpy
        Image stored in numpy in bgr24 format
    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(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)

Thumbnail image from video stream

Thumbnails are in bgr24 format in numpy array. In fact all images for local consumption are in the same format. This makes it easy to consume images in any processing and analytics after.

import chrysalis

chrys = chrysalis.Connect(host="https://myserver.at.chrysalis.com", port="1234", password="mypassword", ssl_ca_cert="mycert.crt")

 d = datetime.today() - timedelta(hours=0, minutes=0, seconds=2)
img = chrys.Screenshot(dt=d)

Due to the nature of H.264 straming it is not guaranteed the successfulness of the Screenshot method. In case no screenshot was found img = None.

This function tries to traverse the H.264 buffered stream seeking for I-Frame. the closest I-Frame to given dt (timestamp) is returned if I-Frame found.

Turn Storage On and Off

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

All examples are in /examples folder. Create conda environment from prepared environment.yml in examples folder before you run the examples.

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

Development

Install FFmpeg

Mac OS X

brew install ffmpeg pkg-config

Ubuntu >= 18.04 LTS

On Ubuntu 18.04 LTS everything can come from the default sources:

sudo apt-get install -y python-dev pkg-config

# Library components
sudo apt-get install -y \
    libavformat-dev libavcodec-dev libavdevice-dev \
    libavutil-dev libswscale-dev libswresample-dev libavfilter-dev

Ubuntu < 18.04 LTS

On older Ubuntu releases you will be unable to satisfy these requirements with the default package sources. We recommend compiling and installing FFmpeg from source. For FFmpeg:

sudo apt install \
    autoconf \
    automake \
    build-essential \
    cmake \
    libass-dev \
    libfreetype6-dev \
    libjpeg-dev \
    libtheora-dev \
    libtool \
    libvorbis-dev \
    libx264-dev \
    pkg-config \
    wget \
    yasm \
    zlib1g-dev

wget http://ffmpeg.org/releases/ffmpeg-3.2.tar.bz2
tar -xjf ffmpeg-3.2.tar.bz2
cd ffmpeg-3.2

./configure --disable-static --enable-shared --disable-doc
make

Installing

git clone https://github.com/cocoonhealth/ChrysalisPythonSDK.git

cd ChrysalisPythonSDK

sudo pip install -e . 

This should install it's dependencies also.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process of submitting pull requests to us.

Versioning

Current version is initial release - 1.0.0

License

This project is licensed under Apache 2.0 License - see the LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chrysalis-1.0.0.tar.gz (15.0 kB view hashes)

Uploaded Source

Built Distribution

chrysalis-1.0.0-py3-none-any.whl (23.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page