-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: marshal ClassificationResult (#1090)
- Loading branch information
Showing
9 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeClassificationResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2023 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Mediapipe | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal readonly struct NativeClassifications | ||
{ | ||
private readonly IntPtr _categories; | ||
public readonly uint categoriesCount; | ||
public readonly int headIndex; | ||
private readonly IntPtr _headName; | ||
|
||
public ReadOnlySpan<NativeCategory> categories | ||
{ | ||
get | ||
{ | ||
unsafe | ||
{ | ||
return new ReadOnlySpan<NativeCategory>((NativeCategory*)_categories, (int)categoriesCount); | ||
} | ||
} | ||
} | ||
|
||
public string headName => Marshal.PtrToStringAnsi(_headName); | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal readonly struct NativeClassificationResult | ||
{ | ||
private readonly IntPtr _classifications; | ||
public readonly uint classificationsCount; | ||
public readonly long timestampMs; | ||
public readonly bool hasTimestampMs; | ||
|
||
public ReadOnlySpan<NativeClassifications> classifications | ||
{ | ||
get | ||
{ | ||
unsafe | ||
{ | ||
return new ReadOnlySpan<NativeClassifications>((NativeClassifications*)_classifications, (int)classificationsCount); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
UnsafeNativeMethods.mp_tasks_c_components_containers_CppCloseClassificationResult(this); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...s/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeClassificationResult.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
mediapipe_api/tasks/c/components/containers/classification_result.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "mediapipe_api/tasks/c/components/containers/classification_result.h" | ||
|
||
MpReturnCode mp_Packet__GetClassificationsVector(mediapipe::Packet* packet, ClassificationResult* value_out) { | ||
TRY_ALL | ||
// get std::vector<ClassificationList> and convert it to ClassificationResult | ||
auto proto_vec = packet->Get<std::vector<mediapipe::ClassificationList>>(); | ||
auto vec_size = proto_vec.size(); | ||
|
||
auto classifications_vec = std::vector<mediapipe::tasks::components::containers::Classifications>(vec_size); | ||
for (auto i = 0; i < vec_size; ++i) { | ||
auto classifications = mediapipe::tasks::components::containers::ConvertToClassifications(proto_vec[i]); | ||
classifications_vec.push_back(classifications); | ||
} | ||
|
||
mediapipe::tasks::components::containers::ClassificationResult result; | ||
result.classifications = std::move(classifications_vec); | ||
|
||
mediapipe::tasks::c::components::containers::CppConvertToClassificationResult(result, value_out); | ||
RETURN_CODE(MpReturnCode::Success); | ||
CATCH_ALL | ||
} | ||
|
||
void mp_tasks_c_components_containers_CppCloseClassificationResult(ClassificationResult data) { | ||
mediapipe::tasks::c::components::containers::CppCloseClassificationResult(&data); | ||
} |
23 changes: 23 additions & 0 deletions
23
mediapipe_api/tasks/c/components/containers/classification_result.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2023 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
#ifndef MEDIAPIPE_API_TASKS_C_COMPONENTS_CONTAINERS_CLASSIFICATION_RESULT_H_ | ||
#define MEDIAPIPE_API_TASKS_C_COMPONENTS_CONTAINERS_CLASSIFICATION_RESULT_H_ | ||
|
||
#include "mediapipe/framework/packet.h" | ||
#include "mediapipe/tasks/c/components/containers/classification_result.h" | ||
#include "mediapipe/tasks/c/components/containers/classification_result_converter.h" | ||
#include "mediapipe/tasks/cc/components/containers/classification_result.h" | ||
#include "mediapipe_api/common.h" | ||
|
||
extern "C" { | ||
|
||
MP_CAPI(MpReturnCode) mp_Packet__GetClassificationsVector(mediapipe::Packet* packet, ClassificationResult* value_out); | ||
MP_CAPI(void) mp_tasks_c_components_containers_CppCloseClassificationResult(ClassificationResult data); | ||
|
||
} // extern "C" | ||
|
||
#endif // MEDIAPIPE_API_TASKS_C_COMPONENTS_CONTAINERS_CLASSIFICATION_RESULT_H_ |