Giter Club home page Giter Club logo

sm-annotate's Introduction

Video and Image Annotation Tool

This project provides an annotation tool for HTML video and image elements. The tool allows users to draw and annotate over video and images using various drawing tools, including curves, rectangles, ellipses, and texts. Users can also customize the color and stroke size of their annotations.

Demo: lifeart.github.io/sm-annotate

Features

  • โœ๏ธ Drawing and annotating over video and image elements.
  • ๐Ÿ› ๏ธ Multiple drawing tools (curve, rectangle, ellipse, and text).
  • ๐ŸŽจ Customizable color and stroke size for the annotations.
  • โ†ฉ๏ธ Undo functionality.
  • ๐Ÿ”— Serialization and deserialization of drawn shapes.
  • ๐Ÿ“ Scaling shapes to the current canvas size.
  • โŒจ๏ธ Keyboard shortcuts for undo (Ctrl/Cmd + Z).
  • ๐Ÿ–ฑ๏ธ Event handling for pointer events (mouse and touch).
  • ๐ŸŽž๏ธ Playback of annotated frames as video.
  • ๐Ÿ’พ Saving the current frame or all frames with annotations.
  • ๐ŸŽž๏ธ Video Overlay comparsion

Additional Benefits

  • ๐Ÿš€ Zero dependencies.
  • ๐Ÿ“ฑ Support for mobile devices.
  • ๐Ÿ”Œ Powerful plugin system.
  • ๐Ÿ“˜ Written in TypeScript.

Getting Started

Add the package to your project using yarn:

yarn add https://github.com/lifeart/sm-annotate.git

Usage

import { SmAnnotate } from '@lifeart/sm-annotate';
const video = document.getElementById('video');
const annotationTool = new AnnotationTool(video);

Users can draw and annotate using the available tools (curve, rectangle, ellipse, and text) and customize the color and stroke size of the annotations. The tool also supports undo functionality and event handling for pointer events (mouse and touch).

To save the current frame or all frames with annotations, use the saveCurrentFrame or saveAllFrames methods, respectively.

Contributing

We welcome contributions to improve the project. Please feel free to submit issues or pull requests for consideration.

License

This code is allowed for non-commercial use. For commercial use, users must contact the author.

sm-annotate's People

Contributors

lifeart avatar

Stargazers

 avatar

Watchers

 avatar  avatar

sm-annotate's Issues

Support diff colored visualisation

ref:
telegram-cloud-photo-size-2-5434150931555010562-y
https://www.robots.ox.ac.uk/~vgg/software/imcomp/

Sample:

// Assuming img1 and img2 are your two images and they have the same dimensions
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
canvas.width = img1.width;
canvas.height = img1.height;

ctx.drawImage(img1, 0, 0);
let imgData1 = ctx.getImageData(0, 0, canvas.width, canvas.height);

ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img2, 0, 0);
let imgData2 = ctx.getImageData(0, 0, canvas.width, canvas.height);

let imgDataResult = ctx.createImageData(canvas.width, canvas.height);

for (let i = 0; i < imgData1.data.length; i += 4) {
  let r1 = imgData1.data[i];
  let g1 = imgData1.data[i + 1];
  let b1 = imgData1.data[i + 2];
  
  let r2 = imgData2.data[i];
  let g2 = imgData2.data[i + 1];
  let b2 = imgData2.data[i + 2];
  
  // calculate the color difference
  let diff = Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2);
  
  if (diff == 0) {
    // the pixels are the same, set to grayscale
    let grayscale = 0.3 * r1 + 0.59 * g1 + 0.11 * b1;
    imgDataResult.data[i] = grayscale;
    imgDataResult.data[i + 1] = grayscale;
    imgDataResult.data[i + 2] = grayscale;
  } else {
    // the pixels are different, set to blue or red
    imgDataResult.data[i] = r1 > r2 ? 255 : 0; // red channel
    imgDataResult.data[i + 1] = 0; // green channel
    imgDataResult.data[i + 2] = r1 < r2 ? 255 : 0; // blue channel
  }
  imgDataResult.data[i + 3] = 255; // alpha channel
}

ctx.putImageData(imgDataResult, 0, 0);

video comparator

class VisualComparator {
 constructor(videoElement1, videoElement2, canvasElement, leftOpacity = 1, rightOpacity = 1) {
    this.video1 = videoElement1;
    this.video2 = videoElement2;
    this.canvas = canvasElement;
    this.ctx = this.canvas.getContext('2d');
    this.comparisonLine = this.canvas.height / 2;
    this.leftOpacity = leftOpacity;
    this.rightOpacity = rightOpacity;

    this.initEventListeners();
    this.draw();
  }

  initEventListeners() {
    this.canvas.addEventListener('mousemove', this.updateComparisonLine.bind(this));
    this.video1.addEventListener('play', () => {
      this.video2.currentTime = this.video1.currentTime;
      this.video2.play();
    });
    this.video1.addEventListener('pause', () => this.video2.pause());
    this.video1.addEventListener('seeked', () => {
      this.video2.currentTime = this.video1.currentTime;
    });
  }

  updateComparisonLine(event) {
    const rect = this.canvas.getBoundingClientRect();
    const y = event.clientY - rect.top;
    this.comparisonLine = y;
  }

  draw() {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);

    this.ctx.globalAlpha = this.leftOpacity;
    this.ctx.drawImage(
      this.video1,
      0, 0, this.canvas.width, this.comparisonLine,
      0, 0, this.canvas.width, this.comparisonLine
    );

    this.ctx.globalAlpha = this.rightOpacity;
    this.ctx.drawImage(
      this.video2,
      0, this.comparisonLine, this.canvas.width, this.canvas.height - this.comparisonLine,
      0, this.comparisonLine, this.canvas.width, this.canvas.height - this.comparisonLine
    );

    this.ctx.globalAlpha = 1;
    this.ctx.beginPath();
    this.ctx.moveTo(0, this.comparisonLine);
    this.ctx.lineTo(this.canvas.width, this.comparisonLine);
    this.ctx.strokeStyle = 'red';
    this.ctx.stroke();

    requestAnimationFrame(this.draw.bind(this));
  }
}
<video id="video1" src="path/to/video1.mp4" controls></video>
<video id="video2" src="path/to/video2.mp4" controls style="display:none;"></video>
<canvas id="comparisonCanvas" width="640" height="360"></canvas>
const video1 = document.getElementById('video1');
const video2 = document.getElementById('video2');
const comparisonCanvas = document.getElementById('comparisonCanvas');
const leftVideoOpacity = 0.7;
const rightVideoOpacity = 0.7;

const visualComparator = new VisualComparator(video1, video2, comparisonCanvas, leftVideoOpacity, rightVideoOpacity);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.