Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/storage.py @ 1295:51893de43293
adding comments and preparing functions to load bounding boxes from dltrack
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 14 Feb 2025 15:01:40 -0500 |
| parents | 46a30ce1a2e4 |
| children | d073524de272 |
comparison
equal
deleted
inserted
replaced
| 1294:3deeb42dffdd | 1295:51893de43293 |
|---|---|
| 470 connection.commit() | 470 connection.commit() |
| 471 | 471 |
| 472 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): | 472 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): |
| 473 '''Loads bounding box moving object from an SQLite | 473 '''Loads bounding box moving object from an SQLite |
| 474 (format of SQLite output by the ground truth annotation tool | 474 (format of SQLite output by the ground truth annotation tool |
| 475 or Urban Tracker | 475 or Urban Tracker''' |
| 476 | |
| 477 Load descriptions?''' | |
| 478 objects = [] | 476 objects = [] |
| 479 if Path(filename).is_file(): | 477 if Path(filename).is_file(): |
| 480 with sqlite3.connect(filename) as connection: | 478 with sqlite3.connect(filename) as connection: |
| 481 if objectType == 'bb': | 479 if objectType == 'bb': |
| 482 topCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbtop', objectNumbers, timeStep) | 480 topCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbtop', objectNumbers, timeStep) |
| 489 annotation = moving.BBMovingObject(num, t, b, t.getTimeInterval(), userTypes[num]) | 487 annotation = moving.BBMovingObject(num, t, b, t.getTimeInterval(), userTypes[num]) |
| 490 objects.append(annotation) | 488 objects.append(annotation) |
| 491 else: | 489 else: |
| 492 print ('Unknown type of bounding box {}'.format(objectType)) | 490 print ('Unknown type of bounding box {}'.format(objectType)) |
| 493 return objects | 491 return objects |
| 492 | |
| 493 def saveBBMovingObjectsToSqlite(outputFilename, objects): | |
| 494 '''Saves bounding boxes as obtained from annotation | |
| 495 object detector-based tracking tool like dltrack | |
| 496 CREATE TABLE bounding_boxes ( 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 ) )''' | |
| 497 pass | |
| 494 | 498 |
| 495 def saveInteraction(cursor, interaction): | 499 def saveInteraction(cursor, interaction): |
| 496 roadUserNumbers = list(interaction.getRoadUserNumbers()) | 500 roadUserNumbers = list(interaction.getRoadUserNumbers()) |
| 497 cursor.execute('INSERT INTO interactions VALUES({}, {}, {}, {}, {})'.format(interaction.getNum(), roadUserNumbers[0], roadUserNumbers[1], interaction.getFirstInstant(), interaction.getLastInstant())) | 501 cursor.execute('INSERT INTO interactions VALUES({}, {}, {}, {}, {})'.format(interaction.getNum(), roadUserNumbers[0], roadUserNumbers[1], interaction.getFirstInstant(), interaction.getLastInstant())) |
| 498 | 502 |
| 561 return [] | 565 return [] |
| 562 return interactions | 566 return interactions |
| 563 # load first and last object instants | 567 # load first and last object instants |
| 564 # 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 | 568 # 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 |
| 565 | 569 |
| 566 def createBoundingBoxTable(filename, invHomography = None): | 570 def createBoundingBoxTableFromFeatures(filename, invHomography = None): |
| 567 '''Create the table to store the object bounding boxes in image space | 571 '''Create the table to store the object bounding boxes in image space |
| 568 ''' | 572 calculated from the object feature coordinates''' |
| 569 with sqlite3.connect(filename) as connection: | 573 with sqlite3.connect(filename) as connection: |
| 570 cursor = connection.cursor() | 574 cursor = connection.cursor() |
| 571 try: | 575 try: |
| 572 cursor.execute('CREATE TABLE IF NOT EXISTS bounding_boxes (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))') | 576 cursor.execute('CREATE TABLE IF NOT EXISTS bounding_boxes (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))') |
| 573 cursor.execute('INSERT INTO bounding_boxes SELECT object_id, frame_number, min(x), min(y), max(x), max(y) from ' | 577 cursor.execute('INSERT INTO bounding_boxes SELECT object_id, frame_number, min(x), min(y), max(x), max(y) from ' |
