changeset 385:1917db662aa7

added rescaling options to scripts play-video and display-trajectories
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 22 Jul 2013 18:33:47 -0400
parents 6da9cf5609aa
children 8bc632cb8344
files python/cvutils.py scripts/display-trajectories.py scripts/play-video.py
diffstat 3 files changed, 29 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Mon Jul 22 18:11:01 2013 -0400
+++ b/python/cvutils.py	Mon Jul 22 18:33:47 2013 -0400
@@ -100,7 +100,17 @@
         for i in range(0, last-1):
             cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color)
 
-    def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None):
+    def cvImshow(windowName, img, rescale = 1.0):
+        'Rescales the image (in particular if too large)'
+        from cv2 import resize
+        if rescale != 1.:
+            size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale)))
+            resizedImg = resize(img, size)
+            cv2.imshow(windowName, resizedImg)
+        else:
+            cv2.imshow(windowName, img)
+
+    def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.):
         '''Plays the video'''
         wait = 5
         if frameRate > 0:
@@ -121,7 +131,7 @@
                     frameNum+=1
                     if text != None:
                        cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 
-                    cv2.imshow('frame', img)
+                    cvImshow('frame', img, rescale)
                     key = cv2.waitKey(wait)
             cv2.destroyAllWindows()
 
@@ -146,7 +156,7 @@
                         images.append(img)
         return images
 
-    def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True):
+    def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
         '''Displays the objects overlaid frame by frame over the video '''
         capture = cv2.VideoCapture(videoFilename)
         if capture.isOpened():
@@ -173,7 +183,7 @@
                                     obj.projectedPositions = obj.positions
                             draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant())
                             cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
-                    cv2.imshow('frame', img)
+                    cvImshow('frame', img, rescale)
                     key = cv2.waitKey()
                     if saveKey(key):
                         cv2.imwrite('image.png', img)
--- a/scripts/display-trajectories.py	Mon Jul 22 18:11:01 2013 -0400
+++ b/scripts/display-trajectories.py	Mon Jul 22 18:33:47 2013 -0400
@@ -14,6 +14,7 @@
 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'feature')
 parser.add_argument('-o', dest = 'homography', help = 'name of the image to world homography')
 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int)
+parser.add_argument('-r', dest = 'rescale', help = 'rescaling factor for the displayed image', default = 1., type = float)
 
 args = parser.parse_args()
 
@@ -36,4 +37,4 @@
     firstFrameNum = args.firstFrameNum
 
 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
-cvutils.displayTrajectories(videoFilename, objects, homography, firstFrameNum)
+cvutils.displayTrajectories(videoFilename, objects, homography, firstFrameNum, rescale = args.rescale)
--- a/scripts/play-video.py	Mon Jul 22 18:11:01 2013 -0400
+++ b/scripts/play-video.py	Mon Jul 22 18:33:47 2013 -0400
@@ -1,22 +1,23 @@
 #! /usr/bin/env python
 
-import sys,getopt
+import sys, argparse
 import cvutils
 
-options, args = getopt.getopt(sys.argv[1:], 'hi:f:',['help', 'fps=']) 
-options = dict(options)
-print options
 
-if '--help' in options.keys() or '-h' in options.keys() or len(sys.argv) == 1:
-    print('Usage: '+sys.argv[0]+' --help|-h -i video-filename [-f first_frame] [--fps frame_rate]')
-    sys.exit()
+parser = argparse.ArgumentParser(description='The program displays the video.')
+parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True)
+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)
+
+args = parser.parse_args()
 
 firstFrameNum = 0
-if '-f' in options.keys():
-    firstFrameNum = int(options['-f'])
+if args.firstFrameNum != None:
+    firstFrameNum = args.firstFrameNum
 
 frameRate = -1
-if '--fps' in options.keys():
-    frameRate = int(options['--fps'])
+if args.frameRate != None:
+    frameRate = args.frameRate
 
-cvutils.playVideo(options['-i'], firstFrameNum, frameRate)
+cvutils.playVideo(args.videoFilename, firstFrameNum, frameRate, rescale = args.rescale)