Example #1
0
 def setUp(self):
     self.conn = sqlite3.connect(':memory:')
     backendDb.createDb(self.conn)
     c = self.conn.cursor()
     c.execute('INSERT INTO images(imagefile) VALUES ("image0")')
     c.execute('INSERT INTO objects(imagefile,objectid,x1,y1,width,height) '
               'VALUES ("image0",0,45,25,10,10)')
     c.execute('INSERT INTO polygons(objectid,x,y) VALUES (0,45,25)')
     c.execute('INSERT INTO polygons(objectid,x,y) VALUES (0,55,25)')
     c.execute('INSERT INTO polygons(objectid,x,y) VALUES (0,55,35)')
     c.execute('INSERT INTO polygons(objectid,x,y) VALUES (0,45,35)')
     # transform = [[2., 0.,   5.]
     #              [0., 0.5, -5.]]
     c.execute(
         'INSERT INTO properties(objectid,key,value) VALUES (0,"kx","2.")')
     c.execute(
         'INSERT INTO properties(objectid,key,value) VALUES (0,"ky","0.5")')
     c.execute(
         'INSERT INTO properties(objectid,key,value) VALUES (0,"bx","5")')
     c.execute(
         'INSERT INTO properties(objectid,key,value) VALUES (0,"by","-5.")')
     # The original bbox (x1, y1, width, height).
     self.original_bbox_gt = (20, 60, 5, 20)
     # The original polygon [(x, y)] * N.
     self.original_polygon_gt = [(20, 60), (25, 60), (25, 80), (20, 80)]
Example #2
0
 def setUp(self):
     self.conn = sqlite3.connect(':memory:')
     backendDb.createDb(self.conn)
     c = self.conn.cursor()
     c.execute('INSERT INTO images(imagefile) VALUES ("a/b")')
     c.execute('INSERT INTO objects(imagefile,objectid,x1,y1,width,height) '
               'VALUES ("a/b",0,10,20,30,40)')
Example #3
0
 def setUp(self):
     self.conn = sqlite3.connect(':memory:')
     backendDb.createDb(self.conn)
     self.conn_gt = sqlite3.connect(':memory:')
     backendDb.createDb(self.conn_gt)
     self.args = argparse.Namespace(IoU_thresh=0.5,
                                    where_object_gt='TRUE',
                                    extra_metrics=[])
Example #4
0
def connect(in_db_path=None, out_db_path=None):
    '''
    Connect to a new or existing database.
    Args:
      in_db_path:     If None, create a new database.
                      Otherwise, open a db at in_db_path.
      out_db_path:    If None, NOT commit (lose transactions).
                      Otherwise, back up out_db_path, if exists. Then commit.
                      Same logic applies when in_db_path==out_db_path.
    Returns:
      sqlite3.Connection
    '''

    if in_db_path is not None and not op.exists(in_db_path):
        raise FileNotFoundError('in_db_path specified but does not exist: %s' %
                                in_db_path)

    logging.info('in_db_path:  %s', in_db_path)
    logging.info('out_db_path: %s', out_db_path)

    if in_db_path is not None and not op.exists(in_db_path):
        raise IOError('Input database provided but does not exist: %s' %
                      in_db_path)

    if in_db_path is None and out_db_path is not None:
        # Create a new db and save as out_db_path.
        logging.info('will create database at %s', out_db_path)
        if op.exists(out_db_path):
            raise Exception(
                'Database "-o" exists. Specify it in "-i" too to change it.')
        conn = sqlite3.connect(out_db_path)
        backendDb.createDb(conn)

    elif in_db_path is not None and out_db_path is not None:
        # Load db from in_db_path and save as out_db_path.
        logging.info('will copy existing database from %s to %s.', in_db_path,
                     out_db_path)
        util.copyWithBackup(in_db_path, out_db_path)
        conn = sqlite3.connect(out_db_path)

    elif in_db_path is not None and out_db_path is None:
        # Load db from in_db_path as read-only.
        logging.info(
            'will load existing database from %s, but will not commit.',
            in_db_path)
        conn = backendDb.connect(in_db_path, 'load_to_memory')

    elif in_db_path is None and out_db_path is None:
        # Create a db and discard it in the end.
        logging.info('will create a temporary database in memory.')
        conn = sqlite3.connect(':memory:')  # Create an in-memory database.
        backendDb.createDb(conn)

    else:
        assert False

    return conn
Example #5
0
    def _create_new(self, rootdir):
        ''' Make a new empty database in a new file, and connect to it. '''
        self.temp_db_path = tempfile.NamedTemporaryFile().name
        self.rootdir = rootdir

        # Create an in-memory database.
        self.conn = sqlite3.connect(self.temp_db_path)
        backendDb.createDb(self.conn)
        self.cursor = self.conn.cursor()
        self._update_subcommands()
Example #6
0
 def setUp(self):
     self.conn = sqlite3.connect(':memory:')
     backendDb.createDb(self.conn)
     c = self.conn.cursor()
     c.execute('INSERT INTO images(imagefile) VALUES ("a"), ("b"), ("c")')
     c.execute('INSERT INTO objects(imagefile,objectid,x1,name,score) '
               'VALUES ("a",0,10,"cat",0.1), ("b",1,20,"dog",0.2)')
     c.execute('INSERT INTO properties(objectid,key,value) '
               'VALUES (0,"color","gray"), (1,"breed","poodle")')
     c.execute('INSERT INTO polygons(objectid,x) VALUES (0,25), (1,35)')
     c.execute('INSERT INTO matches(objectid,match) VALUES (0,0), (1,0)')
Example #7
0
 def testTrivial(self):
     ''' Test on the empty database. '''
     self.conn.close()
     conn = sqlite3.connect(':memory:')
     backendDb.createDb(conn)
     c = conn.cursor()
     c.execute('INSERT INTO properties(objectid,key,value) '
               'VALUES (0,"newval","dummy")')
     args = argparse.Namespace(rootdir='.',
                               target_objects_field='name',
                               properties_key='newval')
     dbModify.propertyToObjectsField(c, args)
Example #8
0
 def setUp(self):
     super(Test_evaluateDetectionForClass_Ngt, self).setUp()
     self.conn_gt = sqlite3.connect(':memory:')
     backendDb.createDb(self.conn_gt)
     c_gt = self.conn_gt.cursor()
     self.N = 1000
     for i in range(self.N):
         imagefile = 'image%d' % i
         c_gt.execute('INSERT INTO images(imagefile) VALUES (?)',
                      (imagefile, ))
         c_gt.execute(
             'INSERT INTO objects(imagefile,objectid,x1,y1,width,height,name) '
             'VALUES (?,?,40,20,40,20,"name")', (imagefile, i))
Example #9
0
    def _load(self, in_db_path, rootdir):
        ''' Open an existing database that has the Shuffler schema. '''
        if in_db_path == ':memory:':
            self.temp_db_path = None
            self.conn = sqlite3.connect(':memory:')
            backendDb.createDb(self.conn)
        else:
            self.temp_db_path = tempfile.NamedTemporaryFile().name
            shutil.copyfile(in_db_path, self.temp_db_path)
            self.conn = sqlite3.connect(self.temp_db_path)

        self.rootdir = rootdir
        self.cursor = self.conn.cursor()
        self._update_subcommands()
Example #10
0
    def __init__(self,
                 out_db_file,
                 rootdir='.',
                 media='pictures',
                 image_path=None,
                 mask_path=None,
                 overwrite=False):

        if out_db_file is None:
            raise TypeError('out_db_file is None')

        self.rootdir = rootdir
        self.media = media

        # Image and mask writer.
        self.imwriter = MediaWriter(media_type=media,
                                    rootdir=rootdir,
                                    image_media=image_path,
                                    mask_media=mask_path,
                                    overwrite=overwrite)

        # Maybe create directory for out_db_file.
        out_db_dir = op.dirname(out_db_file)
        if out_db_dir and not op.exists(out_db_dir):
            os.makedirs(out_db_dir)

        # Create and open a database.
        if op.exists(out_db_file) and overwrite:
            os.remove(out_db_file)
            is_new = True
        elif op.exists(out_db_file):  # not overwrite.
            is_new = False
        else:  # not exists.
            is_new = True
        self.conn = sqlite3.connect(out_db_file)
        self.c = self.conn.cursor()
        if is_new:
            createDb(self.conn)
Example #11
0
 def setUp(self):
     self.conn = sqlite3.connect(':memory:')
     backendDb.createDb(self.conn)