changeset 657:51269511229b

added script printing video info
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 13 May 2015 17:24:19 +0200
parents 2813d74b3635
children 6668f541b915
files python/cvutils.py scripts/info-video.py scripts/play-video.py
diffstat 3 files changed, 63 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Wed May 13 15:11:03 2015 +0200
+++ b/python/cvutils.py	Wed May 13 17:24:19 2015 +0200
@@ -36,6 +36,12 @@
 def saveKey(key):
     return chr(key&255) == 's'
 
+def int2FOURCC(x):
+    fourcc = ''
+    for i in xrange(4):
+        fourcc += unichr((x >> 8*i)&255)
+    return fourcc
+
 def plotLines(filename, origins, destinations, w = 1, resultFilename='image.png'):
     '''Draws lines over the image '''
     import Image, ImageDraw # PIL
@@ -119,7 +125,7 @@
         newCameraMatrix[1,2] = newImgSize[1]/2.
         return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1)
 
-    def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.):
+    def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1):
         '''Plays the video'''
         windowName = 'frame'
         cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
@@ -135,11 +141,13 @@
             frameNum = firstFrameNum
             capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
             while ret and not quitKey(key):
-                ret, img = capture.read()
+                #ret, img = capture.read()
+                for i in xrange(step):
+                    ret, img = capture.read()
                 if ret:
                     if printFrames:
                         print('frame {0}'.format(frameNum))
-                    frameNum+=1
+                    frameNum+=step
                     if text is not None:
                        cv2.putText(img, text, (10,50), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) 
                     cvImshow(windowName, img, rescale)
@@ -150,6 +158,44 @@
         else:
             print('Video capture for {} failed'.format(filename))
 
+    def infoVideo(filename):
+        '''Provides all available info on video '''
+        cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format",
+                           cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)",
+                           cv2.cv.CV_CAP_PROP_FPS: "fps",
+                           cv2.cv.CV_CAP_PROP_FRAME_COUNT: "number of frames",
+                           cv2.cv.CV_CAP_PROP_FRAME_HEIGHT: "heigh",
+                           cv2.cv.CV_CAP_PROP_FRAME_WIDTH: "width",
+                           cv2.cv.CV_CAP_PROP_RECTIFICATION: "rectification",
+                           cv2.cv.CV_CAP_PROP_SATURATION: "saturation"}
+        capture = cv2.VideoCapture(filename)
+        if capture.isOpened():
+            for cvprop in [#cv2.cv.CV_CAP_PROP_BRIGHTNESS
+                    #cv2.cv.CV_CAP_PROP_CONTRAST
+                    #cv2.cv.CV_CAP_PROP_CONVERT_RGB
+                    #cv2.cv.CV_CAP_PROP_EXPOSURE
+                    cv2.cv.CV_CAP_PROP_FORMAT,
+                    cv2.cv.CV_CAP_PROP_FOURCC,
+                    cv2.cv.CV_CAP_PROP_FPS,
+                    cv2.cv.CV_CAP_PROP_FRAME_COUNT,
+                    cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,
+                    cv2.cv.CV_CAP_PROP_FRAME_WIDTH,
+                    #cv2.cv.CV_CAP_PROP_GAIN,
+                    #cv2.cv.CV_CAP_PROP_HUE
+                    #cv2.cv.CV_CAP_PROP_MODE
+                    #cv2.cv.CV_CAP_PROP_POS_AVI_RATIO
+                    #cv2.cv.CV_CAP_PROP_POS_FRAMES
+                    #cv2.cv.CV_CAP_PROP_POS_MSEC
+                    #cv2.cv.CV_CAP_PROP_RECTIFICATION,
+                    #cv2.cv.CV_CAP_PROP_SATURATION
+            ]:
+                prop = capture.get(cvprop)
+                if cvprop == cv2.cv.CV_CAP_PROP_FOURCC and prop > 0:
+                    prop = int2FOURCC(int(prop))
+                print('Video {}: {}'.format(cvPropertyNames[cvprop], prop))
+        else:
+            print('Video capture for {} failed'.format(filename))
+
     def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'):
         '''Returns nFrames images from the video sequence'''
         from math import floor, log10
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/info-video.py	Wed May 13 17:24:19 2015 +0200
@@ -0,0 +1,12 @@
+#! /usr/bin/env python
+
+import sys, argparse
+import cvutils
+
+
+parser = argparse.ArgumentParser(description='The program displays the video.')
+parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True)
+
+args = parser.parse_args()
+
+cvutils.infoVideo(args.videoFilename)
--- a/scripts/play-video.py	Wed May 13 15:11:03 2015 +0200
+++ b/scripts/play-video.py	Wed May 13 17:24:19 2015 +0200
@@ -9,6 +9,7 @@
 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int)
 parser.add_argument('--fps', dest = 'frameRate', help = 'approximate frame rate to replay', type = float)
 parser.add_argument('-r', dest = 'rescale', help = 'rescaling factor for the displayed image', default = 1., type = float)
+parser.add_argument('-s', dest = 'step', help = 'display every s image', default = 1, type = int)
 
 args = parser.parse_args()
 
@@ -20,4 +21,4 @@
 if args.frameRate is not None:
     frameRate = args.frameRate
 
-cvutils.playVideo(args.videoFilename, firstFrameNum, frameRate, rescale = args.rescale)
+cvutils.playVideo(args.videoFilename, firstFrameNum, frameRate, rescale = args.rescale, step = args.step)