Esempio n. 1
0
def filterObjectsInsideCertainObjects(c, args):
    c.execute('SELECT imagefile FROM images')
    for imagefile, in progressbar(c.fetchall()):

        # Shadow objects.
        c.execute(
            'SELECT * FROM objects WHERE imagefile=? AND (%s)' %
            args.where_shadowing_objects, (imagefile, ))
        shadow_object_entries = c.fetchall()
        logging.info('Found %d shadowing objects.', len(shadow_object_entries))
        # Populate polygons of the shadow objects.
        shadow_object_polygons = []
        for shadow_object_entry in shadow_object_entries:
            shadow_objectid = backendDb.objectField(shadow_object_entry,
                                                    'objectid')
            c.execute('SELECT y,x FROM polygons WHERE objectid=?',
                      (shadow_objectid, ))
            shadow_polygon = c.fetchall()
            shadow_object_polygons.append(shadow_polygon)
        shadow_object_ids_set = set([
            backendDb.objectField(entry, 'objectid')
            for entry in shadow_object_entries
        ])

        # Get all the objects that can be considered.
        c.execute(
            'SELECT * FROM objects WHERE imagefile=? AND (%s)' %
            args.where_objects, (imagefile, ))
        object_entries = c.fetchall()
        logging.info('Total %d objects satisfying the condition.',
                     len(object_entries))

        for object_entry in object_entries:
            objectid = backendDb.objectField(object_entry, 'objectid')
            if objectid in shadow_object_ids_set:
                logging.debug('Object %d is in the shadow set', objectid)
                continue
            c.execute('SELECT AVG(y),AVG(x) FROM polygons WHERE objectid=?',
                      (objectid, ))
            center_yx = c.fetchone()
            # If polygon does not exist, use bbox.
            if center_yx[0] is None:
                roi = utilBoxes.bbox2roi(
                    backendDb.objectField(object_entry, 'bbox'))
                center_yx = (roi[0] + roi[2]) / 2, (roi[1] + roi[3]) / 2
            logging.debug('center_yx: %s', str(center_yx))

            for shadow_object_entry, shadow_polygon in zip(
                    shadow_object_entries, shadow_object_polygons):
                # Get the shadow roi, or polygon if it exists.
                shadow_objectid = backendDb.objectField(
                    object_entry, 'objectid')

                # Check that the center is within the shadow polygon or bbox.
                if len(shadow_polygon) > 0:
                    is_inside = cv2.pointPolygonTest(np.array(shadow_polygon),
                                                     center_yx, False) >= 0
                else:
                    shadow_roi = utilBoxes.bbox2roi(
                        backendDb.objectField(shadow_object_entry, 'bbox'))
                    is_inside = (center_yx[0] > shadow_roi[0]
                                 and center_yx[0] < shadow_roi[2]
                                 and center_yx[1] > shadow_roi[1]
                                 and center_yx[1] < shadow_roi[3])

                if is_inside:
                    backendDb.deleteObject(c, objectid)
                    # We do not need to check other shadow_object_entries.
                    continue
Esempio n. 2
0
 def test_negativeDims(self):
     with self.assertRaises(ValueError):
         utilBoxes.bbox2roi([1, 2, 3, -1])
Esempio n. 3
0
 def test_normal(self):
     self.assertEqual(utilBoxes.bbox2roi([1, 2, 3, 4]), [2, 1, 6, 4])
     self.assertEqual(utilBoxes.bbox2roi((1, 2, 3, 4)), [2, 1, 6, 4])
Esempio n. 4
0
 def test_notNumbers(self):
     with self.assertRaises(TypeError):
         utilBoxes.bbox2roi(['a', 'b', 'c', 'd'])
Esempio n. 5
0
 def test_moreThanFourNumbers(self):
     with self.assertRaises(ValueError):
         utilBoxes.bbox2roi([42, 42, 42, 42, 42])
Esempio n. 6
0
 def test_notSequence(self):
     with self.assertRaises(TypeError):
         utilBoxes.bbox2roi(42)
Esempio n. 7
0
 def test_zeroDims(self):
     self.assertEqual(utilBoxes.bbox2roi([1, 2, 0, 0]), [2, 1, 2, 1])
Esempio n. 8
0
def importBdd(c, args):
    if args.with_display:
        imreader = backendMedia.MediaReader(args.rootdir)

    image_paths = sorted(glob(op.join(args.images_dir, '*.jpg')))
    logging.info('Found %d JPG images in %s' %
                 (len(image_paths), args.images_dir))

    if args.detection_json:
        if not op.exists(args.detection_json):
            raise FileNotFoundError('Annotation file not found at "%s".' %
                                    args.detection_json)
        logging.info(
            'Loading the json with annotations. This may take a few seconds.')
        with open(args.detection_json) as f:
            detections = json.load(f)
            # Dict with image name as the key.
            detections = {d['name']: d for d in detections}

    for image_path in progressbar(image_paths):
        filename = op.splitext(op.basename(image_path))[0]
        logging.debug('Processing image: "%s"' % filename)

        # Add image to the database.
        imheight, imwidth = backendMedia.getPictureSize(image_path)
        imagefile = op.relpath(image_path, args.rootdir)
        c.execute('INSERT INTO images(imagefile,width,height) VALUES (?,?,?)',
                  (imagefile, imwidth, imheight))

        if args.with_display:
            img = imreader.imread(imagefile)

        # Detection annotations.
        if args.detection_json:
            imagename = op.basename(imagefile)
            if imagename not in detections:
                logging.error('Cant find image name "%s" in "%s"',
                              args.detection_json, imagename)
                continue

            detections_for_image = detections[imagename]
            image_properties = detections_for_image['attributes']
            for object_ in detections_for_image['labels']:

                object_bddid = object_['id']
                object_name = object_['category']
                object_properties = {
                    key: value
                    for key, value in object_['attributes'].items()
                    if value != 'none'
                }
                object_properties.update(image_properties)

                # Skip 3d object. TODO: import it to properties.
                if 'box3d' in object_:
                    logging.warning('Will skip 3D object %d.' % object_bddid)
                    continue

                # Get the bbox if exists.
                x1 = y1 = width = height = None
                if 'box2d' in object_:
                    box2d = object_['box2d']
                    x1 = int(float(box2d['x1']))
                    y1 = int(float(box2d['y1']))
                    width = int(float(box2d['x2']) - x1)
                    height = int(float(box2d['y2']) - y1)
                    if args.with_display:
                        roi = utilBoxes.bbox2roi((x1, y1, width, height))
                        util.drawScoredRoi(img, roi, object_name)

                c.execute(
                    'INSERT INTO objects(imagefile,x1,y1,width,height,name) '
                    'VALUES (?,?,?,?,?,?)',
                    (imagefile, x1, y1, width, height, object_name))
                objectid = c.lastrowid

                # Get the polygon if it exists.
                if 'poly2d' in object_:
                    if len(object_['poly2d']) > 1:
                        assert 0, len(object_['poly2d'])
                    polygon = object_['poly2d'][0]
                    polygon_name = None if polygon['closed'] else 'open_loop'
                    for pt in polygon['vertices']:
                        c.execute(
                            'INSERT INTO polygons(objectid,x,y,name) '
                            'VALUES (?,?,?,?)',
                            (objectid, pt[0], pt[1], polygon_name))
                    if args.with_display:
                        util.drawScoredPolygon(img,
                                               [(int(x[0]), int(x[1]))
                                                for x in polygon['vertices']],
                                               object_name)

                # Insert image-level and object-level attributes into
                # "properties" table.
                for key, value in object_properties.items():
                    c.execute(
                        'INSERT INTO properties(objectid,key,value) VALUES (?,?,?)',
                        (objectid, key, value))

        # Segmentation annotations.
        if args.segmentation_dir:
            segmentation_path = op.join(args.segmentation_dir,
                                        '%s_train_id.png' % filename)
            if not op.exists(segmentation_path):
                raise FileNotFoundError('Annotation file not found at "%s".' %
                                        segmentation_path)

            # Add image to the database.
            maskfile = op.relpath(segmentation_path, args.rootdir)
            c.execute('UPDATE images SET maskfile=? WHERE imagefile=?',
                      (maskfile, imagefile))

            if args.with_display:
                mask = imreader.maskread(maskfile)
                img = util.drawMaskAside(img, mask, labelmap=None)

        # Maybe display.
        if args.with_display:
            cv2.imshow('importKitti', img[:, :, ::-1])
            if cv2.waitKey(-1) == 27:
                args.with_display = False
                cv2.destroyWindow('importKitti')