增加VideoCapture
This commit is contained in:
@ -14,9 +14,11 @@ export function getAddon() {
|
||||
return require(config.ADDON_PATH);
|
||||
}
|
||||
|
||||
console.log(getAddon())
|
||||
|
||||
export function getConfig<N extends keyof AddonConfig>(name: N): AddonConfig[N] { return config[name]; }
|
||||
export function setConfig<N extends keyof AddonConfig>(name: N, value: AddonConfig[N]) { config[name] = value; }
|
||||
|
||||
export function CVMat() { return getAddon().Mat; }
|
||||
|
||||
export function CVVideoCapture() { return getAddon().VideoCapture; }
|
||||
export function CVUtil() { return getAddon().util; }
|
||||
|
@ -1,10 +1,14 @@
|
||||
export { getConfig as C } from "./addon";
|
||||
import { Mat } from "./mat";
|
||||
import { VideoCapture } from "./videocapture";
|
||||
|
||||
export type TypedArray = Uint8Array | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array
|
||||
|
||||
export function FromCV(mat: any): Mat { return new (Mat as any)(mat); }
|
||||
export function __Mat(mat: any): Mat { return new (Mat as any)(mat); }
|
||||
export function __VideoCapture(cap: any): VideoCapture { return new (VideoCapture as any)(cap); }
|
||||
|
||||
export function M(mat: Mat) { return (mat as any).__mat__; }
|
||||
export function VC(cap: VideoCapture) { return (cap as any).__cap__; }
|
||||
|
||||
export function resolveArgs<R extends Array<any>>(args: any[], checker: boolean | (() => boolean)): R {
|
||||
if (typeof checker === "function") checker = checker();
|
||||
|
111
src/cv/consts.ts
111
src/cv/consts.ts
@ -164,3 +164,114 @@ export const MARKER_DIAMOND = 3;
|
||||
export const MARKER_SQUARE = 4;
|
||||
export const MARKER_TRIANGLE_UP = 5;
|
||||
export const MARKER_TRIANGLE_DOWN = 6;
|
||||
|
||||
|
||||
export const CAP_ANY = 0;
|
||||
export const CAP_VFW = 200;
|
||||
export const CAP_V4L = 200;
|
||||
export const CAP_V4L2 = CAP_V4L;
|
||||
export const CAP_FIREWIRE = 300;
|
||||
export const CAP_FIREWARE = CAP_FIREWIRE;
|
||||
export const CAP_IEEE1394 = CAP_FIREWIRE;
|
||||
export const CAP_DC1394 = CAP_FIREWIRE;
|
||||
export const CAP_CMU1394 = CAP_FIREWIRE;
|
||||
export const CAP_QT = 500;
|
||||
export const CAP_UNICAP = 600;
|
||||
export const CAP_DSHOW = 700;
|
||||
export const CAP_PVAPI = 800;
|
||||
export const CAP_OPENNI = 900;
|
||||
export const CAP_OPENNI_ASUS = 910;
|
||||
export const CAP_ANDROID = 1000;
|
||||
export const CAP_XIAPI = 1100;
|
||||
export const CAP_AVFOUNDATION = 1200;
|
||||
export const CAP_GIGANETIX = 1300;
|
||||
export const CAP_MSMF = 1400;
|
||||
export const CAP_WINRT = 1410;
|
||||
export const CAP_INTELPERC = 1500;
|
||||
export const CAP_REALSENSE = 1500;
|
||||
export const CAP_OPENNI2 = 1600;
|
||||
export const CAP_OPENNI2_ASUS = 1610;
|
||||
export const CAP_OPENNI2_ASTRA = 1620;
|
||||
export const CAP_GPHOTO2 = 1700;
|
||||
export const CAP_GSTREAMER = 1800;
|
||||
export const CAP_FFMPEG = 1900;
|
||||
export const CAP_IMAGES = 2000;
|
||||
export const CAP_ARAVIS = 2100;
|
||||
export const CAP_OPENCV_MJPEG = 2200;
|
||||
export const CAP_INTEL_MFX = 2300;
|
||||
export const CAP_XINE = 2400;
|
||||
export const CAP_UEYE = 2500;
|
||||
export const CAP_OBSENSOR = 2600;
|
||||
|
||||
export const CAP_PROP_POS_MSEC = 0;
|
||||
export const CAP_PROP_POS_FRAMES = 1;
|
||||
export const CAP_PROP_POS_AVI_RATIO = 2;
|
||||
export const CAP_PROP_FRAME_WIDTH = 3;
|
||||
export const CAP_PROP_FRAME_HEIGHT = 4;
|
||||
export const CAP_PROP_FPS = 5;
|
||||
export const CAP_PROP_FOURCC = 6;
|
||||
export const CAP_PROP_FRAME_COUNT = 7;
|
||||
export const CAP_PROP_FORMAT = 8;
|
||||
export const CAP_PROP_MODE = 9;
|
||||
export const CAP_PROP_BRIGHTNESS = 10;
|
||||
export const CAP_PROP_CONTRAST = 11;
|
||||
export const CAP_PROP_SATURATION = 12;
|
||||
export const CAP_PROP_HUE = 13;
|
||||
export const CAP_PROP_GAIN = 14;
|
||||
export const CAP_PROP_EXPOSURE = 15;
|
||||
export const CAP_PROP_CONVERT_RGB = 16;
|
||||
export const CAP_PROP_WHITE_BALANCE_BLUE_U = 17;
|
||||
export const CAP_PROP_RECTIFICATION = 18;
|
||||
export const CAP_PROP_MONOCHROME = 1;
|
||||
export const CAP_PROP_SHARPNESS = 2;
|
||||
export const CAP_PROP_AUTO_EXPOSURE = 21;
|
||||
export const CAP_PROP_GAMMA = 2;
|
||||
export const CAP_PROP_TEMPERATURE = 2;
|
||||
export const CAP_PROP_TRIGGER = 2;
|
||||
export const CAP_PROP_TRIGGER_DELAY = 2;
|
||||
export const CAP_PROP_WHITE_BALANCE_RED_V = 2;
|
||||
export const CAP_PROP_ZOOM = 2;
|
||||
export const CAP_PROP_FOCUS = 2;
|
||||
export const CAP_PROP_GUID = 2;
|
||||
export const CAP_PROP_ISO_SPEED = 3;
|
||||
export const CAP_PROP_BACKLIGHT = 3;
|
||||
export const CAP_PROP_PAN = 3;
|
||||
export const CAP_PROP_TILT = 3;
|
||||
export const CAP_PROP_ROLL = 3;
|
||||
export const CAP_PROP_IRIS = 3;
|
||||
export const CAP_PROP_SETTINGS = 37;
|
||||
export const CAP_PROP_BUFFERSIZE = 3;
|
||||
export const CAP_PROP_AUTOFOCUS = 3;
|
||||
export const CAP_PROP_SAR_NUM = 40;
|
||||
export const CAP_PROP_SAR_DEN = 41;
|
||||
export const CAP_PROP_BACKEND = 42;
|
||||
export const CAP_PROP_CHANNEL = 43;
|
||||
export const CAP_PROP_AUTO_WB = 44;
|
||||
export const CAP_PROP_WB_TEMPERATURE = 45;
|
||||
export const CAP_PROP_CODEC_PIXEL_FORMAT = 46;
|
||||
export const CAP_PROP_BITRATE = 47;
|
||||
export const CAP_PROP_ORIENTATION_META = 48;
|
||||
export const CAP_PROP_ORIENTATION_AUTO = 49;
|
||||
export const CAP_PROP_HW_ACCELERATION = 50;
|
||||
export const CAP_PROP_HW_DEVICE = 51;
|
||||
export const CAP_PROP_HW_ACCELERATION_USE_OPENCL = 52;
|
||||
export const CAP_PROP_OPEN_TIMEOUT_MSEC = 53;
|
||||
export const CAP_PROP_READ_TIMEOUT_MSEC = 54;
|
||||
export const CAP_PROP_STREAM_OPEN_TIME_USEC = 55;
|
||||
export const CAP_PROP_VIDEO_TOTAL_CHANNELS = 5;
|
||||
export const CAP_PROP_VIDEO_STREAM = 5;
|
||||
export const CAP_PROP_AUDIO_STREAM = 5;
|
||||
export const CAP_PROP_AUDIO_POS = 5;
|
||||
export const CAP_PROP_AUDIO_SHIFT_NSEC = 6;
|
||||
export const CAP_PROP_AUDIO_DATA_DEPTH = 6;
|
||||
export const CAP_PROP_AUDIO_SAMPLES_PER_SECOND = 6;
|
||||
export const CAP_PROP_AUDIO_BASE_INDEX = 6;
|
||||
export const CAP_PROP_AUDIO_TOTAL_CHANNELS = 6;
|
||||
export const CAP_PROP_AUDIO_TOTAL_STREAMS = 6;
|
||||
export const CAP_PROP_AUDIO_SYNCHRONIZE = 6;
|
||||
export const CAP_PROP_LRF_HAS_KEY_FRAME = 6;
|
||||
export const CAP_PROP_CODEC_EXTRADATA_INDEX = 6;
|
||||
export const CAP_PROP_FRAME_TYPE = 6;
|
||||
export const CAP_PROP_N_THREADS = 7;
|
||||
export const CAP_PROP_PTS = 7;
|
||||
export const CAP_PROP_DTS_DELAY = 7;
|
||||
|
@ -1,6 +1,7 @@
|
||||
export { setConfig as config } from "./addon";
|
||||
export { downloadAddon } from "./download";
|
||||
export * from "./mat";
|
||||
export * from "./videocapture";
|
||||
export * from "./consts";
|
||||
export * from "./proc";
|
||||
export * from "./draw";
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { CVMat } from "./addon";
|
||||
import { FromCV, M, TypedArray } from "./common";
|
||||
import { __Mat, M, TypedArray } from "./common";
|
||||
import { IMREAD_COLOR_BGR } from "./consts";
|
||||
|
||||
|
||||
@ -40,18 +40,18 @@ export class Mat {
|
||||
else return this.copyTo(this.clone()).data;
|
||||
}
|
||||
|
||||
public clone() { return FromCV(this.#mat.Clone()); }
|
||||
public clone() { return __Mat(this.#mat.Clone()); }
|
||||
public copyTo(mat: Mat) { return this.#mat.CopyTo(M(mat)), mat; }
|
||||
|
||||
public row(y: number) { return FromCV(this.#mat.Row(y)); }
|
||||
public col(x: number) { return FromCV(this.#mat.Col(x)); }
|
||||
public rowRange(start: number, end: number) { return FromCV(this.#mat.RowRange(start, end)); }
|
||||
public colRange(start: number, end: number) { return FromCV(this.#mat.ColRange(start, end)); }
|
||||
public diag(d: number) { return FromCV(this.#mat.Diag(d)); }
|
||||
public row(y: number) { return __Mat(this.#mat.Row(y)); }
|
||||
public col(x: number) { return __Mat(this.#mat.Col(x)); }
|
||||
public rowRange(start: number, end: number) { return __Mat(this.#mat.RowRange(start, end)); }
|
||||
public colRange(start: number, end: number) { return __Mat(this.#mat.ColRange(start, end)); }
|
||||
public diag(d: number) { return __Mat(this.#mat.Diag(d)); }
|
||||
};
|
||||
|
||||
export function imread(filename: string, flag?: number) { return FromCV(CVMat().ImRead(filename, flag ?? IMREAD_COLOR_BGR)); }
|
||||
export function imdecode(data: Uint8Array, flag?: number) { return FromCV(CVMat().ImDecode(data, flag ?? IMREAD_COLOR_BGR)); }
|
||||
export function imread(filename: string, flag?: number) { return __Mat(CVMat().ImRead(filename, flag ?? IMREAD_COLOR_BGR)); }
|
||||
export function imdecode(data: Uint8Array, flag?: number) { return __Mat(CVMat().ImDecode(data, flag ?? IMREAD_COLOR_BGR)); }
|
||||
export function imwrite(filename: string, mat: Mat, params?: number[]): boolean { return CVMat().ImWrite(M(mat), filename, params); }
|
||||
export function imencode(ext: string, mat: Mat, params?: number[]): Uint8Array | null {
|
||||
const res = CVMat().ImEncode(M(mat), ext, params);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Mat } from "./mat";
|
||||
import { FromCV, M, resolveArgs } from "./common";
|
||||
import { __Mat, M, resolveArgs } from "./common";
|
||||
import { BORDER_CONSTANT, BORDER_DEFAULT, INTER_LINEAR } from "./consts";
|
||||
import { CVMat } from "./addon";
|
||||
|
||||
@ -9,14 +9,14 @@ export function crop(src: Mat, dst: Mat, range: CropRange): Mat;
|
||||
export function crop(...args: any[]) {
|
||||
let [src, dst, range] = resolveArgs<[src: Mat, dst: Mat, range: CropRange]>(args, args[1] instanceof Mat);
|
||||
if (!(range instanceof Array)) range = [{ start: range.y, end: range.y + range.height }, { start: range.x, end: range.x + range.width }];
|
||||
return FromCV(CVMat().Crop(M(src), M(dst), range));
|
||||
return __Mat(CVMat().Crop(M(src), M(dst), range));
|
||||
}
|
||||
|
||||
export function resize(src: Mat, width: number, height: number, fx?: number, fy?: number, interpolation?: number): Mat;
|
||||
export function resize(src: Mat, dst: Mat, width: number, height: number, fx?: number, fy?: number, interpolation?: number): Mat;
|
||||
export function resize(...args: any[]) {
|
||||
const [src, dst, width, height, fx, fy, interpolation] = resolveArgs<[src: Mat, dst: Mat, width: number, height: number, fx?: number, fy?: number, interpolation?: number]>(args, args[1] instanceof Mat);
|
||||
return FromCV(CVMat().Resize(M(src), M(dst), width, height, fx ?? 0, fy ?? 0, interpolation ?? INTER_LINEAR));
|
||||
return __Mat(CVMat().Resize(M(src), M(dst), width, height, fx ?? 0, fy ?? 0, interpolation ?? INTER_LINEAR));
|
||||
}
|
||||
|
||||
export function blur(src: Mat, kernelWidth: number, kernelHeight: number, anchorX?: number, anchorY?: number, borderType?: number): Mat
|
||||
@ -24,14 +24,14 @@ export function blur(src: Mat, dst: Mat, kernelWidth: number, kernelHeight: numb
|
||||
export function blur(...args: any[]) {
|
||||
let [src, dst, kernelWidth, kernelHeight, anchorX = -1, anchorY = -1, borderType = BORDER_DEFAULT] = resolveArgs<[src: Mat, dst: Mat, kernelWidth: number, kernelHeight: number, anchorX?: number, anchorY?: number, borderType?: number]>(args, args[1] instanceof Mat);
|
||||
if (anchorX == -1 || anchorY == -1) anchorX = anchorY = -1;
|
||||
return FromCV(CVMat().Blur(M(src), M(dst), kernelWidth, kernelHeight, anchorX, anchorY, borderType));
|
||||
return __Mat(CVMat().Blur(M(src), M(dst), kernelWidth, kernelHeight, anchorX, anchorY, borderType));
|
||||
}
|
||||
|
||||
export function copyMakeBorder(src: Mat, top: number, bottom: number, left: number, right: number, borderType: number): Mat
|
||||
export function copyMakeBorder(src: Mat, dst: Mat, top: number, bottom: number, left: number, right: number, borderType: number): Mat
|
||||
export function copyMakeBorder(...args: any[]) {
|
||||
const [src, dst, top, bottom, left, right, borderType] = resolveArgs<[src: Mat, dst: Mat, top: number, bottom: number, left: number, right: number, borderType: number]>(args, args[1] instanceof Mat);
|
||||
return FromCV(CVMat().CopyMakeBorder(M(src), M(dst), top, bottom, left, right, borderType));
|
||||
return __Mat(CVMat().CopyMakeBorder(M(src), M(dst), top, bottom, left, right, borderType));
|
||||
}
|
||||
|
||||
type FlipCode = 0 | 1 | -1 | "x" | "y" | "both";
|
||||
@ -40,14 +40,14 @@ export function flip(src: Mat, dst: Mat, flipCode: FlipCode): Mat
|
||||
export function flip(...args: any[]) {
|
||||
let [src, dst, flipCode] = resolveArgs<[src: Mat, dst: Mat, flipCode: FlipCode]>(args, args[1] instanceof Mat);
|
||||
if (typeof flipCode == "string") flipCode = ({ x: 0, y: 1, both: -1 } as Record<"x" | "y" | "both", 0 | 1 | -1>)[flipCode];
|
||||
return FromCV(CVMat().Flip(M(src), M(dst), flipCode));
|
||||
return __Mat(CVMat().Flip(M(src), M(dst), flipCode));
|
||||
}
|
||||
|
||||
export function warpAffine(src: Mat, transfromMat: Mat, dwidth: number, dheight: number, flags?: number, borderMode?: number): Mat;
|
||||
export function warpAffine(src: Mat, dst: Mat, transfromMat: Mat, dwidth: number, dheight: number, flags?: number, borderMode?: number): Mat;
|
||||
export function warpAffine(...args: any[]) {
|
||||
const [src, dst, transfromMat, dwidth, dheight, flags, borderMode] = resolveArgs<[src: Mat, dst: Mat, transfromMat: Mat, dwidth: number, dheight: number, flags?: number, borderMode?: number]>(args, () => args[2] instanceof Mat);
|
||||
return FromCV(CVMat().WarpAffine(M(src), M(dst), M(transfromMat), dwidth, dheight, flags ?? INTER_LINEAR, borderMode ?? BORDER_CONSTANT));
|
||||
return __Mat(CVMat().WarpAffine(M(src), M(dst), M(transfromMat), dwidth, dheight, flags ?? INTER_LINEAR, borderMode ?? BORDER_CONSTANT));
|
||||
}
|
||||
|
||||
export function getRotationMatrix2D(centerX: number, centerY: number, angle: number, scale: number) { return FromCV(CVMat().GetRotationMatrix2D(centerX, centerY, angle, scale)); }
|
||||
export function getRotationMatrix2D(centerX: number, centerY: number, angle: number, scale: number) { return __Mat(CVMat().GetRotationMatrix2D(centerX, centerY, angle, scale)); }
|
||||
|
33
src/cv/videocapture.ts
Normal file
33
src/cv/videocapture.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { CVVideoCapture } from "./addon";
|
||||
import { __Mat } from "./common";
|
||||
import { CAP_ANY } from "./consts";
|
||||
|
||||
export class VideoCapture {
|
||||
#cap: any
|
||||
|
||||
constructor(source: string | number, apiPreference?: number, params?: number[]) {
|
||||
const CVCap = CVVideoCapture();
|
||||
if ((source as any) instanceof CVCap) this.#cap = source;
|
||||
else this.#cap = new CVCap(source, apiPreference ?? CAP_ANY, params ?? []);
|
||||
}
|
||||
|
||||
private get __cap__() { return this.#cap; }
|
||||
|
||||
public open(source: string | number, apiPreference?: number, params?: number[]): boolean { return this.#cap.Open(source, apiPreference ?? CAP_ANY, params ?? []); }
|
||||
public get isOpened(): boolean { return this.#cap.IsOpened(); }
|
||||
|
||||
public grab(): boolean { return this.#cap.Grab(); }
|
||||
|
||||
public retrieve(flag?: number) { return __Mat(this.#cap.Retrieve(flag ?? 0)); }
|
||||
public read() { return __Mat(this.#cap.Read()); }
|
||||
|
||||
public get backendName(): string { return this.#cap.GetBackendName(); }
|
||||
|
||||
public prop(propId: number, value: number): void
|
||||
public prop(propId: number): number
|
||||
public prop(propId: number, value?: number) {
|
||||
if (typeof value === "undefined") return this.#cap.Get(propId);
|
||||
else this.#cap.Set(propId, value);
|
||||
}
|
||||
|
||||
}
|
13
src/test.ts
13
src/test.ts
@ -1,7 +1,7 @@
|
||||
import cv from ".";
|
||||
|
||||
|
||||
async function test() {
|
||||
async function testImage() {
|
||||
await cv.downloadAddon();
|
||||
const fs = await import("fs");
|
||||
// const buffer = fs.readFileSync("data/im1.jpeg")
|
||||
@ -32,5 +32,14 @@ async function test() {
|
||||
cv.imwrite("test_data/draw.jpg", res);
|
||||
}
|
||||
|
||||
async function testMovie() {
|
||||
const cap = new cv.VideoCapture("test_data/movie.mp4");
|
||||
if(cap.grab()){
|
||||
const im = cap.retrieve();
|
||||
debugger;
|
||||
}
|
||||
const im = cap.read();
|
||||
debugger
|
||||
}
|
||||
|
||||
test();
|
||||
testMovie();
|
Reference in New Issue
Block a user