Skip to content

Commit

Permalink
Merge pull request #329 from Gold872/async_grab
Browse files Browse the repository at this point in the history
Add async version of VideoCapture.grab()
  • Loading branch information
rainyl authored Jan 30, 2025
2 parents 17fc595 + 0f8fe90 commit 47fec2d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/dartcv/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.3-WIP

* add `VideoCapture.grabAsync`

## 1.1.2

* add more functions in `calib3d`
Expand Down
4 changes: 2 additions & 2 deletions packages/dartcv/lib/src/videoio/videoio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class VideoCapture extends CvStruct<cvg.VideoCapture> {

/// Grabs the next frame from video file or capturing device.
///
/// https://docs.opencv.org/4.x/d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939
void grab() => cvideoio.cv_VideoCapture_grab(ref, ffi.nullptr);
/// https://docs.opencv.org/4.x/d8/dfe/classcv_1_1VideoCapture.html#ae38c2a053d39d6b20c9c649e08ff0146
void grab() => cvRun(() => cvideoio.cv_VideoCapture_grab(ref, ffi.nullptr));

(bool, Mat) read({Mat? m}) {
m ??= Mat.empty();
Expand Down
12 changes: 9 additions & 3 deletions packages/dartcv/lib/src/videoio/videoio_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ extension VideoCaptureAsync on VideoCapture {
);
}

/// Grabs the next frame from video file or capturing device.
///
/// https://docs.opencv.org/4.x/d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939
Future<void> grabAsync() => cvRunAsync0(
(callback) => cvideoio.cv_VideoCapture_grab(ref, callback),
(c) => c.complete(),
);

Future<(bool, Mat)> readAsync({Mat? m}) async {
m ??= Mat.empty();
final p = calloc<ffi.Bool>();
Expand Down Expand Up @@ -196,9 +204,7 @@ extension VideoWriterAsync on VideoWriter {
Future<void> writeAsync(InputArray image) async {
return cvRunAsync0(
(callback) => cvideoio.cv_VideoWriter_write(ref, image.ref, callback),
(c) {
c.complete();
},
(c) => c.complete(),
);
}
}
2 changes: 2 additions & 0 deletions packages/dartcv/test/videoio/videoio_async_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void main() async {
expect(frame.isEmpty, false);
expect(vc.codec, "h264");

await vc.grabAsync();

expect(cv.VideoCapture.toCodec("h264"), closeTo(875967080, 1e-3));
// cv.imwrite("cv.VideoCapture.fromFile.png", frame);
});
Expand Down
2 changes: 2 additions & 0 deletions packages/dartcv/test/videoio/videoio_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void main() async {
expect(success, true);
expect(frame.isEmpty, false);

vc.grab();

expect(vc.getBackendName(), isNotEmpty);

expect(vc.codec, "h264");
Expand Down

0 comments on commit 47fec2d

Please sign in to comment.