changeset 393:eaf7765221d9

added code to create bounding boxes and display them (inc scripts)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 26 Jul 2013 02:12:08 -0400
parents dd4970f4221f
children 6567fee37c16
files python/cvutils.py python/storage.py scripts/create-bounding-boxes.py scripts/delete-tables.py scripts/display-trajectories.py
diffstat 5 files changed, 48 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Thu Jul 25 18:59:35 2013 -0400
+++ b/python/cvutils.py	Fri Jul 26 02:12:08 2013 -0400
@@ -156,7 +156,7 @@
                         images.append(img)
         return images
 
-    def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
+    def displayTrajectories(videoFilename, objects, boundingBoxes, 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():
@@ -182,6 +182,9 @@
                                 else:
                                     obj.projectedPositions = obj.positions
                             draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant())
+                            if frameNum in boundingBoxes.keys():
+                                for rect in boundingBoxes[frameNum]:
+                                    cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed)
                             cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
                     cvImshow('frame', img, rescale)
                     key = cv2.waitKey()
--- a/python/storage.py	Thu Jul 25 18:59:35 2013 -0400
+++ b/python/storage.py	Fri Jul 26 02:12:08 2013 -0400
@@ -307,6 +307,26 @@
     connection.commit()
     connection.close()
 
+def loadBoundingBoxTable(filename):
+    connection = sqlite3.connect(filename)
+    cursor = connection.cursor()
+    boundingBoxes = {} # list of bounding boxes for each instant
+    try:
+        cursor.execute('SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'boundingbox\'')
+        result = [row for row in cursor]
+        if len(result) > 0:
+            cursor.execute('SELECT * FROM boundingbox')
+            #objId = -1
+            for row in cursor:
+                #if row[0] != objId:
+                boundingBoxes.setdefault(row[1], []).append([moving.Point(row[2], row[3]), moving.Point(row[4], row[5])])
+    except sqlite3.OperationalError as error:
+        utils.printDBError(error)
+        return boundingBoxes
+    connection.close()
+    return boundingBoxes
+
+
 #########################
 # txt files
 #########################
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/create-bounding-boxes.py	Fri Jul 26 02:12:08 2013 -0400
@@ -0,0 +1,21 @@
+#! /usr/bin/env python
+
+import argparse
+
+import storage
+
+from numpy.linalg.linalg import inv
+from numpy import loadtxt
+
+parser = argparse.ArgumentParser(description='The program creates bounding boxes in image space around all features (for display and for comparison to ground truth in the form of bouding boxes.')
+parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True)
+parser.add_argument('-o', dest = 'homography', help = 'name of the image to world homography')
+
+args = parser.parse_args()
+
+homography = None
+if args.homography != None:
+    homography = inv(loadtxt(args.homography))            
+
+storage.createBoundingBoxTable(args.databaseFilename, homography)
+
--- a/scripts/delete-tables.py	Thu Jul 25 18:59:35 2013 -0400
+++ b/scripts/delete-tables.py	Fri Jul 26 02:12:08 2013 -0400
@@ -8,7 +8,7 @@
 parser = argparse.ArgumentParser(description='The program deletes the tables in the database before saving new results (for objects, tables object_features and objects are dropped; for interactions, the tables interactions and indicators are dropped')
 #parser.add_argument('configFilename', help = 'name of the configuration file')
 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database', required = True)
-parser.add_argument('-t', dest = 'dataType', help = 'type of the data to remove', required = True, choices = ['object','interaction'])
+parser.add_argument('-t', dest = 'dataType', help = 'type of the data to remove', required = True, choices = ['object','interaction', 'bb'])
 args = parser.parse_args()
 
 storage.removeFromSqlite(args.databaseFilename, args.dataType)
--- a/scripts/display-trajectories.py	Thu Jul 25 18:59:35 2013 -0400
+++ b/scripts/display-trajectories.py	Fri Jul 26 02:12:08 2013 -0400
@@ -37,4 +37,5 @@
     firstFrameNum = args.firstFrameNum
 
 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
-cvutils.displayTrajectories(videoFilename, objects, homography, firstFrameNum, rescale = args.rescale)
+boundingBoxes = storage.loadBoundingBoxTable(databaseFilename)
+cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, homography, firstFrameNum, rescale = args.rescale)