From 99810783216b5bf5adcda3c8860bac034006acdc Mon Sep 17 00:00:00 2001 From: edgar Date: Thu, 17 Nov 2022 13:08:37 +0100 Subject: [PATCH 1/3] update file reader example with latest farm-ng-amiga --- py/examples/file_reader/README.md | 29 +++----------- py/examples/file_reader/main.py | 48 ++++++++++++++---------- py/examples/file_reader/requirements.txt | 6 +-- 3 files changed, 38 insertions(+), 45 deletions(-) diff --git a/py/examples/file_reader/README.md b/py/examples/file_reader/README.md index ac0dbe61..ffdf4e53 100644 --- a/py/examples/file_reader/README.md +++ b/py/examples/file_reader/README.md @@ -1,4 +1,6 @@ -# Setup +# farm-ng-amiga file read example + +## Setup Create first a virtual environment @@ -7,34 +9,15 @@ python3 -m venv venv source venv/bin/activate ``` -# Install - -Clone the repo - -```bash -git clone https://github.com/farm-ng/amiga-dev-kit.git -``` - -Go to the brain sdk directory and install - -```bash -cd amiga-dev-kit/brain -pip install -e . -``` - -Check the version - -```bash -python -c "import farm_ng; print(farm_ng.__version__)" -``` - -# Run example +## Install ```bash cd examples/file_reader pip install -r requirements.txt ``` +## Run example + Specify the file (download before) ```bash diff --git a/py/examples/file_reader/main.py b/py/examples/file_reader/main.py index d30ba937..69b1807e 100644 --- a/py/examples/file_reader/main.py +++ b/py/examples/file_reader/main.py @@ -2,58 +2,68 @@ # Copyright (c) farm-ng, inc. All rights reserved. import argparse from pathlib import Path +from typing import List import cv2 import numpy as np +from farm_ng.core import event_pb2 from farm_ng.core.events_file_reader import EventsFileReader +from farm_ng.core.uri import uri_pb2 from farm_ng.oak import oak_pb2 def main(file_name: str) -> None: + # create the file reader reader = EventsFileReader(Path(file_name)) assert reader.open() - reader.compute_offsets() # main window to visualize image - uris = reader.uris() - print(uris) + uris: List[uri_pb2.Uri] = reader.uris() - while True: + # choose the Uri stream to seek in file + uri: uri_pb2.Uri = uris[1] + + num_events: int = reader.num_events(uri) + current_event: int = 0 + + while current_event < num_events - 1: # read frame by frame - event, sample = reader.read() - if not event: - break - if event.name != "OakDataSample": - continue + # get the event and offset + event: event_pb2.Event + offset: int + event, offset = reader.get_event(uri, current_event) + + # seek and parse the message + sample: oak_pb2.OakDataSample + sample = reader.read_message(event, offset) frame: oak_pb2.OakSyncFrame = sample.frame - if frame is None: - break # cast image data bytes to numpy and decode # NOTE: explore frame.[rgb, disparity, left, right] - depth = cv2.imdecode(np.frombuffer(frame.disparity.image_data, dtype="uint8"), cv2.IMREAD_GRAYSCALE) + disparity = cv2.imdecode(np.frombuffer(frame.disparity.image_data, dtype="uint8"), cv2.IMREAD_GRAYSCALE) rgb = cv2.imdecode(np.frombuffer(frame.rgb.image_data, dtype="uint8"), cv2.IMREAD_UNCHANGED) # visualize the image - # - depth_color = cv2.applyColorMap(depth * 2, cv2.COLORMAP_HOT) + disparity_color = cv2.applyColorMap(disparity * 2, cv2.COLORMAP_HOT) rgb_window_name = "rgb:" + event.uri.query - depth_window_name = "depth:" + event.uri.query + disparity_window_name = "depth:" + event.uri.query - cv2.namedWindow(depth_window_name, cv2.WINDOW_NORMAL) + # we use opencv for convenience, use kivy, pangolin or you preferred viz tool :) + cv2.namedWindow(disparity_window_name, cv2.WINDOW_NORMAL) cv2.namedWindow(rgb_window_name, cv2.WINDOW_NORMAL) - cv2.imshow(depth_window_name, depth_color) + cv2.imshow(disparity_window_name, disparity_color) cv2.imshow(rgb_window_name, rgb) cv2.waitKey(30) - reader.close() + current_event += 1 + + assert reader.close() if __name__ == "__main__": parser = argparse.ArgumentParser(prog="Event file reader example.") parser.add_argument("--file-name", type=str, required=True, help="Path to the `events.log` file.") args = parser.parse_args() - main(args.file_name) diff --git a/py/examples/file_reader/requirements.txt b/py/examples/file_reader/requirements.txt index 0f75dd66..663032fc 100644 --- a/py/examples/file_reader/requirements.txt +++ b/py/examples/file_reader/requirements.txt @@ -1,3 +1,3 @@ -farm_ng>=0.0.0 -opencv-python==4.6.0.66 -numpy==1.23.2 +farm-ng-amiga +opencv-python +numpy From 5a90dbadeb9543df8fcaa7b193a6c50b152a9cc4 Mon Sep 17 00:00:00 2001 From: Edgar Riba Date: Fri, 18 Nov 2022 08:13:03 +0100 Subject: [PATCH 2/3] Update main.py --- py/examples/file_reader/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/examples/file_reader/main.py b/py/examples/file_reader/main.py index 69b1807e..9cdfd475 100644 --- a/py/examples/file_reader/main.py +++ b/py/examples/file_reader/main.py @@ -64,6 +64,6 @@ def main(file_name: str) -> None: if __name__ == "__main__": parser = argparse.ArgumentParser(prog="Event file reader example.") - parser.add_argument("--file-name", type=str, required=True, help="Path to the `events.log` file.") + parser.add_argument("--file-name", type=str, required=True, help="Path to the `events.bin` file.") args = parser.parse_args() main(args.file_name) From 9325432251fa4c9627be80a20e8a9285f7073aa1 Mon Sep 17 00:00:00 2001 From: Edgar Riba Date: Fri, 18 Nov 2022 15:10:46 +0100 Subject: [PATCH 3/3] Update main.py --- py/examples/file_reader/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/examples/file_reader/main.py b/py/examples/file_reader/main.py index 9cdfd475..1e91a70e 100644 --- a/py/examples/file_reader/main.py +++ b/py/examples/file_reader/main.py @@ -17,7 +17,7 @@ def main(file_name: str) -> None: reader = EventsFileReader(Path(file_name)) assert reader.open() - # main window to visualize image + # get a list with all the existing uri uris: List[uri_pb2.Uri] = reader.uris() # choose the Uri stream to seek in file