Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update file reader example with latest farm-ng-amiga #31

Merged
merged 4 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions py/examples/file_reader/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Setup
# farm-ng-amiga file read example

## Setup

Create first a virtual environment

Expand All @@ -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
Expand Down
52 changes: 31 additions & 21 deletions py/examples/file_reader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# get a list with all the existing uri
uris: List[uri_pb2.Uri] = reader.uris()

while True:
# choose the Uri stream to seek in file
uri: uri_pb2.Uri = uris[1]
edgarriba marked this conversation as resolved.
Show resolved Hide resolved

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
Hackerman342 marked this conversation as resolved.
Show resolved Hide resolved
# 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)
edgarriba marked this conversation as resolved.
Show resolved Hide resolved
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.")
parser.add_argument("--file-name", type=str, required=True, help="Path to the `events.bin` file.")
args = parser.parse_args()

main(args.file_name)
6 changes: 3 additions & 3 deletions py/examples/file_reader/requirements.txt
Original file line number Diff line number Diff line change
@@ -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