Skip to main content

dataset2database

supported versions Tweet

About

The package is made as a solution when using video inputs in Machine Learning models. As extracting and storing frames in .JPEG/.PNG files will quickly increase the memory requirements and more importantly the number of inodes, the package provides a convenient alternative. Video frames are stored as blobs at database file .db which can be read as quickly as the .JPEG files but without the additional large memory requirements.

Currently supported video formats include .mp4,mpeg-4,.avi,.wmv. If you have a different extension, you can simply change the script to include them (in the dataset2database/jpgs2singlefile.py)


Package requirements

The three required packages are opencv for image/frame loading, numpy for array manipulation and tqdm for verbose. Make sure that all packages installed before running any functions.

Multiprocessing: The code uses multiprocessing for improving speeds, thus the total time required for the conversion varies across different processors. The code has been tested on an AMD Threadripper 2950X with an average conversion time of 48 minutes for ~500K videos.


Dataset structure

The package assumes a fixed dataset structure such as:

<dataset>    
  │
  └──<class 1>
  │     │
  │     │─── <video_data_1.mp4>
  │     │─── <video_data_2.mp4>
  │     │─── ...
  │    ...      
  │
  └───<class 2>
  │      │
  │      │─── <video_data_1.mp4>
  │      │─── <video_data_2.mp4>
  │      │─── ...
 ...    ...


Usage

The main code is at the jpgs2single.py file. To run the convertor simply call the convert function with the base directory of the dataset and the destination directory for where to save the generated databases.

from dataset2database import convert
#or
from jpgs2singlefile import convert

convert(my_dataset_dir, my_target_dir)

! Please not that you need to use a "/" for Unix-based systems or a "//" for Windows-based systems alongside your my_dataset_dir.


Frames.db files

Video frames are stored in frames.db files with their video name and frame number as their ObjID and the frames array are stored as blobs. The name format is basically <video_name>/frame _ [frame number in 5-digit format]

dataset2database

File viewer: If you want to ensure that everything has been converted correctly, you can use SQLiteStudio which provides an easy to use multi-platform interface (available for Windows, Mac and Ubuntu).


Database loading

Loading the database can easily be done with an SQL SELECT command based on a list of all frames with specified ObjIds. Then, with the help of np.fromstring() and cv2.imdecode() functions the images can be again converted to uint8 arrays.

An example of data loading in python can be found below:

import sqlite3
import cv2
import numpy as np

con = sqlite3.connect('my_video_database.db')
cur = con.cursor()


# retrieve entire video from database (frames are unordered)
frame_names = ["{}/{}".format(my_path.split('/')[-1],'frame_%05d'%(index+1)) for index in frame_indices]
sql = "SELECT Objid, frames FROM Images WHERE ObjId IN ({seq})".format(seq=','.join(['?']*len(frame_names)))
row = cur.execute(sql,frame_names)

ids = []
frames = []
i = 0

row = row.fetchall()
# Video order re-arangement
for ObjId, item in row:
  #--- Decode blob
  nparr  = np.fromstring(item, np.uint8)
  img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  ids.append(ObjId)
  frames.append(img)
  i+=1

# Ensuring correct order of frames
frames = [frame for _, frame in sorted(zip(ids,frames), key=lambda pair: pair[0])]

# (if required) array conversion [frames x height x width x channels]
frames = np.asarray(frames)

cur.close()
con.close()

Installation through git

Please make sure, Git is installed in your machine:

$ sudo apt-get update
$ sudo apt-get install git
$ git clone https://github.com/alexandrosstergiou/dataset2database.git
$ cd dataset2database
$ pip install .

You can then use it as any other package installed through pip.


Installation through pip

The latest stable release is also available for download through pip

$ pip install dataset2database

Release files for dataset2database 1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dataset2database 1.2
File Size Uploaded
dataset2database-1.2.tar.gz 17.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dataset2database 1.2
File Interpreter ABI Platform
dataset2database-1.2-py3-none-any.whl Python 3 none any Details

Total release size: 36.4 kB

Release files / dataset2database-1.2.tar.gz

Download URL dataset2database-1.2.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
175940ab4011362900e39ca575909a7a9325709798a70edb98526abbb9a2486a
BLAKE2b-256 checksum
How to use checksums
1ce4b3ca514f36c5b6d2e2ce297b08ed4b50ccba93e858d7b9a2a4547e78f99a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.7

Release files / dataset2database-1.2-py3-none-any.whl

Download URL dataset2database-1.2-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8c8a8c016a2044b6a66404880db2166dbf22c8e36e1fe7948dd2ad592253299
BLAKE2b-256 checksum
How to use checksums
cbc43af98f3a75beddf946b641599808cd01e323e54a2f9fe018f71e68f459b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.7

Release history Release notifications | RSS feed

This release

1.2 This release

2 release files

1.1

2 release files

1.0

2 release files

0.1

1 release file

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page