-
Notifications
You must be signed in to change notification settings - Fork 32
feat: add vision service #639
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
base: main
Are you sure you want to change the base?
Changes from all commits
fb3c314
ba66c3d
ce587c7
2fc9b00
78b1458
a9d8153
80c7536
a86410a
fa82d46
0ace132
6cc972a
19d2bd3
392e508
e924b9a
4e652d2
817b597
b1be15f
4480ec6
e777497
02ea329
d463bd1
e494533
d647ca6
f127631
7474f19
1141779
1ad943d
3913578
4d183b0
63c2363
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include <viam/sdk/common/private/raw_image.hpp> | ||
|
|
||
| #include <viam/sdk/common/utils.hpp> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
| namespace impl { | ||
|
|
||
| Camera::raw_image from_proto(const ::viam::component::camera::v1::Image& proto) { | ||
| Camera::raw_image raw_image; | ||
| std::string img_string = proto.image(); | ||
| const std::vector<unsigned char> bytes(img_string.begin(), img_string.end()); | ||
| raw_image.bytes = bytes; | ||
| raw_image.mime_type = proto.mime_type(); | ||
| raw_image.source_name = proto.source_name(); | ||
| return raw_image; | ||
| } | ||
|
|
||
| void to_proto(const Camera::raw_image& image, ::viam::component::camera::v1::Image* out) { | ||
| const std::string img_string = bytes_to_string(image.bytes); | ||
| out->set_source_name(image.source_name); | ||
| out->set_mime_type(image.mime_type); | ||
| out->set_image(img_string); | ||
| } | ||
|
|
||
| } // namespace impl | ||
| } // namespace sdk | ||
| } // namespace viam |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /// @file common/private/raw_image.hpp | ||
| /// | ||
| /// @brief Proto conversion helpers for Camera::raw_image and Image proto. | ||
| #pragma once | ||
|
|
||
| #include <viam/api/component/camera/v1/camera.pb.h> | ||
| #include <viam/sdk/components/camera.hpp> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
| namespace impl { | ||
|
|
||
| /// @brief Convert a proto Image to Camera::raw_image. | ||
| Camera::raw_image from_proto(const ::viam::component::camera::v1::Image& proto); | ||
|
|
||
| /// @brief Convert a Camera::raw_image to proto Image. | ||
| void to_proto(const Camera::raw_image& image, ::viam::component::camera::v1::Image* out); | ||
|
|
||
| } // namespace impl | ||
| } // namespace sdk | ||
| } // namespace viam | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // Copyright 2024 Viam Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <viam/sdk/services/private/vision.hpp> | ||
|
|
||
| #include <viam/sdk/common/private/repeated_ptr_convert.hpp> | ||
| #include <viam/sdk/common/proto_convert.hpp> | ||
| #include <viam/sdk/spatialmath/geometry.hpp> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
| namespace impl { | ||
| namespace vision { | ||
|
|
||
| namespace vpb = ::viam::service::vision::v1; | ||
|
|
||
| vpb::Detection to_proto(const Vision::detection& d) { | ||
| vpb::Detection out; | ||
| if (d.x_min) { | ||
| out.set_x_min(*d.x_min); | ||
| } | ||
| if (d.y_min) { | ||
| out.set_y_min(*d.y_min); | ||
| } | ||
| if (d.x_max) { | ||
| out.set_x_max(*d.x_max); | ||
| } | ||
| if (d.y_max) { | ||
| out.set_y_max(*d.y_max); | ||
| } | ||
| if (d.x_min_normalized) { | ||
| out.set_x_min_normalized(*d.x_min_normalized); | ||
| } | ||
| if (d.y_min_normalized) { | ||
| out.set_y_min_normalized(*d.y_min_normalized); | ||
| } | ||
| if (d.x_max_normalized) { | ||
| out.set_x_max_normalized(*d.x_max_normalized); | ||
| } | ||
| if (d.y_max_normalized) { | ||
| out.set_y_max_normalized(*d.y_max_normalized); | ||
| } | ||
| out.set_class_name(d.class_name); | ||
| out.set_confidence(d.confidence); | ||
| return out; | ||
| } | ||
|
|
||
| Vision::detection from_proto(const vpb::Detection& p) { | ||
| Vision::detection out; | ||
| if (p.has_x_min()) { | ||
| out.x_min = p.x_min(); | ||
| } | ||
| if (p.has_y_min()) { | ||
| out.y_min = p.y_min(); | ||
| } | ||
| if (p.has_x_max()) { | ||
| out.x_max = p.x_max(); | ||
| } | ||
| if (p.has_y_max()) { | ||
| out.y_max = p.y_max(); | ||
| } | ||
| if (p.has_x_min_normalized()) { | ||
| out.x_min_normalized = p.x_min_normalized(); | ||
| } | ||
| if (p.has_y_min_normalized()) { | ||
| out.y_min_normalized = p.y_min_normalized(); | ||
| } | ||
| if (p.has_x_max_normalized()) { | ||
| out.x_max_normalized = p.x_max_normalized(); | ||
| } | ||
| if (p.has_y_max_normalized()) { | ||
| out.y_max_normalized = p.y_max_normalized(); | ||
| } | ||
| out.class_name = p.class_name(); | ||
| out.confidence = p.confidence(); | ||
| return out; | ||
| } | ||
|
|
||
| vpb::Classification to_proto(const Vision::classification& c) { | ||
| vpb::Classification out; | ||
| out.set_class_name(c.class_name); | ||
| out.set_confidence(c.confidence); | ||
| return out; | ||
| } | ||
|
|
||
| Vision::classification from_proto(const vpb::Classification& p) { | ||
| return Vision::classification{p.class_name(), p.confidence()}; | ||
| } | ||
|
|
||
| void to_proto(const Vision::point_cloud_object& o, ::viam::common::v1::PointCloudObject* out) { | ||
| out->set_point_cloud( | ||
| std::string(reinterpret_cast<const char*>(o.cloud.pc.data()), o.cloud.pc.size())); | ||
| *(out->mutable_geometries()->mutable_geometries()) = impl::to_repeated_field(o.geometries); | ||
| } | ||
|
|
||
| Vision::point_cloud_object from_proto(const ::viam::common::v1::PointCloudObject& p) { | ||
| Vision::point_cloud_object out; | ||
| const auto& bytes = p.point_cloud(); | ||
| out.cloud.pc.assign(reinterpret_cast<const unsigned char*>(bytes.data()), | ||
| reinterpret_cast<const unsigned char*>(bytes.data()) + bytes.size()); | ||
| out.geometries = impl::from_repeated_field(p.geometries().geometries()); | ||
| return out; | ||
| } | ||
|
|
||
| void to_proto(const Vision::properties& props, vpb::GetPropertiesResponse* out) { | ||
| out->set_classifications_supported(props.classifications_supported); | ||
| out->set_detections_supported(props.detections_supported); | ||
| out->set_object_point_clouds_supported(props.object_point_clouds_supported); | ||
| } | ||
|
|
||
| Vision::properties from_proto(const vpb::GetPropertiesResponse& p) { | ||
| return Vision::properties{ | ||
| p.classifications_supported(), p.detections_supported(), p.object_point_clouds_supported()}; | ||
| } | ||
|
|
||
| } // namespace vision | ||
| } // namespace impl | ||
| } // namespace sdk | ||
| } // namespace viam |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright 2024 Viam Inc. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a separate file for these conversions isn't really in line with other patterns we use in the sdk, but see comments below regarding the actual conversion functions |
||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <viam/api/common/v1/common.pb.h> | ||
| #include <viam/api/service/vision/v1/vision.pb.h> | ||
| #include <viam/sdk/services/vision.hpp> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
| namespace impl { | ||
| namespace vision { | ||
|
|
||
| ::viam::service::vision::v1::Detection to_proto(const Vision::detection&); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly detection, classification, and point_cloud_object should be public i think |
||
| Vision::detection from_proto(const ::viam::service::vision::v1::Detection&); | ||
|
|
||
| ::viam::service::vision::v1::Classification to_proto(const Vision::classification&); | ||
| Vision::classification from_proto(const ::viam::service::vision::v1::Classification&); | ||
|
|
||
| void to_proto(const Vision::point_cloud_object&, ::viam::common::v1::PointCloudObject* out); | ||
| Vision::point_cloud_object from_proto(const ::viam::common::v1::PointCloudObject&); | ||
|
|
||
| void to_proto(const Vision::properties&, ::viam::service::vision::v1::GetPropertiesResponse* out); | ||
| Vision::properties from_proto(const ::viam::service::vision::v1::GetPropertiesResponse&); | ||
|
|
||
| } // namespace vision | ||
| } // namespace impl | ||
| } // namespace sdk | ||
| } // namespace viam | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these conversions should probably be public in
camera.hpp, see summary comment