Browse Source

Use dora metadata to reduce complexity as well as enable zero-copy image transfering

tags/v0.3.6-rc0
haixuanTao 1 year ago
parent
commit
2927e84d3d
3 changed files with 29 additions and 34 deletions
  1. +12
    -13
      node-hub/opencv-plot/opencv_plot/main.py
  2. +8
    -8
      node-hub/opencv-video-capture/opencv_video_capture/main.py
  3. +9
    -13
      node-hub/ultralytics-yolo/ultralytics_yolo/main.py

+ 12
- 13
node-hub/opencv-plot/opencv_plot/main.py View File

@@ -1,6 +1,6 @@
import os
import argparse
from cv2 import cv2 # Importing cv2 this way remove error warnings``
import cv2

import numpy as np
import pyarrow as pa
@@ -129,25 +129,24 @@ def main():
event_id = event["id"]

if event_id == "image":
arrow_image = event["value"][0]
storage = event["value"]

metadata = event["metadata"]
encoding = metadata["encoding"]
width = metadata["width"]
height = metadata["height"]

encoding = arrow_image["encoding"].as_py()
if encoding == "bgr8":
channels = 3
storage_type = np.uint8
else:
raise RuntimeError(f"Unsupported image encoding: {encoding}")

image = {
"width": np.uint32(arrow_image["width"].as_py()),
"height": np.uint32(arrow_image["height"].as_py()),
"encoding": encoding,
"channels": channels,
"data": arrow_image["data"].values.to_numpy().astype(storage_type),
}

plot.frame = np.reshape(
image["data"], (image["height"], image["width"], image["channels"])
plot.frame = (
storage.to_numpy()
.astype(storage_type)
.reshape((height, width, channels))
.copy() # Copy So that we can add annotation on the image
)

plot_frame(plot)


+ 8
- 8
node-hub/opencv-video-capture/opencv_video_capture/main.py View File

@@ -1,6 +1,6 @@
import os
import argparse
from cv2 import cv2 # Importing cv2 this way remove error warnings``
import cv2

import numpy as np
import pyarrow as pa
@@ -105,14 +105,14 @@ def main():
if image_width is not None and image_height is not None:
frame = cv2.resize(frame, (image_width, image_height))

image = {
"width": np.uint32(frame.shape[1]),
"height": np.uint32(frame.shape[0]),
"encoding": "bgr8",
"data": frame.ravel(),
}
storage = pa.array(frame.ravel())

node.send_output("image", pa.array([image]), event["metadata"])
metadata = event["metadata"]
metadata["width"] = int(frame.shape[1])
metadata["height"] = int(frame.shape[0])
metadata["encoding"] = "bgr8"

node.send_output("image", storage, metadata)

elif event_type == "ERROR":
raise RuntimeError(event["error"])


+ 9
- 13
node-hub/ultralytics-yolo/ultralytics_yolo/main.py View File

@@ -45,8 +45,11 @@ def main():
event_id = event["id"]

if event_id == "image":
arrow_image = event["value"][0]
encoding = arrow_image["encoding"].as_py()
storage = event["value"]
metadata = event["metadata"]
encoding = metadata["encoding"]
width = metadata["width"]
height = metadata["height"]

if encoding == "bgr8":
channels = 3
@@ -54,18 +57,11 @@ def main():
else:
raise RuntimeError(f"Unsupported image encoding: {encoding}")

image = {
"width": np.uint32(arrow_image["width"].as_py()),
"height": np.uint32(arrow_image["height"].as_py()),
"encoding": encoding,
"channels": channels,
"data": arrow_image["data"].values.to_numpy().astype(storage_type),
}

frame = image["data"].reshape(
(image["height"], image["width"], image["channels"])
frame = (
storage.to_numpy()
.astype(storage_type)
.reshape((height, width, channels))
)

if encoding == "bgr8":
frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
else:


Loading…
Cancel
Save