opencv-js
Copilot instructions786 starsChanged 4 months agoApache-2.0View on GitHub
Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. OpenCV-JS is a TypeScript NPM package that provides OpenCV.js (JavaScript/WebAssembly version of OpenCV) for both Node.js and browser environments. The package wraps a pre-built 11MB OpenCV.js WASM binary with TypeScript type definitions. Repository: https://github.com/TechStark/opencv-js NPM Package: @techstark/opencv-js Version: 5.0.0-release.1
- Installs packages
We split this copilot-instructions.md into the rules it gives an agent
47 rules. About 2 are common, 21 are distinctive, and 24 are very specific to this project.
≈2
Common · estimate
Many files like this have a rule of this kind.
≈21
Distinctive · estimate
This author's own choices. Worth a look.
24
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 3
Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does …
- Very specific: line 43
Run tests: npm test -- takes ~8 seconds.
- Very specific: line 47
ALWAYS run the complete build process: npm install && npm run build && npm test
- Very specific: line 48
Test package creation: npm pack -- creates .tgz file for distribution testing
- Very specific: line 49
ALWAYS run npm audit fix to address security vulnerabilities before committing
- Distinctive: line 52
ALWAYS test OpenCV functionality after making changes using this pattern:
- Very specific: line 62
ALWAYS call .delete() on OpenCV objects (Mat, Size, etc.) to prevent memory leaks
- Distinctive: line 63
NEVER forget memory cleanup in tests and examples
- Common: line 64
Use try/catch with proper cleanup in finally blocks when appropriate
- Distinctive: line 84
ALWAYS include webpack polyfills for browser usage:
- Very specific: line 102
Test files include: Mat.test.ts, Tracker.test.ts, rect.test.ts, cvKeys.test.ts
- Very specific: line 103
ALWAYS wait for async OpenCV initialization in tests using setupOpenCv() helper
- Distinctive: line 106
After making changes, ALWAYS test these scenarios:
- Very specific: line 114
Use test/Lenna.png for image processing tests
- Very specific: line 115
Use Jimp library for loading images in Node.js environment
- Distinctive: line 138
Always check existing type definitions before adding new ones
- Distinctive: line 149
NEVER CANCEL builds or tests - they may take longer than expected
- Very specific: line 155
Check if types exist in src/types/opencv/
- Distinctive: line 156
Add type definitions following existing patterns
- Distinctive: line 157
Create test in appropriate test file
- Very specific: line 158
Run full validation: npm run build && npm test
- Distinctive: line 159
Test with real OpenCV operations, not just TypeScript compilation
- Very specific: line 162
Use npm audit fix for security updates
- Very specific: line 163
Test package creation with npm pack after updates
- Distinctive: line 164
ALWAYS run full test suite after dependency changes
- Very specific: line 167
Check for missing .delete() calls in tests and examples
- Very specific: line 168
Use translateException() helper for OpenCV error handling
- Distinctive: line 180
Must handle async WASM loading
- Very specific: line 213
ALWAYS run npm audit before committing changes
- Very specific: line 214
Use npm audit fix to automatically fix security vulnerabilities
- Distinctive: line 216
Keep TypeScript and build dependencies up to date
- Very specific: line 220
Verify the source when updating: https://docs.opencv.org/5.0.0/opencv.js
- Very specific: line 221
Do NOT accept modified opencv.js files from untrusted sources
- Very specific: line 224
Always call .delete() on OpenCV objects to prevent memory leaks
- Common: line 226
Use try/finally blocks to ensure cleanup even when errors occur
- Distinctive: line 257
Search existing issues and PRs to avoid duplicates
- Very specific: line 260
Ensure you understand the codebase by running: npm install && npm run build && npm test
- Distinctive: line 263
Follow existing code style and patterns
- Distinctive: line 264
Add TypeScript type definitions for new OpenCV features
- Distinctive: line 266
Update documentation if adding new features or changing behavior
- Very specific: line 267
Ensure all tests pass: npm test
- Very specific: line 269
Check for security issues: npm audit
- Distinctive: line 272
Write clear, descriptive commit messages
- Distinctive: line 274
Ensure CI/CD workflows pass (unit tests, build)
- Distinctive: line 281
Keep PRs focused and reasonably sized
- Distinctive: line 282
Update type definitions when adding new OpenCV methods
- Very specific: line 284
ALWAYS follow these patterns for reliable OpenCV-JS development and avoid common pitfalls with async initialization and memory management.
≈ “Common” is an estimate: at least 1 in 5 of the 3,669 Copilot instructions files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
OpenCV-JS Package Development Instructions
Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here.
About This Project
OpenCV-JS is a TypeScript NPM package that provides OpenCV.js (JavaScript/WebAssembly version of OpenCV) for both Node.js and browser environments. The package wraps a pre-built 11MB OpenCV.js WASM binary with TypeScript type definitions.
Repository: https://github.com/TechStark/opencv-js
NPM Package: @techstark/opencv-js
Version: 5.0.0-release.1
Technology Stack
- Language: TypeScript
- Runtime: Node.js 20.x (also supports browser environments)
- Build Tool: TypeScript Compiler (tsc)
- Testing: Jest with ts-jest
- Package Manager: npm
- OpenCV Version: 5.0.0 (WebAssembly/JavaScript build)
- Key Dependencies: Jimp (for image loading in tests)
Table of Contents
- Working Effectively
- Package Usage Patterns
- Testing and Validation
- File Structure and Navigation
- CI/CD and Publishing
- Common Development Tasks
- Browser vs Node.js Differences
- Performance and Timing Expectations
- Troubleshooting
- Security Considerations
- External Documentation
- Contributing Guidelines
Working Effectively
Initial Setup and Build
- Install dependencies:
npm install-- takes ~15 seconds - Build TypeScript:
npm run build-- takes ~2 seconds. NEVER CANCEL. Set timeout to 60+ seconds. - Run tests:
npm test-- takes ~8 seconds. NEVER CANCEL. Set timeout to 300+ seconds. - Format code:
npm run format-- takes ~1 second
Build Process Validation
- ALWAYS run the complete build process:
npm install && npm run build && npm test - Test package creation:
npm pack-- creates .tgz file for distribution testing - ALWAYS run
npm audit fixto address security vulnerabilities before committing
Manual Testing and Validation
- ALWAYS test OpenCV functionality after making changes using this pattern:
const cv = await require('./dist/opencv.js');
global.cv = cv;
const mat = new cv.Mat(3, 3, cv.CV_8UC1);
console.log(`Mat: ${mat.rows}x${mat.cols}, channels: ${mat.channels()}`);
mat.delete(); // CRITICAL: Always call delete() for memory managementMemory Management Requirements
- ALWAYS call
.delete()on OpenCV objects (Mat, Size, etc.) to prevent memory leaks - NEVER forget memory cleanup in tests and examples
- Use try/catch with proper cleanup in finally blocks when appropriate
Package Usage Patterns
v4.11+ API (Current)
import cvReadyPromise from "@techstark/opencv-js";
const cv = await cvReadyPromise;
// Use cv herev4.10- API (Legacy)
import cv from "@techstark/opencv-js";
cv.onRuntimeInitialized = () => {
// Use cv here
};Browser Configuration
- ALWAYS include webpack polyfills for browser usage:
module.exports = {
resolve: {
fallback: {
fs: false,
path: false,
crypto: false
}
}
};Testing and Validation
Running Tests
- Unit tests use Jest with TypeScript preset
- Tests validate: Mat operations, image processing, color conversions, tracking
- Test files include:
Mat.test.ts,Tracker.test.ts,rect.test.ts,cvKeys.test.ts - ALWAYS wait for async OpenCV initialization in tests using
setupOpenCv()helper
Key Validation Scenarios
After making changes, ALWAYS test these scenarios:
- Basic Mat creation and property access
- Color space conversion (RGBA2GRAY)
- Image filtering operations (GaussianBlur, threshold)
- Contour detection and processing
- Memory cleanup with .delete() calls
Testing with Real Images
- Use
test/Lenna.pngfor image processing tests - Use Jimp library for loading images in Node.js environment
- Pattern:
const jimpSrc = await Jimp.read(path); const img = cv.matFromImageData(jimpSrc.bitmap);
File Structure and Navigation
Key Directories
src/- TypeScript source (mainly type definitions)src/index.ts- Main export (32 bytes, exports from types/opencv)src/types/- Comprehensive OpenCV type definitions
test/- Jest test files with OpenCV functionality validationdist/- Build output including the 11MB opencv.js binary.github/workflows/- CI/CD configuration
Important Files
dist/opencv.js- Pre-built OpenCV.js WASM binary (11MB, core functionality)package.json- NPM configuration with build/test scriptstsconfig.json- TypeScript compilation settingsjest.config.js- Jest testing configurationdoc/cvKeys.json- Runtime OpenCV methods and properties reference
Type Definitions Structure
- Over 100 TypeScript definition files in
src/types/opencv/ - Core modules: Mat.ts, core_array.ts, imgproc_*.ts, calib3d.ts
- Always check existing type definitions before adding new ones
CI/CD and Publishing
GitHub Workflows
unit-test.yml- Runs on PRs/pushes, uses Node.js 20.x, sets NODE_OPTIONS for memorybuild-opencv.yml- Manual workflow for building opencv.js from source (uses Emscripten 2.0.10)npm-publish.yml- Publishes to NPM on releases
Build Requirements
- Node.js 20.x (as used in CI)
- NEVER CANCEL builds or tests - they may take longer than expected
- Set explicit timeouts: 60+ seconds for builds, 300+ seconds for tests
Common Development Tasks
Adding New OpenCV Features
- Check if types exist in
src/types/opencv/ - Add type definitions following existing patterns
- Create test in appropriate test file
- Run full validation:
npm run build && npm test - Test with real OpenCV operations, not just TypeScript compilation
Updating Dependencies
- Use
npm audit fixfor security updates - Test package creation with
npm packafter updates - ALWAYS run full test suite after dependency changes
Memory Debugging
- Check for missing
.delete()calls in tests and examples - Use
translateException()helper for OpenCV error handling - Monitor memory usage in long-running operations
Browser vs Node.js Differences
Node.js Environment
- Direct require() of opencv.js works
- Can use filesystem for loading images
- Full OpenCV functionality available
Browser Environment
- Requires webpack polyfills (fs: false, path: false, crypto: false)
- Must handle async WASM loading
- Limited to browser-compatible image loading methods
Performance and Timing Expectations
Build Times (NEVER CANCEL)
npm install: ~15 secondsnpm run build: ~2 secondsnpm test: ~8 secondsnpm run format: ~1 second- Full CI pipeline: ~2-3 minutes
Package Size
- Source: ~12.8MB unpacked, ~4.1MB packed
- Main contributor: dist/opencv.js (11MB WASM binary)
- 214 files total in package
Troubleshooting
Common Issues
- TypeError: cv.Mat is not a constructor - OpenCV not properly initialized, use await pattern
- Memory issues - Missing .delete() calls on OpenCV objects
- Browser webpack errors - Missing fallback polyfills configuration
- Test failures - OpenCV async initialization not awaited in setupOpenCv()
Debug Commands
node -e "console.log(require('./dist/opencv.js'))"- Test opencv.js loadingnpm audit- Check for security vulnerabilitiesnpm pack && tar -tzf *.tgz | head -20- Inspect package contents
Security Considerations
Dependency Security
- ALWAYS run
npm auditbefore committing changes - Use
npm audit fixto automatically fix security vulnerabilities - Review security advisories for OpenCV.js and WebAssembly-related issues
- Keep TypeScript and build dependencies up to date
WASM Binary Safety
- The
dist/opencv.jsfile is a pre-built WebAssembly binary from OpenCV.org - Verify the source when updating: https://docs.opencv.org/5.0.0/opencv.js
- Do NOT accept modified opencv.js files from untrusted sources
Memory Safety
- Always call
.delete()on OpenCV objects to prevent memory leaks - Memory leaks in WASM can cause browser crashes or performance degradation
- Use try/finally blocks to ensure cleanup even when errors occur
Input Validation
- Validate image dimensions and formats before processing
- Handle invalid inputs gracefully to prevent crashes
- Be cautious with user-provided images in browser environments
External Documentation
OpenCV Resources
Package Resources
- NPM Package Page
- GitHub Repository
- Code Examples Repository
- Live Demo (React)
- Live Demo (Angular)
- Face Detection Demo
TypeScript Resources
Contributing Guidelines
Before Making Changes
- Search existing issues and PRs to avoid duplicates
- For significant changes, open an issue first to discuss
- Fork the repository and create a feature branch
- Ensure you understand the codebase by running:
npm install && npm run build && npm test
Making Changes
- Follow existing code style and patterns
- Add TypeScript type definitions for new OpenCV features
- Include tests for new functionality in the
test/directory - Update documentation if adding new features or changing behavior
- Ensure all tests pass:
npm test - Format code:
npm run format - Check for security issues:
npm audit
Submitting Changes
- Write clear, descriptive commit messages
- Reference related issues in commit messages (e.g., "Fixes #123")
- Ensure CI/CD workflows pass (unit tests, build)
- Provide clear PR description explaining the changes
- Be responsive to review feedback
Code Review Process
- PRs require passing CI checks before merge
- Maintainer review is required
- Keep PRs focused and reasonably sized
- Update type definitions when adding new OpenCV methods
ALWAYS follow these patterns for reliable OpenCV-JS development and avoid common pitfalls with async initialization and memory management.
Where files are headingNew Copilot instructions files have more rules about branches and pull requests, and fewer about code style.
Of the Copilot instructions files we read, the 2,297 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repositoryOne other file it gives its agents.
- CLAUDE.mdCLAUDE.md
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

