我对TypeScript非常陌生,我想在TypeScript文件中使用OpenCV js库。我设定
strict
到
false
要禁用类型检查,目前只需测试这是否可行,但所有OpenCV函数都会返回一个错误:
Property Mat does not exist on type '(cv: any, ...args: any[]) => any'
我的代码示例:
import cv from "../OpenCV/opencv.js";
export class FaceRecognizer {
private sourceMat: any;
private grayMat: any
private faceVector: any;
private faceMat: any;
private videoSource: HTMLVideoElement;
private videoWidth: number;
private videoHeight: number;
constructor(videoSource: HTMLVideoElement) {
this.sourceMat = new cv.Mat(this.videoHeight, this.videoWidth, cv.CV_8UC4);
this.grayMat = new cv.Mat(this.videoHeight, this.videoWidth, cv.CV_8UC1);
this.faceVector = new cv.RectVector();
this.faceMat = new cv.Mat(0, 0, 0);
}
}
编辑:
下面是我如何定义类型的。
我的文件夹结构:
src
tsconfig.json
@types
OpenCV
main.d.ts
package.json
OpenCV
opencv.js
包裹json
{
"name": "types",
"version": "1.0.0",
"typings": "main.d.ts"
}
tsconfig。js
...
"declaration": false,
"typeRoots": [
"node_modules/@types",
"./src/@types"
]
主要的d、 ts
declare namespace cv {
export function Mat(...args: any): any;
export function rotatedRectPoints(...args: any): any;
export function rotatedRectBoundingRect(...args: any): any;
export function rotatedRectBoundingRect2f(...args: any): any;
export function exceptionFromPtr(...args: any): any;
export function minEnclosingCircle(...args: any): any;
export class MatVector {
push_back: any;
resize: any;
size: any;
get: any;
set: any;
}
export class RectVector {
push_back: any;
resize: any;
size: any;
get: any;
set: any;
}
}