Skip to main content

Video reading into numpy

Project description

video2numpy

pypi Open In Colab Try it on gitpod

Optimized library for large-scale extraction of frames and audio from video.

Install

pip install video2numpy

Or build from source:

python setup.py install

Usage

NAME
    video2numpy - Read frames from videos and save as numpy arrays

SYNOPSIS
    video2numpy SRC <flags>

DESCRIPTION
    Input:
    src:
        str: path to mp4 file
        str: youtube link
        str: path to txt file with multiple mp4's or youtube links
        list: list with multiple mp4's or youtube links
    dest:
        str: directory where to save frames to
        None: dest = src + .npy
    take_every_nth:
        int: only take every nth frame
    resize_size:
        int: new pixel height and width of resized frame
    workers:
        int: number of workers used to read videos
    memory_size:
        int: number of GB of shared memory used for reading, use larger shared memory for more videos

POSITIONAL ARGUMENTS
    SRC

FLAGS
    --dest=DEST
        Default: ''
    --take_every_nth=TAKE_EVERY_NTH
        Default: 1
    --resize_size=RESIZE_SIZE
        Default: 224
    --workers=WORKERS
        Default: 1
    --memory_size=MEMORY_SIZE
        Default: 4

NOTES
    You can also use flags syntax for POSITIONAL ARGUMENTS

API

This module exposes a single function video2numpy which takes the same arguments as the command line tool:

import glob
from video2numpy import video2numpy

VIDS = glob.glob("some/path/my_videos/*.mp4")
FRAME_DIR = "some/path/my_frames"
take_every_5 = 5

video2numpy(VIDS, FRAME_DIR, take_every_5)

You can also directly use the reader and iterate over videos yourself:

import glob
from video2numpy.frame_reader import FrameReader

VIDS = glob.glob("some/path/my_videos/*.mp4")
take_every_5 = 5
resize_size = 300
batch_size = 64 # output shape will be (n, batch_size, height, width, 3)

reader = FrameReader(VIDS, take_every_5, resize_size, batch_size)
reader.start_reading()

for vid_frames, info_dict in reader:
    # info_dict["dst_name"] - name for saving numpy array
    # info_dict["pad_by"] - how many pad frames were added to final block so n_frames % batch_size == 0
    # do something with vid_frames of shape (n_blocks, 64, 300, 300, 3)
    ...

For development

Either locally, or in gitpod (do export PIP_USER=false there)

Setup a virtualenv:

python3 -m venv .env
source .env/bin/activate
pip install -e .

to run tests:

pip install -r requirements-test.txt

then

make lint
make test

You can use make black to reformat the code

python -m pytest -x -s -v tests -k "dummy" to run a specific test

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

video2numpy-2.3.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

video2numpy-2.3.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file video2numpy-2.3.0.tar.gz.

File metadata

  • Download URL: video2numpy-2.3.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for video2numpy-2.3.0.tar.gz
Algorithm Hash digest
SHA256 88b51df0023b7c159415579e7f954cb6b1492525ad7434bc91745fbc038be24b
MD5 8e397a35cf09b103092790e65ec0b239
BLAKE2b-256 1cea64a6e5e0627154d802233bdb2c6d94ca7f687d5355222111eb58158a1b31

See more details on using hashes here.

File details

Details for the file video2numpy-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: video2numpy-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for video2numpy-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53f559812613e573917a61c46dd2cf34c4670d418528a36e8081c199fb0080ba
MD5 bf915b6db798e24a8ac2e27eec5d68e7
BLAKE2b-256 5a396715dbd299dd9843d5abdbdcd0a41327a60356220fb8fa044c7ea33ae22b

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