diff python/storage.py @ 390:12be4a0cb9aa

sql code to create bounding boxes in image space
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 25 Jul 2013 18:55:44 -0400
parents ba813f148ade
children eaf7765221d9
line wrap: on
line diff
--- a/python/storage.py	Thu Jul 25 16:01:12 2013 -0400
+++ b/python/storage.py	Thu Jul 25 18:55:44 2013 -0400
@@ -195,6 +195,8 @@
         utils.dropTables(connection, ['objects', 'objects_features'])
     elif dataType == 'interaction':
         utils.dropTables(connection, ['interactions', 'indicators'])
+    elif dataType == 'bb':
+        utils.dropTables(connection, ['boundingbox'])
     else:
         print('Unknown data type {} to delete from database'.format(dataType))
     connection.close()
@@ -289,6 +291,21 @@
 # load first and last object instants
 # CREATE TEMP TABLE IF NOT EXISTS object_instants AS SELECT OF.object_id, min(frame_number) as first_instant, max(frame_number) as last_instant from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id group by OF.object_id order by OF.object_id
 
+def createBoundingBoxTable(filename, invHomography = None):
+    '''Create the table to store the object bounding boxes
+    '''
+    connection = sqlite3.connect(filename)
+    cursor = connection.cursor()
+    try:
+        cursor.execute('CREATE TABLE IF NOT EXISTS boundingbox (object_id INTEGER, frame_number INTEGER, x_top_left REAL, y_top_left REAL, x_bottom_right REAL, y_bottom_right REAL,  PRIMARY KEY(object_id, frame_number))')
+        cursor.execute('INSERT INTO boundingbox SELECT object_id, frame_number, min(x), min(y), max(x), max(y) from '
+              '(SELECT object_id, frame_number, (x*{}+y*{}+{})/w as x, (x*{}+y*{}+{})/w as y from '
+              '(SELECT OF.object_id, P.frame_number, P.x_coordinate as x, P.y_coordinate as y, P.x_coordinate*{}+P.y_coordinate*{}+{} as w from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id)) '.format(invHomography[0,0], invHomography[0,1], invHomography[0,2], invHomography[1,0], invHomography[1,1], invHomography[1,2], invHomography[2,0], invHomography[2,1], invHomography[2,2])+
+              'GROUP BY object_id, frame_number')
+    except sqlite3.OperationalError as error:
+        utils.printDBError(error)
+    connection.commit()
+    connection.close()
 
 #########################
 # txt files