def classification_job(classification_model, image_name, num_inferences):
        """Runs classification job."""
        engine = ClassificationEngine(classification_model)
        with open_image(image_name) as image:
            tensor = get_input_tensor(engine, image)

        # Using `classify_with_input_tensor` to exclude image down-scale cost.
        for _ in range(num_inferences):
            engine.classify_with_input_tensor(tensor, top_k=1)
Пример #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()
Пример #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()
Пример #4
0
def run_two_models_one_tpu(classification_model, detection_model, image_name,
                           num_inferences, batch_size):
    """Runs two models ALTERNATIVELY using one Edge TPU.

  It runs classification model `batch_size` times and then switch to run
  detection model `batch_size` time until each model is run `num_inferences`
  times.

  Args:
    classification_model: string, path to classification model
    detection_model: string, path to detection model.
    image_name: string, path to input image.
    num_inferences: int, number of inferences to run for each model.
    batch_size: int, indicates how many inferences to run one model before
      switching to the other one.

  Returns:
    double, wall time it takes to finish the job.
  """
    start_time = time.perf_counter()
    engine_a = ClassificationEngine(classification_model)
    # `engine_b` shares the same Edge TPU as `engine_a`
    engine_b = DetectionEngine(detection_model, engine_a.device_path())
    with open_image(image_name) as image:
        # Resized image for `engine_a`, `engine_b`.
        tensor_a = get_input_tensor(engine_a, image)
        tensor_b = get_input_tensor(engine_b, image)

    num_iterations = (num_inferences + batch_size - 1) // batch_size
    for _ in range(num_iterations):
        # Using `classify_with_input_tensor` and `detect_with_input_tensor` on purpose to
        # exclude image down-scale cost.
        for _ in range(batch_size):
            engine_a.classify_with_input_tensor(tensor_a, top_k=1)
        for _ in range(batch_size):
            engine_b.detect_with_input_tensor(tensor_b, top_k=1)
    return time.perf_counter() - start_time
Пример #5
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
        _, 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.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()