Skip to content

Commit

Permalink
Merge pull request #157 from la-we/allow-passing-cap-to-videocapture
Browse files Browse the repository at this point in the history
Allow passing cap to VideoCapture constructor
  • Loading branch information
UrielCh authored Sep 13, 2024
2 parents b1bb59b + 0ed8c75 commit da02bd9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
18 changes: 16 additions & 2 deletions cc/io/VideoCapture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,25 @@ NAN_METHOD(VideoCapture::New) {
FF::TryCatch tryCatch("VideoCapture::New");
FF_ASSERT_CONSTRUCT_CALL();
VideoCapture* self = new VideoCapture();

uint32_t apiPreference = cv::CAP_ANY;
if (info.Length() >= 2) {
if (info[1]->IsUint32()) {
apiPreference = info[1]->ToUint32(Nan::GetCurrentContext()).ToLocalChecked()->Value();
} else {
return tryCatch.throwError("Support for 'params' not implemented");
}
}

if (info.Length() == 3) {
return tryCatch.throwError("Support for 'params' not implemented");
}

if (info[0]->IsString()) {
self->path = FF::StringConverter::unwrapUnchecked(info[0]);
self->self.open(self->path);
self->self.open(self->path, apiPreference);
} else if (info[0]->IsUint32()) {
self->self.open(info[0]->ToUint32(Nan::GetCurrentContext()).ToLocalChecked()->Value());
self->self.open(info[0]->ToUint32(Nan::GetCurrentContext()).ToLocalChecked()->Value(), apiPreference);
} else {
return tryCatch.throwError("expected arg 0 to be path or device port");
}
Expand Down
6 changes: 6 additions & 0 deletions test/tests/io/VideoCapture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ if (toTest.io && !process.env.DOCKER_BUILD && !process.env.BINDINGS_DEBUG) {
});
});

describe('constructor with specified caps', () => {
it(`can be opened from valid video file ${path.resolve(getTestVideoPath())}`, () => {
expect(() => new cv.VideoCapture(getTestVideoPath(), cv.CAP_ANY)).to.not.throw();
});
});

describe(`read cap ${getTestVideoPath()}`, () => {
let cap: VideoCapture | undefined;
before(() => {
Expand Down
2 changes: 1 addition & 1 deletion typings/VideoCapture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mat } from './Mat.d';

export class VideoCapture {
constructor(filePathOrdevicePort: string | number);
constructor(filePathOrdevicePort: string | number, apiPreference?: number);
get(property: number): number;
read(): Mat;
readAsync(): Promise<Mat>;
Expand Down

0 comments on commit da02bd9

Please sign in to comment.