def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='File path of Tflite model.',
                        required=True)
    parser.add_argument('--label',
                        help='File path of label file.',
                        required=True)
    args = parser.parse_args()

    labels = dataset_utils.ReadLabelFile(args.label)
    engine = ClassificationEngine(args.model)

    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 30
        _, height, width, _ = engine.get_input_tensor_shape()
        camera.start_preview()
        try:
            stream = io.BytesIO()
            for _ in camera.capture_continuous(stream,
                                               format='rgb',
                                               use_video_port=True,
                                               resize=(width, height)):
                stream.truncate()
                stream.seek(0)
                input_tensor = np.frombuffer(stream.getvalue(), dtype=np.uint8)
                start_ms = time.time()
                results = engine.ClassifyWithInputTensor(input_tensor, top_k=1)
                elapsed_ms = time.time() - start_ms
                if results:
                    camera.annotate_text = '%s %.2f\n%.2fms' % (labels[
                        results[0][0]], results[0][1], elapsed_ms * 1000.0)
        finally:
            camera.stop_preview()
Beispiel #2
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--model', help='File path of Tflite model.', required=True)
  parser.add_argument('--label', help='File path of label file.', required=True)
  args = parser.parse_args()

  labels = dataset_utils.read_label_file(args.label)
  engine = ClassificationEngine(args.model)
  detectionEngine = DetectionEngine('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/models/ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite')
  detectionLabels = dataset_utils.read_label_file('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/models/coco_labels.txt')

  with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.framerate = 30
    _, height, width, _ = engine.get_input_tensor_shape()
    camera.start_preview()
    try:
      stream = io.BytesIO()
      count = 0
      for _ in camera.capture_continuous(
          stream, format='rgb', use_video_port=True, resize=(width, height)):
        stream.truncate()
        stream.seek(0)
        input_tensor = np.frombuffer(stream.getvalue(), dtype=np.uint8)
        print(type(stream.getvalue()))
        image = Image.frombuffer('RGB',(width,height), stream.getvalue())
        draw = ImageDraw.Draw(image)

        with open('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/test_images/' + str(count) + '.png','wb') as f:
            image.save(f)
        start_ms = time.time()
        results = engine.classify_with_input_tensor(input_tensor, top_k=1)
        objects = detectionEngine.detect_with_image(image,threshold=0.1,keep_aspect_ratio=True,relative_coord=False,top_k=3)
        elapsed_ms = time.time() - start_ms
        print('--------------------------')
        for obj in objects:
            if detectionLabels:
                print(detectionLabels[obj.label_id] + ' score = ' + str(obj.score))
            box = obj.bounding_box.flatten().tolist()
            print('box = ', box)
            draw.rectangle(box, outline='red')
            draw.text((box[0],box[1]), detectionLabels[obj.label_id] + " " + str(obj.score)) 
        if not objects:
            print('No objects detected')
        else:
            with open('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/test_images/' + str(count) + '_boxes.png','wb') as f:
                image.save(f)

        count+=1
        #if results:
        #  camera.annotate_text = '%s %.2f\n%.2fms' % (
        #      labels[results[0][0]], results[0][1], elapsed_ms * 1000.0)
    finally:
      camera.stop_preview()
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='File path of Tflite model.',
                        required=True)
    parser.add_argument('--label',
                        help='File path of label file.',
                        required=True)
    args = parser.parse_args()

    labels = dataset_utils.read_label_file(args.label)
    engine = ClassificationEngine(args.model)

    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 30
        camera.hflip = True
        camera.rotation = 90
        _, input_height, input_width, _ = engine.get_input_tensor_shape()

        input_size = (input_width, input_height)

        # Width is rounded up to the nearest multiple of 32,
        # height to the nearest multiple of 16.
        capture_size = (math.ceil(input_width / 32) * 32,
                        math.ceil(input_height / 16) * 16)

        camera.start_preview()
        try:
            stream = io.BytesIO()
            for _ in camera.capture_continuous(stream,
                                               format='rgb',
                                               use_video_port=True,
                                               resize=capture_size):
                stream.truncate()
                stream.seek(0)

                input_tensor = np.frombuffer(stream.getvalue(), dtype=np.uint8)
                if input_size != capture_size:
                    # Crop to input size. Note dimension order (height, width, channels)
                    input_tensor = input_tensor.reshape(
                        (capture_size[1], capture_size[0],
                         3))[0:input_height, 0:input_width, :].ravel()

                start_ms = time.time()
                results = engine.classify_with_input_tensor(input_tensor,
                                                            top_k=1)
                elapsed_ms = time.time() - start_ms
                if results:
                    camera.annotate_text = '%s %.2f\n%.2fms' % (labels[
                        results[0][0]], results[0][1], elapsed_ms * 1000.0)
        finally:
            camera.stop_preview()
Beispiel #4
0
def main():
    default_model_dir = "../all_models"
    default_model = 'mobilenet_v2_1.0_224_quant_edgetpu.tflite'
    default_labels = 'imagenet_labels.txt'
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='.tflite model path',
                        default=os.path.join(default_model_dir, default_model))
    parser.add_argument('--labels',
                        help='label file path',
                        default=os.path.join(default_model_dir,
                                             default_labels))
    parser.add_argument('--top_k',
                        type=int,
                        default=3,
                        help='number of classes with highest score to display')
    parser.add_argument('--threshold',
                        type=float,
                        default=0.1,
                        help='class score threshold')
    args = parser.parse_args()

    print("Loading %s with %s labels." % (args.model, args.labels))
    engine = ClassificationEngine(args.model)
    labels = load_labels(args.labels)

    input_shape = engine.get_input_tensor_shape()
    inference_size = (input_shape[1], input_shape[2])

    # Average fps over last 30 frames.
    fps_counter = common.avg_fps_counter(30)

    def user_callback(input_tensor, src_size, inference_box):
        nonlocal fps_counter
        start_time = time.monotonic()
        results = engine.classify_with_input_tensor(input_tensor,
                                                    threshold=args.threshold,
                                                    top_k=args.top_k)
        end_time = time.monotonic()
        text_lines = [
            'Inference: %.2f ms' % ((end_time - start_time) * 1000),
            'FPS: %d fps' % (round(next(fps_counter))),
        ]
        for index, score in results:
            text_lines.append('score=%.2f: %s' % (score, labels[index]))
        print(' '.join(text_lines))
        return generate_svg(src_size, text_lines)

    result = gstreamer.run_pipeline(user_callback, appsink_size=inference_size)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='File path of Tflite model.',
                        required=True)
    parser.add_argument('--label',
                        help='File path of label file.',
                        required=True)
    parser.add_argument('--image',
                        help='File path of the image to be recognized.',
                        required=True)
    args = parser.parse_args()

    # Prepare labels.
    labels = ReadLabelFile(args.label)
    # Initialize engine.
    engine = ClassificationEngine(args.model)

    # Read input image and convert to tensor
    img = Image.open(args.image)
    img = img.convert('L')
    img = ImageOps.invert(img)
    input_tensor_shape = engine.get_input_tensor_shape()
    if (input_tensor_shape.size != 4 or input_tensor_shape[3] != 1
            or input_tensor_shape[0] != 1):
        raise RuntimeError(
            'Invalid input tensor shape! Expected: [1, height, width, 3]')
    _, height, width, _ = input_tensor_shape
    img = img.resize((width, height), Image.NEAREST)
    input_tensor = numpy.asarray(img).flatten()

    # Run inference.
    for result in engine.ClassifyWithInputTensor(input_tensor=input_tensor,
                                                 threshold=0.1,
                                                 top_k=3):
        print('---------------------------')
        print(labels[result[0]])
        print('Score : ', result[1])

    # for time measurement
    for i in range(5000):
        for result in engine.ClassifyWithInputTensor(input_tensor=input_tensor,
                                                     threshold=0.1,
                                                     top_k=3):
            pass
Beispiel #6
0
    help="name of file containing labels")
  parser.add_argument("-k", "--top_k", default=5, help="top_k")
  parser.add_argument("-t", "--threshold", default=0.0, help="threshold")
  parser.add_argument("-c", "--loop_counts", default=1, help="loop counts")
  parser.add_argument("-d", "--device_path", help="device_path")
  parser.add_argument("-b", "--input_mean", default=127.5, help="input_mean")
  parser.add_argument("-s", "--input_std", default=127.5, help="input standard deviation")
  args = parser.parse_args()

  if args.device_path:
    engine = ClassificationEngine(args.model_file, device_path=args.device_path)
  else:
    engine = ClassificationEngine(args.model_file)
  print("device path:", engine.device_path())

  input_tensor_shape = engine.get_input_tensor_shape()
  if (input_tensor_shape.size != 4 or input_tensor_shape[3] != 3 or
                  input_tensor_shape[0] != 1):
    raise RuntimeError('Invalid input tensor shape! Expected: [1, height, width, 3]')
  _, height, width, _ = input_tensor_shape

  img = Image.open(args.image)
  img = img.resize((width, height))

  input_tensor = np.asarray(img).flatten()

  if floating_model:
    input_tensor = (np.float32(input_tensor) - args.input_mean) / args.input_std

  loop_counts = int(args.loop_counts)
  if (loop_counts > 1):