-

Sunday 21 January 2018

Video processing in python: Scikit Video (skvideo) tutorial for reading, writing and processing of the videos

In this post, I would like to give a small tutorial to get started with skvideo. skvideo is python package which is very useful to read videos, write videos and process the videos in python.

I am organizing this particular post in the format of questions and answers.
1. Dependencies and Installation
  • Either ffmpeg (version >= 2.8) or libav (either version 10 or 11)
  • python (2.6, 2.7, 3.3, and 3.5)
  • numpy (version >= 1.9.2)
  • scipy (version >= 0.16.0)
  • PIL/Pillow (version >= 3.1)
  • scikit-learn (version >= 0.18)
Installing ffmpeg
 sudo add-apt-repository ppa:mc3man/trusty-media   
 sudo apt-get update   
 sudo apt-get install ffmpeg   
 sudo apt-get install frei0r-plugins   
 $ sudo pip install numpy scipy scikit-learn PIL  


2. How to install scikit-video or skvideo in Ubuntu?
 $ sudo pip install sk-video  
 
You can also install by cloning the skvideo from git Link
$ git clone https://github.com/scikit-video/scikit-video 
$ cd scikit-video
$ sudo python setup.py install
$ python
import skvideo 

3. How to read and write videos using skvideo or scikit-video?

Reading video using skvideo scikit video:

import skvideo.io  
videodata = skvideo.io.vread("video_file_name")  
print(videodata.shape)

print("Loading only luminance channel")
vid = skvideo.io.vread(filename, outputdict={"-pix_fmt": "gray"})[:, :, :, 0]
print(vid.shape)
print("Enforcing video shape")
vid = skvideo.utils.vshape(vid)
print(vid.shape)
print("") 
 
print("Loading only first 5 luminance channel frames")
vid = skvideo.io.vread(filename, num_frames=5, outputdict={"-pix_fmt": "gray"})[:, :, :, 0]
print(vid.shape)
print("Enforcing video shape")
vid = skvideo.utils.vshape(vid)
print(vid.shape)
print("")
 

Writing video using skvideo scikit video:

 import skvideo.io  
 import numpy as np  
 outputdata = np.random.random(size=(5, 480, 680, 3)) * 255  
 outputdata = outputdata.astype(np.uint8)  
 skvideo.io.vwrite("outputvideo.mp4", outputdata)  

4. Local mean substract contrast normalization (MSCN) on images using scikit video:

 import skvideo.io  
 x_mscn,x_var,x_mu = skvideo.utils.compute_image_mscn_transform(x)
where x is the 2D input image and x_mscn is mscn version of x.

For more details please see detailed documentation Link

5. Edge detection of video using python, opencv and skvideo.  

Handling videos are always the difficult task, by using skvideo we can read write videos easily in python with the single line by importing skvideo. After this, it is pretty much straight apply any edge detection algorithm frame-by-frame basis.

The following code is self-explanatory...!

import skvideo.io
import skvideo.datasets
import cv2
import numpy as np
from matplotlib import pyplot as plt

videodata = skvideo.io.vread(skvideo.datasets.bigbuckbunny()) # loads the in build test video
# you can read your own video with following code
#videodata = skvideo.io.vread("your_video_file_name")  
edges = np.zeros(videodata.shape[0:3])
for i in range(videodata.shape[0]):
    edges[i] = cv2.Canny(videodata[i],100,200) # canny edge detection
    # you can add othe edge detectors from opencv lib
skvideo.io.vwrite("edge_video.mp4", edges)     # saving edge video


#you can view the video frame and corresponding edge with following code

plt.subplot(2,1,1),plt.imshow(videodata[0],cmap = 'gray')
plt.title('video'), plt.xticks([]), plt.yticks([])
plt.subplot(2,1,2),plt.imshow(edges[0],cmap = 'gray')
plt.title('canny edge'), plt.xticks([]), plt.yticks([])
plt.show()

5 comments: