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

async object detect #115

Merged
merged 23 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions ffigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ headers:
- src/imgproc/imgproc.h
- src/imgproc/imgproc_async.h
- src/objdetect/objdetect.h
- src/objdetect/objdetect_async.h
- src/photo/photo.h
- src/stitching/stitching.h
- src/video/video.h
Expand All @@ -62,6 +63,7 @@ headers:
- src/imgproc/imgproc.h
- src/imgproc/imgproc_async.h
- src/objdetect/objdetect.h
- src/objdetect/objdetect_async.h
- src/photo/photo.h
- src/stitching/stitching.h
- src/video/video.h
Expand Down
53 changes: 44 additions & 9 deletions lib/src/core/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ ffi.DynamicLibrary loadNativeLibrary() {
"windows" => "$_libraryName.dll",
"linux" || "android" || "fuchsia" => "lib$_libraryName.so",
"macos" => "lib$_libraryName.dylib",
_ => throw UnsupportedError("Platform ${Platform.operatingSystem} not supported")
_ => throw UnsupportedError(
"Platform ${Platform.operatingSystem} not supported")
};
final libPath = Platform.environment["OPENCV_DART_LIB_PATH"] ?? defaultLibPath;
final libPath =
Platform.environment["OPENCV_DART_LIB_PATH"] ?? defaultLibPath;
return ffi.DynamicLibrary.open(libPath);
}

Expand All @@ -62,7 +64,8 @@ abstract class ICvStruct<T extends ffi.Struct> extends CvObject<T> {
T get ref;
}

abstract class CvStruct<T extends ffi.Struct> extends ICvStruct<T> with EquatableMixin {
abstract class CvStruct<T extends ffi.Struct> extends ICvStruct<T>
with EquatableMixin {
CvStruct.fromPointer(super.ptr) : super.fromPointer();
}

Expand Down Expand Up @@ -137,7 +140,8 @@ Future<T> cvRunAsync2<T>(

Future<T> cvRunAsync3<T>(
ffi.Pointer<cvg.CvStatus> Function(cvg.CvCallback_3 callback) func,
void Function(Completer<T> completer, VoidPtr p, VoidPtr p1, VoidPtr p2) onComplete,
void Function(Completer<T> completer, VoidPtr p, VoidPtr p1, VoidPtr p2)
onComplete,
) {
final completer = Completer<T>();
late final NativeCallable<cvg.CvCallback_3Function> ccallback;
Expand All @@ -151,9 +155,33 @@ Future<T> cvRunAsync3<T>(
return completer.future;
}

Future<T> cvRunAsync4<T>(
ffi.Pointer<cvg.CvStatus> Function(cvg.CvCallback_4 callback) func,
void Function(
Completer<T> completer,
VoidPtr p,
VoidPtr p1,
VoidPtr p2,
VoidPtr p3,
) onComplete,
) {
final completer = Completer<T>();
late final NativeCallable<cvg.CvCallback_4Function> ccallback;
void onResponse(VoidPtr p, VoidPtr p1, VoidPtr p2, VoidPtr p3) {
onComplete(completer, p, p1, p2, p3);
ccallback.close();
}

ccallback = ffi.NativeCallable.listener(onResponse);
throwIfFailed(func(ccallback.nativeFunction));
return completer.future;
}

Future<T> cvRunAsync5<T>(
ffi.Pointer<cvg.CvStatus> Function(cvg.CvCallback_5 callback) func,
void Function(Completer<T> completer, VoidPtr p, VoidPtr p1, VoidPtr p2, VoidPtr p3, VoidPtr p4) onComplete,
void Function(Completer<T> completer, VoidPtr p, VoidPtr p1, VoidPtr p2,
VoidPtr p3, VoidPtr p4)
onComplete,
) {
final completer = Completer<T>();
late final NativeCallable<cvg.CvCallback_5Function> ccallback;
Expand All @@ -168,11 +196,17 @@ Future<T> cvRunAsync5<T>(
}

// Completers for async
void matCompleter(Completer<Mat> completer, VoidPtr p) => completer.complete(Mat.fromPointer(p.cast()));
void matCompleter(Completer<Mat> completer, VoidPtr p) =>
completer.complete(Mat.fromPointer(p.cast()));
void matCompleter2(Completer<(Mat, Mat)> completer, VoidPtr p, VoidPtr p1) =>
completer.complete((Mat.fromPointer(p.cast()), Mat.fromPointer(p1.cast())));
void matCompleter3(Completer<(Mat, Mat, Mat)> completer, VoidPtr p, VoidPtr p1, VoidPtr p2) =>
completer.complete((Mat.fromPointer(p.cast()), Mat.fromPointer(p1.cast()), Mat.fromPointer(p2.cast())));
void matCompleter3(Completer<(Mat, Mat, Mat)> completer, VoidPtr p, VoidPtr p1,
VoidPtr p2) =>
completer.complete((
Mat.fromPointer(p.cast()),
Mat.fromPointer(p1.cast()),
Mat.fromPointer(p2.cast())
));

// Arena wrapper
R cvRunArena<R>(
Expand Down Expand Up @@ -200,7 +234,8 @@ R cvRunArena<R>(
typedef NativeFinalizerFunctionT<T extends ffi.NativeType>
= ffi.Pointer<ffi.NativeFunction<ffi.Void Function(T token)>>;

ffi.NativeFinalizer OcvFinalizer<T extends ffi.NativeType>(NativeFinalizerFunctionT<T> func) =>
ffi.NativeFinalizer OcvFinalizer<T extends ffi.NativeType>(
NativeFinalizerFunctionT<T> func) =>
ffi.NativeFinalizer(func.cast<ffi.NativeFinalizerFunction>());

// native types
Expand Down
Loading