@searpent/react-image-annotate 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/.babelrc +6 -0
  2. package/.env +1 -0
  3. package/.flowconfig +2 -0
  4. package/.github/workflows/release-on-master.yml +32 -0
  5. package/.github/workflows/test.yml +16 -0
  6. package/.prettierrc +3 -0
  7. package/.releaserc.js +18 -0
  8. package/.storybook/addons.js +2 -0
  9. package/.storybook/config.js +16 -0
  10. package/LICENSE +21 -0
  11. package/README.md +101 -0
  12. package/package.json +93 -0
  13. package/public/favicon.ico +0 -0
  14. package/public/index.html +38 -0
  15. package/src/Annotator/bike-pic.png +0 -0
  16. package/src/Annotator/bike-pic2.png +0 -0
  17. package/src/Annotator/dab-keyframes.story.json +1 -0
  18. package/src/Annotator/index.js +206 -0
  19. package/src/Annotator/index.story.js +727 -0
  20. package/src/Annotator/poses.story.js +150 -0
  21. package/src/Annotator/reducers/combine-reducers.js +7 -0
  22. package/src/Annotator/reducers/convert-expanding-line-to-polygon.js +53 -0
  23. package/src/Annotator/reducers/fix-twisted.js +6 -0
  24. package/src/Annotator/reducers/general-reducer.js +914 -0
  25. package/src/Annotator/reducers/get-active-image.js +21 -0
  26. package/src/Annotator/reducers/get-implied-video-regions.js +115 -0
  27. package/src/Annotator/reducers/history-handler.js +60 -0
  28. package/src/Annotator/reducers/image-reducer.js +23 -0
  29. package/src/Annotator/reducers/video-reducer.js +85 -0
  30. package/src/Annotator/video.story.js +51 -0
  31. package/src/ClassSelectionMenu/index.js +108 -0
  32. package/src/Crosshairs/index.js +64 -0
  33. package/src/DebugSidebarBox/index.js +36 -0
  34. package/src/DemoSite/Editor.js +235 -0
  35. package/src/DemoSite/ErrorBoundaryDialog.js +34 -0
  36. package/src/DemoSite/index.js +41 -0
  37. package/src/DemoSite/index.story.js +10 -0
  38. package/src/DemoSite/simple-segmentation-example.json +572 -0
  39. package/src/FullImageSegmentationAnnotator/hard1.story.jpg +0 -0
  40. package/src/FullImageSegmentationAnnotator/hard2.story.jpg +0 -0
  41. package/src/FullImageSegmentationAnnotator/hard3.story.jpg +0 -0
  42. package/src/FullImageSegmentationAnnotator/index.js +7 -0
  43. package/src/FullImageSegmentationAnnotator/index.story.js +177 -0
  44. package/src/FullImageSegmentationAnnotator/orange.story.png +0 -0
  45. package/src/HighlightBox/index.js +143 -0
  46. package/src/HistorySidebarBox/index.js +78 -0
  47. package/src/ImageCanvas/dancing-man.story.jpg +0 -0
  48. package/src/ImageCanvas/index.js +488 -0
  49. package/src/ImageCanvas/index.story.js +214 -0
  50. package/src/ImageCanvas/mouse_mask.story.png +0 -0
  51. package/src/ImageCanvas/region-tools.js +171 -0
  52. package/src/ImageCanvas/seves_desk.story.jpg +0 -0
  53. package/src/ImageCanvas/styles.js +27 -0
  54. package/src/ImageCanvas/use-mouse.js +168 -0
  55. package/src/ImageCanvas/use-project-box.js +23 -0
  56. package/src/ImageCanvas/use-wasd-mode.js +50 -0
  57. package/src/ImageMask/index.js +127 -0
  58. package/src/ImageMask/load-image.js +32 -0
  59. package/src/ImageSelectorSidebarBox/index.js +54 -0
  60. package/src/KeyframeTimeline/get-time-string.js +25 -0
  61. package/src/KeyframeTimeline/index.js +223 -0
  62. package/src/KeyframesSelectorSidebarBox/index.js +93 -0
  63. package/src/LandingPage/content.md +57 -0
  64. package/src/LandingPage/github-markdown.css +964 -0
  65. package/src/LandingPage/index.js +147 -0
  66. package/src/MainLayout/icon-dictionary.js +79 -0
  67. package/src/MainLayout/index.js +420 -0
  68. package/src/MainLayout/index.story.js +240 -0
  69. package/src/MainLayout/styles.js +26 -0
  70. package/src/MainLayout/types.js +156 -0
  71. package/src/MainLayout/use-implied-video-regions.js +17 -0
  72. package/src/PointDistances/index.js +90 -0
  73. package/src/PreventScrollToParents/index.js +48 -0
  74. package/src/PreventScrollToParents/index.story.js +23 -0
  75. package/src/RegionLabel/index.js +201 -0
  76. package/src/RegionLabel/styles.js +51 -0
  77. package/src/RegionSelectAndTransformBoxes/index.js +234 -0
  78. package/src/RegionSelectorSidebarBox/index.js +216 -0
  79. package/src/RegionSelectorSidebarBox/styles.js +54 -0
  80. package/src/RegionShapes/index.js +236 -0
  81. package/src/RegionTags/index.js +130 -0
  82. package/src/SettingsDialog/index.js +58 -0
  83. package/src/SettingsProvider/index.js +44 -0
  84. package/src/Shortcuts/ShortcutField.js +44 -0
  85. package/src/Shortcuts/index.js +129 -0
  86. package/src/ShortcutsManager/index.js +162 -0
  87. package/src/Sidebar/index.js +117 -0
  88. package/src/SidebarBoxContainer/index.js +93 -0
  89. package/src/SmallToolButton/index.js +57 -0
  90. package/src/TagsSidebarBox/index.js +93 -0
  91. package/src/TaskDescriptionSidebarBox/index.js +43 -0
  92. package/src/Theme/index.js +36 -0
  93. package/src/VideoOrImageCanvasBackground/index.js +170 -0
  94. package/src/colors.js +32 -0
  95. package/src/hooks/use-event-callback.js +11 -0
  96. package/src/hooks/use-exclude-pattern.js +27 -0
  97. package/src/hooks/use-load-image.js +21 -0
  98. package/src/hooks/use-window-size.js +46 -0
  99. package/src/hooks/xpattern.js +1 -0
  100. package/src/hooks/xpattern.png +0 -0
  101. package/src/index.js +18 -0
  102. package/src/lib.js +7 -0
  103. package/src/screenshot.png +0 -0
  104. package/src/site.css +5 -0
  105. package/src/stories.js +2 -0
  106. package/src/utils/get-from-local-storage.js +7 -0
  107. package/src/utils/get-hotkey-help-text.js +11 -0
  108. package/src/utils/get-landmarks-with-transform.js +23 -0
  109. package/src/utils/set-in-local-storage.js +6 -0
@@ -0,0 +1,170 @@
1
+ // @flow weak
2
+
3
+ import React, { useRef, useEffect, useMemo, useState } from "react"
4
+ import { styled } from "@mui/material/styles"
5
+ import { createTheme, ThemeProvider } from "@mui/material/styles"
6
+ import useEventCallback from "use-event-callback"
7
+ import { useSettings } from "../SettingsProvider"
8
+
9
+ const theme = createTheme()
10
+ const Video = styled("video")(({ theme }) => ({
11
+ zIndex: 0,
12
+ position: "absolute",
13
+ }))
14
+
15
+ const StyledImage = styled("img")(({ theme }) => ({
16
+ zIndex: 0,
17
+ position: "absolute",
18
+ }))
19
+
20
+ const Error = styled("div")(({ theme }) => ({
21
+ zIndex: 0,
22
+ position: "absolute",
23
+ left: 0,
24
+ right: 0,
25
+ bottom: 0,
26
+ top: 0,
27
+ backgroundColor: "rgba(255,0,0,0.2)",
28
+ color: "#ff0000",
29
+ fontWeight: "bold",
30
+ whiteSpace: "pre-wrap",
31
+ padding: 50,
32
+ }))
33
+
34
+ export default ({
35
+ imagePosition,
36
+ mouseEvents,
37
+ videoTime,
38
+ videoSrc,
39
+ imageSrc,
40
+ onLoad,
41
+ useCrossOrigin = false,
42
+ videoPlaying,
43
+ onChangeVideoTime,
44
+ onChangeVideoPlaying,
45
+ }) => {
46
+ const settings = useSettings()
47
+ const videoRef = useRef()
48
+ const imageRef = useRef()
49
+ const [error, setError] = useState()
50
+
51
+ useEffect(() => {
52
+ if (!videoPlaying && videoRef.current) {
53
+ videoRef.current.currentTime = (videoTime || 0) / 1000
54
+ }
55
+ }, [videoTime])
56
+
57
+ useEffect(() => {
58
+ let renderLoopRunning = false
59
+ if (videoRef.current) {
60
+ if (videoPlaying) {
61
+ videoRef.current.play()
62
+ renderLoopRunning = true
63
+ if (settings.videoPlaybackSpeed) {
64
+ videoRef.current.playbackRate = parseFloat(
65
+ settings.videoPlaybackSpeed
66
+ )
67
+ }
68
+ } else {
69
+ videoRef.current.pause()
70
+ }
71
+ }
72
+
73
+ function checkForNewFrame() {
74
+ if (!renderLoopRunning) return
75
+ if (!videoRef.current) return
76
+ const newVideoTime = Math.floor(videoRef.current.currentTime * 1000)
77
+ if (videoTime !== newVideoTime) {
78
+ onChangeVideoTime(newVideoTime)
79
+ }
80
+ if (videoRef.current.paused) {
81
+ renderLoopRunning = false
82
+ onChangeVideoPlaying(false)
83
+ }
84
+ requestAnimationFrame(checkForNewFrame)
85
+ }
86
+ checkForNewFrame()
87
+
88
+ return () => {
89
+ renderLoopRunning = false
90
+ }
91
+ }, [videoPlaying])
92
+
93
+ const onLoadedVideoMetadata = useEventCallback((event) => {
94
+ const videoElm = event.currentTarget
95
+ videoElm.currentTime = (videoTime || 0) / 1000
96
+ if (onLoad)
97
+ onLoad({
98
+ naturalWidth: videoElm.videoWidth,
99
+ naturalHeight: videoElm.videoHeight,
100
+ videoElm: videoElm,
101
+ duration: videoElm.duration,
102
+ })
103
+ })
104
+ const onImageLoaded = useEventCallback((event) => {
105
+ const imageElm = event.currentTarget
106
+ if (onLoad)
107
+ onLoad({
108
+ naturalWidth: imageElm.naturalWidth,
109
+ naturalHeight: imageElm.naturalHeight,
110
+ imageElm,
111
+ })
112
+ })
113
+ const onImageError = useEventCallback((event) => {
114
+ setError(
115
+ `Could not load image\n\nMake sure your image works by visiting ${
116
+ imageSrc || videoSrc
117
+ } in a web browser. If that URL works, the server hosting the URL may be not allowing you to access the image from your current domain. Adjust server settings to enable the image to be viewed.${
118
+ !useCrossOrigin
119
+ ? ""
120
+ : `\n\nYour image may be blocked because it's not being sent with CORs headers. To do pixel segmentation, browser web security requires CORs headers in order for the algorithm to read the pixel data from the image. CORs headers are easy to add if you're using an S3 bucket or own the server hosting your images.`
121
+ }\n\n If you need a hand, reach out to the community at universaldatatool.slack.com`
122
+ )
123
+ })
124
+
125
+ const stylePosition = useMemo(() => {
126
+ let width = imagePosition.bottomRight.x - imagePosition.topLeft.x
127
+ let height = imagePosition.bottomRight.y - imagePosition.topLeft.y
128
+ return {
129
+ imageRendering: "pixelated",
130
+ left: imagePosition.topLeft.x,
131
+ top: imagePosition.topLeft.y,
132
+ width: isNaN(width) ? 0 : width,
133
+ height: isNaN(height) ? 0 : height,
134
+ }
135
+ }, [
136
+ imagePosition.topLeft.x,
137
+ imagePosition.topLeft.y,
138
+ imagePosition.bottomRight.x,
139
+ imagePosition.bottomRight.y,
140
+ ])
141
+
142
+ if (!videoSrc && !imageSrc)
143
+ return <Error>No imageSrc or videoSrc provided</Error>
144
+
145
+ if (error) return <Error>{error}</Error>
146
+
147
+ return (
148
+ <ThemeProvider theme={theme}>
149
+ {imageSrc && videoTime === undefined ? (
150
+ <StyledImage
151
+ {...mouseEvents}
152
+ src={imageSrc}
153
+ ref={imageRef}
154
+ style={stylePosition}
155
+ onLoad={onImageLoaded}
156
+ onError={onImageError}
157
+ crossOrigin={useCrossOrigin ? "anonymous" : undefined}
158
+ />
159
+ ) : (
160
+ <Video
161
+ {...mouseEvents}
162
+ ref={videoRef}
163
+ style={stylePosition}
164
+ onLoadedMetadata={onLoadedVideoMetadata}
165
+ src={videoSrc || imageSrc}
166
+ />
167
+ )}
168
+ </ThemeProvider>
169
+ )
170
+ }
package/src/colors.js ADDED
@@ -0,0 +1,32 @@
1
+ // @flow
2
+
3
+ import * as muiColors from "@mui/material/colors"
4
+
5
+ export const colors = [
6
+ muiColors.red[500],
7
+ muiColors.blue[500],
8
+ muiColors.green[500],
9
+ muiColors.orange[800],
10
+ muiColors.brown[500],
11
+ muiColors.lightGreen[700],
12
+ muiColors.pink[500],
13
+ muiColors.purple[500],
14
+ muiColors.indigo[500],
15
+ muiColors.teal[500],
16
+ muiColors.lime[500],
17
+ muiColors.blueGrey[500],
18
+ ]
19
+
20
+ const transparency = 0x88000000
21
+
22
+ function reverseParseColor(rrggbb) {
23
+ rrggbb = rrggbb.replace("#", "")
24
+ const bbggrr = rrggbb.substr(4, 2) + rrggbb.substr(2, 2) + rrggbb.substr(0, 2)
25
+ return parseInt(bbggrr, 16)
26
+ }
27
+
28
+ export const colorInts: Array<number> = colors.map(
29
+ (c) => (reverseParseColor(c) | transparency) >>> 0
30
+ )
31
+
32
+ export default colors
@@ -0,0 +1,11 @@
1
+ // @flow
2
+
3
+ import { useRef, useCallback, useLayoutEffect, useEffect } from "react"
4
+
5
+ export default (fn) => {
6
+ let ref = useRef()
7
+ useLayoutEffect(() => {
8
+ ref.current = fn
9
+ })
10
+ return useCallback((...args) => (0, ref.current)(...args), [])
11
+ }
@@ -0,0 +1,27 @@
1
+ // @flow weak
2
+
3
+ import { useRef } from "react"
4
+ import excludePatternSrc from "./xpattern.js"
5
+
6
+ export default () => {
7
+ const excludePattern = useRef(null)
8
+ if (excludePattern.current === null) {
9
+ excludePattern.current = {
10
+ image: new Image(),
11
+ pattern: null,
12
+ }
13
+ const canvas = document.createElement("canvas")
14
+ canvas.width = 100
15
+ canvas.height = 100
16
+ const context = canvas.getContext("2d")
17
+
18
+ excludePattern.current.image.onload = () => {
19
+ excludePattern.current.pattern = context.createPattern(
20
+ excludePattern.current.image,
21
+ "repeat"
22
+ )
23
+ }
24
+ excludePattern.current.image.src = excludePatternSrc
25
+ }
26
+ return excludePattern.current.pattern
27
+ }
@@ -0,0 +1,21 @@
1
+ // @flow weak
2
+
3
+ import { useRef, useState } from "react"
4
+
5
+ export default (imageSrc, onImageLoaded) => {
6
+ const [imageLoaded, changeImageLoaded] = useState(false)
7
+ const image = useRef(null)
8
+ if (image.current === null) {
9
+ image.current = new Image()
10
+ image.current.onload = () => {
11
+ changeImageLoaded(true)
12
+ if (onImageLoaded)
13
+ onImageLoaded({
14
+ width: image.current.naturalWidth,
15
+ height: image.current.naturalHeight,
16
+ })
17
+ }
18
+ image.current.src = imageSrc
19
+ }
20
+ return [image.current, imageLoaded]
21
+ }
@@ -0,0 +1,46 @@
1
+ // @flow
2
+
3
+ import { useEffect } from "react"
4
+
5
+ import { useRafState, useInterval } from "react-use"
6
+
7
+ const useWindowSize = (initialWidth = Infinity, initialHeight = Infinity) => {
8
+ const isClient = typeof window !== "undefined"
9
+ const [state, setState] = useRafState({
10
+ width: isClient ? window.innerWidth : initialWidth,
11
+ height: isClient ? window.innerHeight : initialHeight,
12
+ })
13
+
14
+ useEffect(() => {
15
+ if (!isClient) return
16
+ const handler = () => {
17
+ setState({
18
+ width: window.innerWidth,
19
+ height: window.innerHeight,
20
+ })
21
+ }
22
+
23
+ window.addEventListener("resize", handler)
24
+
25
+ return () => {
26
+ window.removeEventListener("resize", handler)
27
+ }
28
+ }, [])
29
+
30
+ useInterval(() => {
31
+ if (!isClient) return
32
+ if (
33
+ window.innerWidth !== state.width ||
34
+ window.innerHeight !== state.height
35
+ ) {
36
+ setState({
37
+ width: window.innerWidth,
38
+ height: window.innerHeight,
39
+ })
40
+ }
41
+ }, 100)
42
+
43
+ return state
44
+ }
45
+
46
+ export default useWindowSize
@@ -0,0 +1 @@
1
+ export default `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH4wMSAxY5oG+lzgAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAcSURBVAjXY/jPwPCfAQnA+TAGugLcAhhakRUAAK3lEe8m9qZhAAAAAElFTkSuQmCC`
Binary file
package/src/index.js ADDED
@@ -0,0 +1,18 @@
1
+ // @flow
2
+
3
+ import React from "react"
4
+ import ReactDOM from "react-dom"
5
+ import Theme from "./Theme"
6
+ import DemoSite from "./DemoSite"
7
+ import LandingPage from "./LandingPage"
8
+ import "./site.css"
9
+
10
+ const Site = () => {
11
+ const path = window.location.pathname
12
+ .replace(/\/$/, "")
13
+ .split("/")
14
+ .slice(-1)[0]
15
+ return <Theme>{path === "demo" ? <DemoSite /> : <LandingPage />}</Theme>
16
+ }
17
+
18
+ ReactDOM.render(<Site />, document.getElementById("root"))
package/src/lib.js ADDED
@@ -0,0 +1,7 @@
1
+ // @flow
2
+
3
+ import Annotator from "./Annotator"
4
+
5
+ export { Annotator }
6
+
7
+ export default Annotator
Binary file
package/src/site.css ADDED
@@ -0,0 +1,5 @@
1
+ html,
2
+ body {
3
+ padding: 0;
4
+ margin: 0;
5
+ }
package/src/stories.js ADDED
@@ -0,0 +1,2 @@
1
+ const importAll = (r) => r.keys().map(r)
2
+ importAll(require.context("./", true, /\.story\.js$/))
@@ -0,0 +1,7 @@
1
+ export default (key, defaultValue) => {
2
+ try {
3
+ return JSON.parse(window.localStorage[`__REACT_IMAGE_ANNOTATE_${key}`])
4
+ } catch (e) {
5
+ return defaultValue
6
+ }
7
+ }
@@ -0,0 +1,11 @@
1
+ import { getApplicationKeyMap } from "react-hotkeys"
2
+
3
+ export const getHotkeyHelpText = (commandName) => {
4
+ const firstSequence =
5
+ getApplicationKeyMap()[commandName]?.sequences?.[0]?.sequence
6
+
7
+ if (!firstSequence) return ""
8
+ return ` (${firstSequence})`
9
+ }
10
+
11
+ export default getHotkeyHelpText
@@ -0,0 +1,23 @@
1
+ // @flow
2
+ import type { KeypointDefinition } from "../ImageCanvas/region-tools"
3
+
4
+ type Parameters = {
5
+ center: { x: number, y: number },
6
+ scale: number,
7
+ landmarks: {
8
+ [string]: KeypointDefinition,
9
+ },
10
+ }
11
+
12
+ export default ({ center, scale, landmarks }: Parameters) => {
13
+ const points = {}
14
+ for (const [keypointId, { defaultPosition }] of (Object.entries(
15
+ landmarks
16
+ ): any)) {
17
+ points[keypointId] = {
18
+ x: defaultPosition[0] * scale + center.x,
19
+ y: defaultPosition[1] * scale + center.y,
20
+ }
21
+ }
22
+ return points
23
+ }
@@ -0,0 +1,6 @@
1
+ export default (key, val) => {
2
+ window.localStorage.setItem(
3
+ `__REACT_IMAGE_ANNOTATE_${key}`,
4
+ JSON.stringify(val)
5
+ )
6
+ }