Skip to main content

IMGStore houses your video frames

Project description

IMGStore - Houses Your Video And Data

Imgstore is a container for video frames and metadata. It allows efficient storage and seeking through recordings from hours to weeks in duration. It supports compressed and uncompressed formats.

Imgstore allows reading (and writing) videos recorded with loopbio's Motif recording system.

Introduction

The Concept

Video data is broken into chunks, which can be individual video files VideoImgStore, or a directory full of images DirectoryImgStore. The format of the chunks determines if the store is compressed, uncompressed, lossless or lossy.

Basic API

There are only a few public API entry points exposed (most operations are done on ImgStore objects (see writing and reading examples below).

  • new_for_filename(path) - Open a store for reading
  • new_for_format(format, path, **kwargs)
    • Open a store for writing
    • You also need to pass imgshape= and imgdtype
    • Note: imgshape is the array shape, i.e. (h,w,d) and not (w,h)
  • get_supported_formats() - list supports formats (remember to test after install)
  • extract_only_frame(path, frame_index) - extract a single frame at given index from file

Example: Write a store

import imgstore
import numpy as np
import cv2
import time

height = width = 500
blank_image = np.zeros((height,width,3), np.uint8)

store = imgstore.new_for_format('npy',  # numpy format (uncompressed raw image frames)
                                mode='w', basedir='mystore',
                                imgshape=blank_image.shape, imgdtype=blank_image.dtype,
                                chunksize=1000)  # 1000 files per chunk (directory)

for i in range(40):
    img = blank_image.copy()
    cv2.putText(img,str(i),(0,300), cv2.FONT_HERSHEY_SIMPLEX, 4, 255)
    store.add_image(img, i, time.time())

store.close()

You can also add additional (JSON serialable) data at any time, and this will be stored with a reference to the current frame_number so that it can be retrieved and easily combined later.

store.add_extra_data(temperature=42.5, humidity=12.4)

Example: Read a store

from imgstore import new_for_filename

store = new_for_filename('mystore/metadata.yaml')

print 'frames in store:', store.frame_count
print 'min frame number:', store.frame_min
print 'max frame number:', store.frame_max

# read first frame
img, (frame_number, frame_timestamp) = store.get_next_image()
print 'framenumber:', frame_number, 'timestamp:', frame_timestamp

# read last frame
img, (frame_number, frame_timestamp) = store.get_image(store.frame_max)
print 'framenumber:', frame_number, 'timestamp:', frame_timestamp

Extracting frames: frame index vs frame number

Stores maintain two separate and distinct concepts, 'frame number', which is any integer value associated with a single frame, and 'frame index', which is numbered from 0 to the number of frames in the store. This difference is visible in the API with

class ImgStore
    def get_image(self, frame_number, exact_only=True, frame_index=None):
        pass

where 'frame index' OR 'frame number' can be passed.

Extracting Metadata or Extra data

To get all the image metadata at once you can call ImgStore.get_frame_metadata() which will return a dictionary containing all frame_number and frame_timestamps.

To retrieve a pandas DataFrame of all extra data and associated frame_number and frame_timestamps call ImgStore.get_extra_data()

Command line tools

Some simple tools for creating, converting and viewing imgstores are provided

  • imgstore-view /path/to/store
    • view an imgstore
  • imgstore-save --format 'avc1/mp4' --source /path/to/input.mp4 /path/to/store/to/save
    • --source if omitted will be the first webcam
  • imgstore-test
    • run extensive tests to check opencv build has mp4 support and trustworthy encoding/decoding

Install

IMGStore depends on reliable OpenCV builds, and built with mp4/h264 support for writing mp4s. Loopbio provides reliable conda OpenCV builds in our conda channel, and we recommend using these.

Once you have a conda environment with a recent and reliable OpenCV build, you can install IMGStore from pip

$ pip install imgstore

After installing imgstore from any location, you should check it's tests pass to guarantee that you have a trustworthy OpenCV version

Installing from source and with all dependencies

  • git clone this repository
  • conda env create -f environment.yml

If you are on MacOSX

  • conda env create -f environment-mac.yml

Installing only IMGStore and using system dependencies

We recommend installing IMGStore dependencies using the conda package manager, however it is possible to create a virtual env which uses your system OpenCV install.

# generate virtual env
virtualenv ~/.envs/imgstore --system-site-packages
# activate the virtual env
source ~/.envs/imgstore/bin/activate
# install imgstore
pip install imgstore

Note: If you install in this manner you have to ensure that opencv is correct and has the required functionality (such as mp4 write support if required). Remember to run the tests imgstore-test after installing.

Post install testing

You should always run the command imgstore-test after installing imgstore. If your environment is working correctly you should see a lot of text printed, followed by the text ==== 66 passed, ..... ======

Release Checklist

  • test with GPL opencv/ffmpeg
  • test with LGPL opencv/ffmpeg
  • test with Python2.7 and Python3
  • git clean -dfx
  • python setup.py sdist bdist_wheel
  • twine upload --repository-url https://test.pypi.org/legacy/ dist/*
  • (test with pip, new env)
    • pip install --index-url https://test.pypi.org/simple/ imgstore

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

imgstore-0.2.3.tar.gz (899.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

imgstore-0.2.3-py2.py3-none-any.whl (902.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file imgstore-0.2.3.tar.gz.

File metadata

  • Download URL: imgstore-0.2.3.tar.gz
  • Upload date:
  • Size: 899.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/2.7.15

File hashes

Hashes for imgstore-0.2.3.tar.gz
Algorithm Hash digest
SHA256 9fb68cd6c346a0d10585b27b1ad367f532c2fa415096306ef04e78f2881f397c
MD5 c5c10ef92d6918aa423b18c289b4965c
BLAKE2b-256 d51c819e4c67fd665faf091565e6b34fd399df68a25e0e28d29d9c0de3bdd1cc

See more details on using hashes here.

File details

Details for the file imgstore-0.2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: imgstore-0.2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 902.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/2.7.15

File hashes

Hashes for imgstore-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f11b07ecfc8d49963b590bafb65856029a8c2037d14a791e9d9c0fb5a059ef67
MD5 9729ea9647a8af54a81dc9252ac841f7
BLAKE2b-256 060de943d6e8bd93bb558eb6d39de3403a4721e2c41b2c62e6f2aeb93759e1a9

See more details on using hashes here.

Supported by

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