Package: dataset2database
dataset2databaseAboutThe package is made as a solution when using video inputs in Machine Learning models. As extracting and storing frames in.JPEG/.PNGfiles will quickly increase the memory requirements and more importantly the number ofinodes, the package provides a convenient alternative. Video frames are stored as blobs at database file.dbwhich can be read as quickly as the.JPEGfiles 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 thedataset2database/jpgs2singlefile.py)Package requirementsThe three required packages areopencvfor image/frame loading,numpyfor array manipulation andtqdmfor 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 structureThe 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>
  │      │─── ...
 ...    ...UsageThe main code is at thejpgs2single.pyfile. To run the convertor simply call theconvertfunction with the base directory of the dataset and the destination directory for where to save the generated databases.fromdataset2databaseimportconvert#orfromjpgs2singlefileimportconvertconvert(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 yourmy_dataset_dir.Frames.db filesVideo frames are stored inframes.dbfiles with their video name and frame number as theirObjIDand the frames array are stored asblobs. The name format is basically<video_name>/frame_[frame number in 5-digit format]File viewer:If you want to ensure that everything has been converted correctly, you can useSQLiteStudiowhich provides an easy to use multi-platform interface (available for Windows, Mac and Ubuntu).Database loadingLoading the database can easily be done with an SQLSELECTcommand based on a list of all frames with specifiedObjIds. Then, with the help ofnp.fromstring()andcv2.imdecode()functions the images can be again converted touint8arrays.An example of data loading in python can be found below:importsqlite3importcv2importnumpyasnpcon=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))forindexinframe_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=0row=row.fetchall()# Video order re-arangementforObjId,iteminrow:#--- Decode blobnparr=np.fromstring(item,np.uint8)img=cv2.imdecode(nparr,cv2.IMREAD_COLOR)ids.append(ObjId)frames.append(img)i+=1# Ensuring correct order of framesframes=[framefor_,frameinsorted(zip(ids,frames),key=lambdapair:pair[0])]# (if required) array conversion [frames x height x width x channels]frames=np.asarray(frames)cur.close()con.close()Installation through gitPlease 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 pipThe latest stable release is also available for download through pip$ pip install dataset2database