{"id":15951752,"url":"https://github.com/peteruhnak/opencv-ts","last_synced_at":"2025-03-17T15:31:16.282Z","repository":{"id":57097892,"uuid":"326810677","full_name":"peteruhnak/opencv-ts","owner":"peteruhnak","description":"OpenCV TypeScript support","archived":false,"fork":false,"pushed_at":"2021-01-10T14:31:36.000Z","size":4488,"stargazers_count":31,"open_issues_count":1,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T00:53:10.454Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peteruhnak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-04T21:21:48.000Z","updated_at":"2024-11-18T01:06:34.000Z","dependencies_parsed_at":"2022-08-20T18:10:24.910Z","dependency_job_id":null,"html_url":"https://github.com/peteruhnak/opencv-ts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteruhnak%2Fopencv-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteruhnak%2Fopencv-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteruhnak%2Fopencv-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteruhnak%2Fopencv-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peteruhnak","download_url":"https://codeload.github.com/peteruhnak/opencv-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243867079,"owners_count":20360695,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-07T13:03:58.604Z","updated_at":"2025-03-17T15:31:15.157Z","avatar_url":"https://github.com/peteruhnak.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OpenCV-TS\n\nExperimental WIP TypeScript typings and OpenCV.js/wasm loader.\n\nTypings in this repository are generated by reusing/extending OpenCV python and embindgen generators.\nThis approach makes it easier to ensure the typings actually match the wasm, as well as being able to reuse documentation in C++ headers.\n\n## Contents\n\nMain opencv-ts contents:\n\n* hand-written `lib/typings/core` for hand-written `core_bindings.cpp`\n* generated `lib/typings/gen` from wasm build of opencv\n* pretty-printed `lib/_opencv.js` (to avoid generating massive stacktraces in Node 14)\n* generator invoker `ts_generator/typegen.py`\n* `tests/opencv` are copied from `opencv` and converted to typescript mainly to test typings and find missing stuff\n\n## Running Tests\n\n1. clone repo \u0026\u0026 npm install\n2. compile tests `npm run watch:test`\n3. run all tests `npm run test`\n\nOr run a single test file `npm run test:single dist/tests/opencv/test_calib3d.js`\n\n\n## Usage\n\n1. Clone this repository https://github.com/peteruhnak/opencv-ts\n2. `npm install /your/clone/dir/opencv-ts`\n\nTo use opencv in node, the library/wasm must be fully initialized, which may take a moment.\nThis library wraps emscripten's `onRuntimeInitialized` in a Promise-based loader `cv.loadOpenCV(): Promise\u003cvoid\u003e`.\n\n* import the library as `import cv from 'opencv'`\n* await promise `cv.loadOpenCV()` before doing anything\n  * the loader does some derpy stuff to ensure `loadOpenCV` is reentrant, but loaded only once\n* you must access cv objects and types through `cv`\n  * e.g. `import { Mat } from 'opencv'` will not work\n\n\n### Node Example\n\nUses `pngjs` to serialize image.\n\n`cv-test.ts`:\n\n```typescript\nimport cv from 'opencv'\nimport { PNG } from 'pngjs'\nimport fs from 'fs'\n\nfunction matToDataUrl(mat: cv.Mat): string {\n  const size = mat.size()\n  const png = new PNG({ width: size.width, height: size.height })\n  png.data.set(mat.data)\n  const buffer = PNG.sync.write(png)\n  return 'data:image/png;base64,' + buffer.toString('base64')\n}\n\ncv.loadOpenCV().then(() =\u003e {\n  const baseMatSrc = new cv.Mat(2, 4, cv.CV_8UC3)\n  baseMatSrc.data.set(new Uint8Array([\n    255, 0, 0, 0, 255, 0, 0, 0,\n    255, 255, 255, 255, 255, 0, 255, 0,\n    255, 255, 255, 255, 0, 0, 0, 0\n  ]))\n  const baseMat = new cv.Mat()\n  cv.cvtColor(baseMatSrc, baseMat, cv.COLOR_RGB2RGBA)\n  const largerMat = new cv.Mat()\n  cv.resize(baseMat, largerMat, { width: 20 * baseMat.cols, height: 20 * baseMat.rows }, 0, 0, cv.INTER_NEAREST)\n\n  const contents = `\n    \u003cp\u003e\n    base:\u003cbr\u003e\n    \u003cimg src=\"${matToDataUrl(baseMat)}\"\u003e\n    \u003cp\u003e\n    larger:\u003cbr\u003e\n    \u003cimg src=\"${matToDataUrl(largerMat)}\"\u003e\n    `\n  fs.writeFileSync('out.html', contents)\n  console.log('written to out.html')\n\n  baseMatSrc.delete()\n  baseMat.delete()\n  largerMat.delete()\n})\n```\n\n```\nts-node cv-test.ts\n```\n\nOpen `out.html` in browser.\n\n\n### Rebuilding Sources\n\n* Clone this repository https://github.com/peteruhnak/opencv-ts\n* Clone `master` branch of https://github.com/opencv/opencv\n* Clone `ts-experimental` branch of https://github.com/peteruhnak/opencv\n\n### OpenCV.js\n\nBuild (master) of `opencv/opencv` -- follow https://docs.opencv.org/master/d4/da1/tutorial_js_setup.html but use emsdk `1.39.0-upstream` (See also https://github.com/opencv/opencv/issues/19243 )\n\nYou can probably build from `ts-experimental` instead, but changes in `embindgen.py` may be unstable.\n\n### Typings\n\nModify paths in `ts_generator/typegen.py`:\n\n* `opencv_path = \"D:/prog/opencv\"` \u003c- `ts-experimental` opencv clone\n* `build_js_path = \"D:/prog/github/build_js\"` \u003c- compiled opencv.js directory\n* `ts_gen_path = \"D:/prog/opencv-ts/lib/typings/gen\"` \u003c- where to generate typings (`lib/typings/gen` of this repository)\n* `replace_wsl = True` \u003c- applicable when running gen on Windows, but opencv.js was compiled via WSL\n\nRun `npm run gen:types`.\n\nNote: Only Python 3.x is supported. \n\n### What Is Where\n\nThe actual generator in `ts-experimental` of peteruhnak/opencv consists of:\n\n* `modules/js/generator/jsgen.py`\n  * this is code extracted from `enbindgen.py` and modified so it can be actually imported from other modules\n  * adds docstring from `hdr_parser.py` (c++ header parser) to FuncVariant \u0026 ClassInfo\n  * this file is normally responsible for generating `bindings.cpp`\n* `modules/js/generator/tsgen.py`\n  * uses data constructed by `jsgen.py`/`JSWrapperGenerator` (Namespaces, ClassInfo, ...)\n  * generates TypeScript files","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeteruhnak%2Fopencv-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeteruhnak%2Fopencv-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeteruhnak%2Fopencv-ts/lists"}