Get duration of a video using ffmpeg or avconv

First you should have the ffmpeg dev packages installed. Follow this post to install the required ffmpeg dev packages.

Step 1: Open a video file using the function avformat_open_input.

Step 2: Get stream information using the function call avformat_find_stream_info.

Step 3: Duration of the video can be obtained from member duration of AVFormatContext.

Make sure you add linker flags -lavformat -lavcodec while you compile it.

face/eye/hand detection using openCV python binding

Haar transform can be used to detect a required object in an image. To detect an object using haar transform, we have to train by providing it some positive and negative images and generate an xml containing the required object’s features. It might take several days to completely train it. In this post, let us look at some already trained cases like face, eye or hand detection. If you have installed openCV python bindings provided by ubuntu, /usr/share/opencv/haarcascades/ directory has few trained xml feature files. Let us use few of them.

face detection

#!/usr/bin/python
import sys, os
from opencv.cv import *
from opencv.highgui import *
import opencv

def detectObjects(image):
    grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1)
    cvCvtColor(image, grayscale, CV_BGR2GRAY)

    storage = cvCreateMemStorage(0)
    cvClearMemStorage(storage)
    cvEqualizeHist(grayscale, grayscale)
    cascade = cvLoadHaarClassifierCascade('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',
                                          cvSize(1, 1))
    faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(50, 50))
    if faces:
        for i in faces:
            cvRectangle(image, cvPoint( int(i.x), int(i.y)),
                         cvPoint(int(i.x + i.width), int(i.y + i.height)),
                         CV_RGB(0, 255, 0), 3, 8, 0)

def main():
    cvNamedWindow("cam", 1)
    capture = cvCreateCameraCapture(0)
    orig = cvQueryFrame(capture)
    grey = cvCreateImage(cvGetSize(orig), IPL_DEPTH_8U, 1)
    face = cvCreateImage(cvGetSize(grey), IPL_DEPTH_8U, 1)
    while True:
        orig = cvQueryFrame(capture)
        cvCvtColor(orig, grey, CV_RGB2GRAY)
        detectObjects(orig)
        cvShowImage("cam", orig)
        k = cvWaitKey(10)
        if k == 'q': # press q to quit
            break

if __name__ == "__main__":
    main()

Here is the output,

eye detection

#!/usr/bin/python
import sys, os
from opencv.cv import *
from opencv.highgui import *
import opencv

def detectObjects(image):
    grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1)
    cvCvtColor(image, grayscale, CV_BGR2GRAY)

    storage = cvCreateMemStorage(0)
    cvClearMemStorage(storage)
    cvEqualizeHist(grayscale, grayscale)
    cascade = cvLoadHaarClassifierCascade('/usr/share/opencv/haarcascades/haarcascade_mcs_righteye.xml',
                                          cvSize(1, 1))
    faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(50, 50))
    if faces:
        for i in faces:
            cvRectangle(image, cvPoint( int(i.x), int(i.y)),
                         cvPoint(int(i.x + i.width), int(i.y + i.height)),
                         CV_RGB(0, 255, 0), 3, 8, 0)

def main():
    cvNamedWindow("cam", 1)
    capture = cvCreateCameraCapture(0)
    orig = cvQueryFrame(capture)
    grey = cvCreateImage(cvGetSize(orig), IPL_DEPTH_8U, 1)
    face = cvCreateImage(cvGetSize(grey), IPL_DEPTH_8U, 1)
    while True:
        orig = cvQueryFrame(capture)
        cvCvtColor(orig, grey, CV_RGB2GRAY)
        detectObjects(orig)
        cvShowImage("cam", orig)
        k = cvWaitKey(10)
        if k == 'q': # press q to quit
            break

if __name__ == "__main__":
    main()

In our next post, let us see how we can train haar transform to detect an object of our desire.

installing openCV python wrapper and executing simple program in ubuntu

I wanted to try image processing and openCV. So I set my journey. Installing openCV python bindings in ubuntu is just a piece of cake,

sudo apt-get install libcv4 libcvaux4 libhighgui4 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev

Following tutorials from various sites I tried to import the cv package in python,

import cv

And I got this error,

Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named cv

The reason is, there are different implementations of openCV bindings for python. Some of them are SWIG, ctypes-opencv, etc. Out of the box, Ubuntu provides old SWIG-based implementation of openCV python bindings. Here is a sample program that captures frames from webcam and performs edge detection using canny edge detector,

from opencv.cv import *
from opencv.highgui import *

cvNamedWindow("cam", 1)
cvNamedWindow("canny", 1)
capture = cvCreateCameraCapture(0)
orig = cvQueryFrame(capture)
grey = cvCreateImage(cvGetSize(orig), IPL_DEPTH_8U, 1)
canny = cvCreateImage(cvGetSize(grey), IPL_DEPTH_8U, 1)
while True:
    orig = cvQueryFrame(capture)
    cvCvtColor(orig, grey, CV_RGB2GRAY)
    cvShowImage("cam", grey)
    cvCanny(grey, canny, 50, 150, 3)
    cvShowImage("canny", canny)
    c = cvWaitKey(7) % 0x100
    if c == 27 or c == 10:
        break

You might find these links useful,
1) Python openCV Reference
2) CV Reference Manual