From 2927e84d3d49ff3caa17674380c20078d134cf93 Mon Sep 17 00:00:00 2001 From: haixuanTao Date: Sun, 21 Jul 2024 08:08:30 +0200 Subject: [PATCH] Use dora metadata to reduce complexity as well as enable zero-copy image transfering --- node-hub/opencv-plot/opencv_plot/main.py | 25 +++++++++---------- .../opencv_video_capture/main.py | 16 ++++++------ .../ultralytics-yolo/ultralytics_yolo/main.py | 22 +++++++--------- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/node-hub/opencv-plot/opencv_plot/main.py b/node-hub/opencv-plot/opencv_plot/main.py index bd43469f..7d8af16e 100644 --- a/node-hub/opencv-plot/opencv_plot/main.py +++ b/node-hub/opencv-plot/opencv_plot/main.py @@ -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) diff --git a/node-hub/opencv-video-capture/opencv_video_capture/main.py b/node-hub/opencv-video-capture/opencv_video_capture/main.py index d74408e5..d9dee5e1 100644 --- a/node-hub/opencv-video-capture/opencv_video_capture/main.py +++ b/node-hub/opencv-video-capture/opencv_video_capture/main.py @@ -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"]) diff --git a/node-hub/ultralytics-yolo/ultralytics_yolo/main.py b/node-hub/ultralytics-yolo/ultralytics_yolo/main.py index 98b9a981..23ca85b0 100644 --- a/node-hub/ultralytics-yolo/ultralytics_yolo/main.py +++ b/node-hub/ultralytics-yolo/ultralytics_yolo/main.py @@ -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: