Пример #1
0
def main():
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--input', '-i', dest='input', required=True,
                        help='Input image file.')
    parser.add_argument('--output', '-o', dest='output',
                        help='Output image file with bounding boxes.')
    parser.add_argument('--sparse', '-s', action='store_true', default=False,
                        help='Use sparse tensors.')
    parser.add_argument('--threshold', '-t', type=float, default=0.3,
                        help='Detection probability threshold.')
    args = parser.parse_args()

    with ImageInference(object_detection.model()) as inference:
        image = Image.open(args.input)
        image_center, offset = crop_center(image)

        if args.sparse:
            result = inference.run(image_center,
                                   sparse_configs=object_detection.sparse_configs(args.threshold))
            objects = object_detection.get_objects_sparse(result, offset)
        else:
            result = inference.run(image_center)
            objects = object_detection.get_objects(result, args.threshold, offset)

        for i, obj in enumerate(objects):
            print('Object #%d: %s' % (i, obj))

        if args.output:
            draw = ImageDraw.Draw(image)
            for i, obj in enumerate(objects):
                x, y, width, height = obj.bounding_box
                draw.rectangle((x, y, x + width, y + height), outline='red')
            image.save(args.output)
Пример #2
0
 def test_detection(self):
     with TestImage(self.image_file) as image:
         image_center, offset = crop_center(image)
         with ImageInference(od.model()) as inference:
             if self.sparse:
                 sparse_configs = od.sparse_configs(threshold=self.THRESHOLD)
                 result = inference.run(image_center, sparse_configs=sparse_configs)
                 objects = od.get_objects_sparse(result, offset)
             else:
                 result = inference.run(image_center)
                 objects = od.get_objects(result, self.THRESHOLD, offset)
             self.check(objects)
Пример #3
0
    def write(self, b):
        '''
        Here is where the image data is received and made available at self.output
        '''

        try:
            # b is the numpy array of the image, 3 bytes of color depth
            self.output = np.reshape(
                np.frombuffer(b, dtype=np.uint8),
                (self.args.input_height, self.args.input_width, 3))

            image_center, offset = crop_center(Image.fromarray(self.output))

            if self.args.sparse:
                result = self.inference.run(
                    image_center,
                    sparse_configs=object_detection.sparse_configs(
                        self.args.threshold))
                objects = object_detection.get_objects_sparse(result, offset)
            else:
                result = self.inference.run(image_center)
                objects = object_detection.get_objects(result,
                                                       self.args.threshold,
                                                       offset)

            if self.DEBUG:
                for i, obj in enumerate(objects):
                    x, y, width, height = obj.bounding_box

                    x = x + int(width / 2)
                    y = y + int(height / 2)
                    print(
                        'ImgCap - Object #{}: kind={} score={:.3f} pos(x,y)=({},{})'
                        .format(i, obj.kind, obj.score, x, y))
            if len(objects):
                with self.lock:
                    self.new_obj = True  # indicates a new object is available
                    self.last_objects = deepcopy(objects)
                    self.last_image = np.copy(self.output)

            if self.DEBUG:
                print("ImgCap - Image.shape {}".format(self.output.shape))
                print("ImgCap - Running at {:2.2f} Hz".format(
                    1 / (time.time() - self.prev_time)))

            self.prev_time = time.time()

        except Exception as e:
            print("ImgCap error: {}".format(e))

        finally:
            return len(b)