Giter VIP home page Giter VIP logo

react-image-label's Introduction

A comprehensive component for tagging images. Check out demo 1 and demo 2 for some examples.

Features

  • Bounding Box (Rectangle, Square, Circle, and Ellipse), and Polygon Annotations
  • Add, Edit, Drag, Rotate, and Delete Annotations
  • Zoom and Pan
  • Changing image on the fly
  • Colorful annotations
  • Raw or typed input/output

Screenshot of ImageAnnotator

Usage

Install react-image-label using npm.

npm install react-image-label

Then you can just import the component and its hook:

import { ImageAnnotator, useImageAnnotator } from 'react-image-label';

and use it as below:

const { setHandles, annotator } = useImageAnnotator();
<ImageAnnotator
  setHandles={setHandles}
  naturalSize={true}
  imageUrl={'your-image-url'}
  onReady={annotator => { annotator.drawRectangle() }} />

Now you can draw rectangles on the image by dragging the left mouse button.

Mouse and Keyboard events

  • click: Edit/Stop Edit Annotations
  • Ctrl + mouse wheel: Zoom
  • Ctrl + mouse drag: Pan
  • Delete key: Delete Annotations
  • mouse drag: Drag/Edit/Rotate Annotations

Props

The following props can be defined on ImageAnnotator:

Prop Type Description Default
imageUrl * string Use a state for image url if you want to change it on the fly
shapes Shape[] | any[] Annotations being displayed on load (see shapes)
naturalSize boolean To show image in its natural size false
width number container width image.naturalWidth
height number container height image.naturalHeight
discRadius number The radius of the green discs in edit mode 5
onAdded Shape => any When an annotation is drawn (see Annotations with Categories)
onSelected Shape => any When an annotation goes into edit mode by double-clicking
onContextMenu Shape => any When an annotation is right-clicked (see Annotations with Categories)
onReady AnnotatorHandles => any When the component is mounted

(*) required props

Handles

You can access the handles using the annotator object as follows:

<button onClick={() => { annotator.drawCircle() }}>Draw Circle</button>

Below is a list of all handles:

Handle Type Description
drawCircle () => void Allows drawing circles by dragging the left mouse button
drawEllipse () => void Allows drawing ellipses by dragging the left mouse button
drawRectangle () => void Allows drawing rectangles by dragging the left mouse button (keep the shift key to draw square)
drawPolygon () => void Allows drawing polygons by clicking and double-clicking
drawDot () => void Allows adding dots (points) by clicking
stop () => void Stops draw/edit/drag mode
edit (id: number) => void The annotation identified by id can be edited and dragged
stopEdit () => void Stops editing and dragging
updateCategories (id: number, categories: string[], color?: string) => void Updates the categories associated with the annotation identified by id
zoom (factor: number) => void Multiplies the dimensions by factor
getShapes () => Shape[] Gets all annotations (shapes)
container HTMLDivElement The div wrapping the SVG

Annotations with Categories

To attach one or more categories to an annotation, utilize onAdded and onContextMenu props being called when an annotation is drawn and right-clicked, respectively. Use shape.categories to get the previous categories as string[]:

const showCategoriesDialog = (shape) => {
  console.log(shape.id) // 1
  console.log(shape.getCenterWithOffset()) // { X: 247.5, Y: 193 }
  console.log(shape.categories) // string []
  // Show a category selection component
}

return (
  <ImageAnnotator
    onAdded={showCategoriesDialog}
    onContextMenu={showCategoriesDialog}
    ...
);

Finally, call annotator.updateCategories to update the categories of the annotation.

Shapes

The data models for all shapes are listed below:

Shape Data Model type Value
Circle { id: number, centre: [number, number], radius: number, categories: string[], type: string, color: string } circle
Ellipse { id: number, centre: [number, number], radiusX: number, radiusY: number, phi: number, categories: string[], type: string, color: string } ellipse
Rectangle { id: number, points: [number, number][], phi: number, categories: string[], type: string, color: string } rectangle
Polygon { id: number, points: [number, number][], categories: string[], type: string, color: string } polygon
Dot { id: number, position: [number, number], categories: string[], type: string, color: string } dot

Contributing

  • Fork the project.
  • Make changes.
  • Run the project in development mode: npm run ladle.
  • Write your own tests and/or update existing ones in src/test dir.
  • Check the new features and changes using annotator.stories.tsx or your own Stories (*.stories.tsx).
  • Update README with appropriate docs.
  • Commit and PR

Dependencies

The package is dependent on SVG.js through react-svgdotjs package. The following peer dependencies must be specified by your project in order to avoid version conflicts: react, react-dom. NPM will not automatically install these for you but it will show you a warning message with instructions on how to install them.

react-image-label's People

Contributors

taqbostan avatar azadeh-koohjani avatar

Stargazers

Lucas Nardon avatar Fan avatar Jonathan Peña avatar  avatar Nothing avatar  avatar  avatar

Watchers

 avatar

react-image-label's Issues

when i import lib to my nextjs

Internal error: TypeError: Cannot read properties of null (reading 'createElementNS')
at create (./node_modules/react-svgdotjs/node_modules/@svgdotjs/svg.js/src/utils/adopter.js:33:73)
at nodeOrNew (./node_modules/react-svgdotjs/node_modules/@svgdotjs/svg.js/src/utils/adopter.js:65:103)
at new Svg (./node_modules/react-svgdotjs/node_modules/@svgdotjs/svg.js/src/elements/Svg.js:20:71)
at makeInstance (./node_modules/react-svgdotjs/node_modules/@svgdotjs/svg.js/src/utils/adopter.js:44:12)
at eval (./node_modules/react-svgdotjs/dist/index.js:1:1092)
at Object.oN [as useMemo] (/home/iiv030/Documents/my-app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:12:68806)
at t.useMemo (/home/iiv030/Documents/my-app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:162732)
at c (./node_modules/react-svgdotjs/dist/index.js:1:1064)
at au (/home/iiv030/Documents/my-app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:10446)
at /home/iiv030/Documents/my-app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:15122
at aw (/home/iiv030/Documents/my-app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:169

is there a way to disable zooming when mouse wheel scrolling

Hi,

I just updated from version 0.2.8. I suddenly found that since v 1.0.0, the image component supports zoom in /out when mouse wheel scrolling, which is good but I am using Apple Magic mouse, it always trigger unexpected zooming action
😄

How can i change the color of polygon

I have many categories when I plot polygons or other types of labelling tools. I want to differentiate the colours and background label text for each category. Can you suggest a way to do that? For example, if a polygon represents blueberries, it should be blue with blue text, while a polygon representing strawberries should be red with red text. Thanks a lot.

Pass init shapes doesn't work

Hi

I am passing shapes from a redux state, the "imageUrl" can be changed dynamically without any problems but shapes cannot init after the ImageAnnotator receives shapes changes, seems like the component doesn't refresh itself

    <ImageAnnotator
        setHandles={setHandles}
        naturalSize={false}
        width={400}
        imageUrl={file?.url}
        shapes={file?.[FILE_FIELDS.SHAPES.NAME]}
        onContextMenu={showCategoriesDialog}
        onAdded={onAnnotationDraw}
        onReady={annotator => {
            annotatorRef.current = annotator
            annotator.drawRectangle()
        }}
    />

and for "onAdded" props, I tried to set a default category after shape drawn but the "annotator" is always undefined, I solved it by store it in a ref when "onReady" called

thank you for your time

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.