PeculiarVentures / GammaCV

GammaCV is a WebGL accelerated Computer Vision library for browser

Home Page:https://gammacv.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typescript Support

jamesmfriedman opened this issue · comments

I'm still using this lib for quite a few things. I'm currently adding types for my own purposes, but I wanted to guage if there was any interest in converting this lib to Typescript. Given the space and subject matter, strong types seem like they would be a must.

We have a little progress to add a ts definitions https://github.com/PeculiarVentures/GammaCV/blob/basic-typings/lib/index.d.ts
I'll try to finish up it ASAP

Moving to TS vs just having types would be good and we would be open to working with you on that change.

Also extensions to the library would also be potentially welcome. Let us know what you’ve added.

I've done a couple of massive js to ts overhauls at this point. It looks like a lot of it would just be moving your current jsdoc comments to proper typescript types. I'd be happy to make a branch and get the boilerplate handled so it can start being iterated on.

Other than that I've just been lazily adding types where I've needed them.

declare module 'gammacv/dist' {
  export class Operation {}

  export class Session {
    init(input: Tensor | Operation): void;
    runOp(
      /** operation to run */
      op: Tensor | Operation,
      /** context of a run, passing the same context twice in a row will use cached result */
      ctx: any,
      /** if passed, the output is put into it. */
      output?: Tensor
    ): void;
  }

  export class Tensor {
    constructor(
      /** the data type for tensor instance */
      dtype: string,
      /** the list of integers */
      shape: ShapeT,
      /** initial data to store */
      data?: number[],
      /** custom mapping from plain to NDArray */
      stride?: number[],
      /** number of data elements to skip */
      offset?: number
    );

    data: Uint8Array;
    dtype: string;
    shape: ShapeT;
    size: number;
    stride: ShapeT;
    get: (x: number, y: number, z: number) => number;
  }

  export class Line {
    static Intersection(l1: Line, l2: Line): TupleT | false;
    fromParallelCoords(
      x: number,
      y: number,
      w: number,
      h: number,
      maxDistance: number,
      maxAngles: number
    ): void;

    x1: number;
    y1: number;
    x2: number;
    y2: number;
    px: number;
    py: number;
    length: number;
    angle: number;
  }

  export class Rect {
    constructor(...args: number[]);
  }

  type ShapeT = [number, number, number];
  type TupleT = [number, number];

  export const imageTensorFromURL: (
    imgSrc: string,
    type?: string,
    size?: ShapeT,
    cors?: boolean
  ) => Promise<Tensor>;

  export const grayscale: (
    /** The source image to be grayscale. */
    tSrc: Tensor
  ) => Tensor;

  export const downsample: (
    /** The source image to be downsampled. */
    tSrc: Tensor,
    /**  Downsampling coefficient. */
    downsampleCoefficient?: number,
    /** Downsampling support two possible variants of processing. */
    type?: 'max' | 'mean'
  ) => Tensor;

  export const upsample: (
    /** The source image to be downsampled. */
    tSrc: Tensor,
    /**  Upsampling coefficient. */
    upsampleCoefficient?: number,
    /** Downsampling support two possible variants of processing. */
    interpolation?: 'nearest' | 'linear'
  ) => Tensor;

  export const norm: (
    /** Input data */
    tSrc: Tensor,
    /** Normalization type */
    type: 'l2' | 'minmax',
    /** Number of layers for a parallel reduction */
    parallelReductionLayers?: number
  ) => Tensor;

  export const gaussianBlur: (
    /** Input data */
    tSrc: Tensor,
    /**  Size of the kernel. */
    kernelSize: number,
    /** Sigma coeficient value. */
    sigma: number
  ) => Tensor;

  export const morphologyEx: (
    /** Input data */
    tSrc: Tensor,
    /**  Operation type. */
    type?: 'open' | 'close' | 'gradient' | 'tophat' | 'blackhat',
    /** Size structure of element. */
    kernelSize?: TupleT,
    /** Optional Kernel */
    tKernel?: Tensor
  ) => Tensor;

  export const sobelOperator: (
    /** The source image. */
    tSrc: Tensor
  ) => Tensor;

  export const cannyEdges: (
    /** Input data */
    tSrc: Tensor,
    /**  The low threshold to be applied. */
    lowThreshold: number,
    /** The high threshold to be applied. */
    highThreshold: number
  ) => Tensor;

  export const dilate: (
    /** Input data */
    tSrc: Tensor,
    /**  Size of the kernel. */
    kernelSize: TupleT,
    /** Optional kernel. */
    tKernel?: Tensor
  ) => Tensor;

  export const pcLinesEnhance: (
    /** The source image. */
    tSrc: Tensor
  ) => Tensor;

  export const pcLines: (
    /** Image edges image should be binarized to [0, 1], could be used with Canny Edges. */
    tSrc: Tensor,
    /**  count of parallel reduction layers. */
    layers?: number,
    /** discretization step */
    dStep?: number,
    /**  reduction coefficient */
    downsampleCoefficient?: number
  ) => Tensor;

  export const canvasFromTensor: (
    canvas: HTMLCanvasElement,
    tSrc: Tensor,
    rgba?: boolean,
    transposed?: boolean
  ) => void;

  export const canvasDrawLine: (
    canvas: HTMLCanvasElement,
    line: Line,
    color?: string,
    width?: number
  ) => void;

  export const tensorFrom: (
    input: Tensor | Operation,
    cast?: boolean
  ) => Tensor;

  export const generateTransformMatrix: (
    /** A rectangle */
    rect: Rect,
    /** this is an output shape you want to have [height, width], e.g you want your quadrilateral to be fixed to a rectangle with width 640 and height 480 */
    dstBounds: TupleT,
    /** we can use a placeholder instead of returning a new Tensor, because we want to reuse already allocated memory, and change the value from a call to call without reconstructing the graph */
    transformMatrix?: Tensor,
    /** Amount to pad */
    pad?: number
  ) => Tensor;

  export const perspectiveProjection: (
    tSrc: Tensor,
    tTransformMatrix: Tensor,
    shape?: ShapeT,
    dType?: string
  ) => Tensor;
}

^ plus you get the benefit of being able to specify multiple output formats for different environments.

Currently, my open src project RMWC supports ES and ESNext out of the box. Transpilation for the win. https://jamesmfriedman.github.io/rmwc/

Please do

@jamesmfriedman, please check out the latest release (0.3.8), which added the Typescript support.

Yeah, thanks guys!