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

Feat: deletion of tfs via flatbuffers service #404

Merged
merged 8 commits into from
Aug 7, 2024
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
6 changes: 4 additions & 2 deletions examples/python/gRPC/images/gRPC_fb_addCameraIntrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def add_camintrins_raw(
ciuuid: Optional[str] = "fa2f27e3-7484-48b0-9f21-ec362075baca",
target_proj_uuid: Optional[str] = None,
grpc_channel: Channel = get_gRPC_channel(),
frame_id: str = "map",
) -> Optional[bytearray]:
"""
Creates a example cameraintrinsics object and sends it to a SEEREP server
Expand Down Expand Up @@ -71,7 +72,7 @@ def add_camintrins_raw(

# Create all necessary objects for the query
ts = createTimeStamp(builder, 1, 2)
header = createHeader(builder, ts, "map", target_proj_uuid, ciuuid)
header = createHeader(builder, ts, frame_id, target_proj_uuid, ciuuid)
roi = createRegionOfInterest(builder, 3, 5, 6, 7, True)

distortion_matrix = [4, 5, 6, 7, 8, 9, 10, 11, 12]
Expand Down Expand Up @@ -106,9 +107,10 @@ def add_camintrins(
ciuuid: Optional[str] = "fa2f27e3-7484-48b0-9f21-ec362075baca",
target_proj_uuid: Optional[str] = None,
grpc_channel: Channel = get_gRPC_channel(),
frame_id: str = "map",
) -> Optional[CameraIntrinsics.CameraIntrinsics]:
return CameraIntrinsics.CameraIntrinsics.GetRootAs(
add_camintrins_raw(ciuuid, target_proj_uuid, grpc_channel)
add_camintrins_raw(ciuuid, target_proj_uuid, grpc_channel, frame_id)
)


Expand Down
2 changes: 1 addition & 1 deletion examples/python/gRPC/images/gRPC_fb_addLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def add_label_raw(
builder = flatbuffers.Builder(1024)
query = createQuery(
builder,
projectUuids=[builder.CreateString(target_proj_uuid)],
projectUuids=[target_proj_uuid],
withoutData=True,
)
builder.Finish(query)
Expand Down
8 changes: 2 additions & 6 deletions examples/python/gRPC/images/gRPC_fb_queryImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ def query_images_raw(
"""
image_service_stub = imageService.ImageServiceStub(grpc_channel)

project_uuid_buffer = fbb.CreateString(target_proj_uuid)

query = createQuery(
fbb, *args, projectUuids=[project_uuid_buffer], **kwargs
)
query = createQuery(fbb, *args, projectUuids=[target_proj_uuid], **kwargs)

fbb.Finish(query)
query_buf = fbb.Output()
Expand Down Expand Up @@ -100,7 +96,7 @@ def query_images(
time_max = createTimeStamp(fbb, 1938549273, 0)
time_interval = createTimeInterval(fbb, time_min, time_max)

project_uuids = [fbb.CreateString(project_uuid)]
project_uuids = [project_uuid]

labels = [
create_label(builder=fbb, label=label_str, label_id=i)
Expand Down
110 changes: 104 additions & 6 deletions examples/python/gRPC/images/gRPC_fb_sendImages.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import random
import time
from typing import List, Union
import uuid
from typing import Iterator, List, Tuple, Union
from uuid import uuid4

import flatbuffers
import numpy as np
from grpc import Channel
from quaternion import quaternion
from seerep.fb import ServerResponse
from seerep.fb import (
camera_intrinsics_service_grpc_fb as camera_intrinsic_service,
)
from seerep.fb import image_service_grpc_fb as image_service
from seerep.fb import tf_service_grpc_fb as tf_service
from seerep.util.common import get_gRPC_channel
from seerep.util.fb_helper import (
createCameraIntrinsics,
createHeader,
createImage,
createProject,
createQuaternion,
createRegionOfInterest,
createTimeStamp,
createTransform,
createTransformStamped,
createVector3,
)


Expand All @@ -26,6 +34,8 @@ def send_images(
project_uuid: str,
camera_intrinsic_uuid: str,
image_payloads: List[Union[np.ndarray, str]],
timestamps: Union[List[Tuple[int, int]], None] = None,
frame_id: str = "map",
) -> List[bytes]:
"""
Send images to a SEEREP project
Expand All @@ -38,12 +48,21 @@ def send_images(
image_payloads (List[Union[np.ndarray, str]]): A list of image payloads.
Each payload can be either a numpy array (actual data) or a string
(for a remote stroage URI).
timestamps (Union[List[Tuple[int, int]], None]): A list of timestamps
to attach to the images. Has to be the same size as image_payloads.

Returns:
List[bytes]: A list of bytes representing the serialized
FlatBuffers messages.

"""
if timestamps and len(timestamps) != len(image_payloads):
print(
"Couldn't send images, "
"due to len(image_payloads) != len(timestamps)"
)
return []

fbb = flatbuffers.Builder()
image_service_stub = image_service.ImageServiceStub(grpc_channel)

Expand All @@ -53,8 +72,17 @@ def send_images(
)

fb_msgs = []
for image in image_payloads:
header = createHeader(fbb, timestamp, "map", project_uuid, str(uuid4()))
for idx, image in enumerate(image_payloads):
if timestamps is None:
header = createHeader(
fbb, timestamp, frame_id, project_uuid, str(uuid4())
)
else:
timestamp = createTimeStamp(fbb, *timestamps[idx])
header = createHeader(
fbb, timestamp, frame_id, project_uuid, str(uuid4())
)

fb_image = createImage(
fbb,
header,
Expand Down Expand Up @@ -109,7 +137,9 @@ def create_project(grpc_channel: Channel, project_name: str) -> str:
)


def send_cameraintrinsics(grpc_channel: Channel, project_uuid: str) -> str:
def send_cameraintrinsics(
grpc_channel: Channel, project_uuid: str, frame_id: str = "map"
) -> str:
"""
Create and send a camera intrinsics object to SEEREP.

Expand All @@ -126,7 +156,7 @@ def send_cameraintrinsics(grpc_channel: Channel, project_uuid: str) -> str:
)
timestamp = createTimeStamp(fbb, 0, 0)
ci_uuid = str(uuid4())
header = createHeader(fbb, timestamp, "map", project_uuid, ci_uuid)
header = createHeader(fbb, timestamp, frame_id, project_uuid, ci_uuid)
roi = createRegionOfInterest(fbb, 0, 0, 0, 0, False)

distortion_matrix = [4, 5, 6, 7, 8, 9, 10, 11, 12]
Expand Down Expand Up @@ -154,14 +184,82 @@ def send_cameraintrinsics(grpc_channel: Channel, project_uuid: str) -> str:
return ci_uuid


def send_tfs(
grpc_channel: Channel,
project_uuid: str,
timestamps: List[Tuple[int, int]],
frame_id: str = "map",
child_frame_id: str = "camera",
) -> bytes:
"""
Stream len(timestamps) transforms with the form frame_id -> child_frame_id
to SEEREP.

Args:
grpc_channel (Channel): The grpc_channel to a SEEREP server.
project_uuid (str): The project target for the transformations.
timestamps (List[Tuple[int, int]]): The timestamps of
the transformation.
frame_id (str): The parent frame for the transformations.
child_frame_id (str): The child frame for the transformations.
"""

def tfs_gen() -> Iterator[bytes]:
builder = flatbuffers.Builder(1024)
quat = createQuaternion(builder, quaternion(1, 0, 0, 0))

for idx, (seconds, nanos) in enumerate(timestamps):
timestamp = createTimeStamp(builder, seconds, nanos)

header = createHeader(
builder, timestamp, frame_id, project_uuid, str(uuid.uuid4())
)

translation = createVector3(
builder,
np.array(
[100 * idx**2, 100 * idx**2, 30 * idx], dtype=np.float64
),
)

tf = createTransform(builder, translation, quat)
tfs = createTransformStamped(builder, child_frame_id, header, tf)

builder.Finish(tfs)
yield bytes(builder.Output())

return tf_service.TfServiceStub(grpc_channel).TransferTransformStamped(
tfs_gen()
)


if __name__ == "__main__":
channel = get_gRPC_channel()
project_uuid = create_project(channel, "testproject")
camera_uuid = send_cameraintrinsics(channel, project_uuid)

timestamp_nanos = 1245
nanos_factor = 1e-9

timestamps = [
(t, timestamp_nanos) for t in range(1661336507, 1661336608, 10)
]

img_bufs = send_images(
channel, project_uuid, camera_uuid, generate_image_ressources(10)
channel,
project_uuid,
camera_uuid,
generate_image_ressources(10),
timestamps,
)

tf_resp = send_tfs(channel, project_uuid, timestamps)

if img_bufs is not None and len(img_bufs) > 0:
print("Images sent successfully")

if (
ServerResponse.ServerResponse.GetRootAs(tf_resp).Message().decode()
== "everything stored!"
):
print("Transforms sent successfully")
5 changes: 3 additions & 2 deletions examples/python/gRPC/images/gRPC_pb_sendLabeledImageGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
import time
import uuid
from typing import List, Tuple
from typing import List, Tuple, Union

import flatbuffers
import numpy as np
Expand Down Expand Up @@ -33,7 +33,8 @@


def send_labeled_image_grid(
target_proj_uuid: str = None, grpc_channel: Channel = get_gRPC_channel()
target_proj_uuid: Union[str, None] = None,
grpc_channel: Channel = get_gRPC_channel(),
) -> Tuple[
List[List[image.Image]],
List[Tuple[int, int]],
Expand Down
6 changes: 3 additions & 3 deletions examples/python/gRPC/instances/gRPC_fb_getInstances.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
timeInterval = createTimeInterval(builder, timeMin, timeMax)


projectUuids = [builder.CreateString(projectuuid)]
projectUuids = [projectuuid]

labelStr = ["label1", "label2"]
labels = []
Expand All @@ -63,8 +63,8 @@
)
)

dataUuids = [builder.CreateString("5a0438b8-37cf-412e-8331-a95ef95c1016")]
instanceUuids = [builder.CreateString("3e12e18d-2d53-40bc-a8af-c5cca3c3b248")]
dataUuids = ["5a0438b8-37cf-412e-8331-a95ef95c1016"]
instanceUuids = ["3e12e18d-2d53-40bc-a8af-c5cca3c3b248"]

# 4. Create a query with parameters
# All parameters are optional
Expand Down
8 changes: 3 additions & 5 deletions examples/python/gRPC/point/gRPC_fb_queryPoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_points_raw(
timeMax = createTimeStamp(builder, 1938549273, 0)
timeInterval = createTimeInterval(builder, timeMin, timeMax)

projectUuids = [builder.CreateString(target_proj_uuid)]
projectUuids = [target_proj_uuid]

labelStr = ["label1", "label2"]
labels = []
Expand All @@ -60,10 +60,8 @@ def get_points_raw(
)
)

dataUuids = [builder.CreateString("3e12e18d-2d53-40bc-a8af-c5cca3c3b248")]
instanceUuids = [
builder.CreateString("3e12e18d-2d53-40bc-a8af-c5cca3c3b248")
]
dataUuids = ["3e12e18d-2d53-40bc-a8af-c5cca3c3b248"]
instanceUuids = ["3e12e18d-2d53-40bc-a8af-c5cca3c3b248"]

# 4. Create a query with parameters
# all parameters are optional
Expand Down
2 changes: 1 addition & 1 deletion examples/python/gRPC/pointcloud/gRPC_fb_addLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def add_pc_label_raw(

query = createQuery(
builder,
projectUuids=[builder.CreateString(target_proj_uuid)],
projectUuids=[target_proj_uuid],
withoutData=True,
)
builder.Finish(query)
Expand Down
10 changes: 3 additions & 7 deletions examples/python/gRPC/pointcloud/gRPC_fb_queryPointCloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,10 @@ def query_pcs_raw(
)

# filter for specific data
data_uuids = [
fb_builder.CreateString("3e12e18d-2d53-40bc-a8af-c5cca3c3b248")
]
instance_uuids = [
fb_builder.CreateString("3e12e18d-2d53-40bc-a8af-c5cca3c3b248")
]
data_uuids = ["3e12e18d-2d53-40bc-a8af-c5cca3c3b248"]
instance_uuids = ["3e12e18d-2d53-40bc-a8af-c5cca3c3b248"]

project_uuids = [fb_builder.CreateString(target_proj_uuid)]
project_uuids = [target_proj_uuid]

# set the query parameters according to your needs
query = createQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,10 @@ def query_pcs(
)

# filter for specific data
data_uuids = [
fb_builder.CreateString("3e12e18d-2d53-40bc-a8af-c5cca3c3b248")
]
instance_uuids = [
fb_builder.CreateString("3e12e18d-2d53-40bc-a8af-c5cca3c3b248")
]
data_uuids = ["3e12e18d-2d53-40bc-a8af-c5cca3c3b248"]
instance_uuids = ["3e12e18d-2d53-40bc-a8af-c5cca3c3b248"]

project_uuids = [fb_builder.CreateString(target_proj_uuid)]
project_uuids = [target_proj_uuid]

# set the query parameters according to your needs
query = createQuery(
Expand Down
2 changes: 1 addition & 1 deletion examples/python/gRPC/simulated-data/querySimulatedData.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
timeMax = createTimeStamp(builder, 1654688940, 0)
timeInterval = createTimeInterval(builder, timeMin, timeMax)

projectUuids = [builder.CreateString(projectuuid)]
projectUuids = [projectuuid]
labels = createLabelsWithCategoryVector(
builder, ["ground_truth"], [["Gänseblümchen"]]
)
Expand Down
Loading