Skip to content

Commit 6c4350a

Browse files
committed
major improvements to accuracy and performance
1 parent a1fdaa6 commit 6c4350a

File tree

7 files changed

+52
-88
lines changed

7 files changed

+52
-88
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"color-alpha": "^1.0.4",
1212
"get-image-data": "^3.0.1",
1313
"material-survey": "^1.0.34",
14-
"mmgc1-cpp": "^1.0.26",
14+
"mmgc1-cpp": "^1.0.35",
1515
"moment": "^2.23.0",
1616
"react-full-screen": "^0.2.4",
1717
"react-hotkeys": "^2.0.0",

src/Annotator/reducers/convert-expanding-line-to-polygon.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ function clamp(num, min, max) {
55
}
66

77
export default (expandingLine) => {
8-
console.log({ expandingLine })
98
const expandingWidth = expandingLine.expandingWidth || 0.005
109
const pointPairs = expandingLine.points.map(({ x, y, angle, width }, i) => {
1110
if (!angle) {

src/Annotator/reducers/general-reducer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ export default (state: MainLayoutState, action: Action) => {
480480
open: true,
481481
highlighted: true,
482482
color: defaultRegionColor,
483-
defaultRegionCls: defaultRegionCls,
483+
cls: defaultRegionCls,
484484
id: getRandomId(),
485485
}
486486
state = setIn(state, ["mode"], {
@@ -498,7 +498,7 @@ export default (state: MainLayoutState, action: Action) => {
498498
open: true,
499499
highlighted: true,
500500
color: defaultRegionColor,
501-
defaultRegionCls: defaultRegionCls,
501+
cls: defaultRegionCls,
502502
id: getRandomId(),
503503
}
504504
state = setIn(state, ["mode"], {
@@ -693,6 +693,8 @@ export default (state: MainLayoutState, action: Action) => {
693693
const { mode } = state
694694
if (mode) {
695695
switch (mode.mode) {
696+
case "DRAW_EXPANDING_LINE":
697+
case "SET_EXPANDING_LINE_WIDTH":
696698
case "DRAW_POLYGON": {
697699
const { regionId } = mode
698700
return modifyRegion(regionId, null)

src/ImageMask/index.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useDebounce } from "react-use"
66

77
import MMGC_INIT from "mmgc1-cpp"
88

9-
export default ({
9+
export const ImageMask = ({
1010
classPoints,
1111
regionClsList,
1212
imageSrc,
@@ -51,23 +51,34 @@ export default ({
5151
if (!sampleImageData) return
5252
if (classPoints.filter((cp) => cp.cls).length < 3) return
5353
if (!mmgc.setImageSize) return
54-
// NEEDS DEBOUNCE
5554
const context = canvasRef.getContext("2d")
5655

5756
if (!superPixelsGenerated.current) {
5857
superPixelsGenerated.current = "processing"
58+
mmgc.setMaxClusters(10000)
5959
mmgc.setImageSize(sampleImageData.width, sampleImageData.height)
60+
mmgc.setClassColor(0, 0)
61+
for (let i = 0; i < colorInts.length; i++) {
62+
mmgc.setClassColor(i + 1, colorInts[i])
63+
}
6064
const imageAddress = mmgc.getImageAddr()
6165
mmgc.HEAPU8.set(sampleImageData.data, imageAddress)
6266
mmgc.computeSuperPixels()
63-
for (let i = 0; i < colorInts.length; i++) {
64-
mmgc.setClassColor(i, colorInts[i])
65-
}
6667
superPixelsGenerated.current = "done"
6768
}
6869
if (superPixelsGenerated.current !== "done") return
6970

7071
// mmgc.setVerboseMode(true)
72+
if (
73+
!["bg", "background", "nothing"].includes(
74+
regionClsList[0].toLowerCase()
75+
)
76+
) {
77+
console.log(
78+
`first region cls must be "bg" or "background" or "nothing"`
79+
)
80+
return
81+
}
7182
mmgc.clearClassPoints()
7283
for (const classPoint of classPoints) {
7384
if (!classPoint.cls) continue
@@ -100,17 +111,10 @@ export default ({
100111
)
101112

102113
context.clearRect(0, 0, sampleImageData.width, sampleImageData.height)
103-
context.globalAlpha = 0.25
104114
context.putImageData(maskImageData, 0, 0)
105-
context.globalAlpha = 1
106115
},
107116
1000,
108-
[
109-
canvasRef,
110-
sampleImageData,
111-
JSON.stringify(classPoints.map((c) => [c.x, c.y, c.cls])),
112-
hide,
113-
]
117+
[canvasRef, sampleImageData, classPoints, hide]
114118
)
115119

116120
const style = useMemo(() => {
@@ -146,3 +150,5 @@ export default ({
146150
/>
147151
)
148152
}
153+
154+
export default ImageMask

src/RegionSelectAndTransformBoxes/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,16 @@ export const RegionSelectAndTransformBox = memo(
169169
arePropsEqual
170170
)
171171

172-
export const RegionSelectAndTransformBoxes = (props) => {
173-
return props.regions
174-
.filter((r) => r.visible || r.visible === undefined)
175-
.filter((r) => !r.locked)
176-
.map((r, i) => {
177-
return <RegionSelectAndTransformBox key={r.id} {...props} region={r} />
178-
})
179-
}
172+
export const RegionSelectAndTransformBoxes = memo(
173+
(props) => {
174+
return props.regions
175+
.filter((r) => r.visible || r.visible === undefined)
176+
.filter((r) => !r.locked)
177+
.map((r, i) => {
178+
return <RegionSelectAndTransformBox key={r.id} {...props} region={r} />
179+
})
180+
},
181+
(n, p) => n.regions === p.regions
182+
)
180183

181184
export default RegionSelectAndTransformBoxes

src/RegionShapes/index.js

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -107,64 +107,23 @@ const RegionComponents = {
107107
/>
108108
</>
109109
)
110-
return (
111-
<polyline
112-
points={points
113-
.map(({ x, y }) => [x * iw, y * ih])
114-
.map((a) => a.join(" "))
115-
.join(" ")}
116-
strokeWidth={2}
117-
stroke={colorAlpha(region.color, 0.75)}
118-
fill={colorAlpha(region.color, 0.25)}
119-
/>
120-
)
121110
}),
122111
pixel: () => null,
123112
}
124113

125-
export const RegionShapes = ({ mat, imagePosition, regions = [] }) => {
126-
// for (const region of regions.filter(
127-
// (r) => r.visible || r.visible === undefined
128-
// )) {
129-
// switch (region.type) {
130-
// case "box": {
131-
// context.save()
132-
//
133-
// context.shadowColor = "black"
134-
// context.shadowBlur = 4
135-
// context.strokeStyle = region.color
136-
// context.strokeRect(
137-
// region.x * iw,
138-
// region.y * ih,
139-
// region.w * iw,
140-
// region.h * ih
141-
// )
142-
//
143-
// context.restore()
144-
// break
145-
// }
146-
// case "polygon": {
147-
// context.save()
148-
//
149-
// context.shadowColor = "black"
150-
// context.shadowBlur = 4
151-
// context.strokeStyle = region.color
152-
//
153-
// context.beginPath()
154-
// context.moveTo(region.points[0][0] * iw, region.points[0][1] * ih)
155-
// for (const point of region.points) {
156-
// context.lineTo(point[0] * iw, point[1] * ih)
157-
// }
158-
// if (!region.open) context.closePath()
159-
// context.stroke()
160-
// context.restore()
161-
// break
162-
// }
163-
// default:
164-
// break
165-
// }
166-
// }
114+
export const WrappedRegionList = memo(
115+
({ regions, iw, ih }) => {
116+
return regions
117+
.filter((r) => r.visible !== false)
118+
.map((r) => {
119+
const Component = RegionComponents[r.type]
120+
return <Component key={r.regionId} region={r} iw={iw} ih={ih} />
121+
})
122+
},
123+
(n, p) => n.regions === p.regions && n.iw === p.iw && n.ih === p.ih
124+
)
167125

126+
export const RegionShapes = ({ mat, imagePosition, regions = [] }) => {
168127
const iw = imagePosition.bottomRight.x - imagePosition.topLeft.x
169128
const ih = imagePosition.bottomRight.y - imagePosition.topLeft.y
170129
return (
@@ -181,12 +140,7 @@ export const RegionShapes = ({ mat, imagePosition, regions = [] }) => {
181140
height: ih,
182141
}}
183142
>
184-
{regions
185-
.filter((r) => r.visible !== false)
186-
.map((r) => {
187-
const Component = RegionComponents[r.type]
188-
return <Component key={r.regionId} region={r} iw={iw} ih={ih} />
189-
})}
143+
<WrappedRegionList regions={regions} iw={iw} ih={ih} />
190144
</svg>
191145
)
192146
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10841,10 +10841,10 @@ mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@
1084110841
dependencies:
1084210842
minimist "0.0.8"
1084310843

10844-
mmgc1-cpp@^1.0.26:
10845-
version "1.0.26"
10846-
resolved "https://registry.yarnpkg.com/mmgc1-cpp/-/mmgc1-cpp-1.0.26.tgz#0ee55745673eb5cedeee1393a7b698533458442d"
10847-
integrity sha512-BdekMtXha86jFACURppLJaxMWIjxDWI4oSbBRGvQ5YnD3Hy7GuP3OCmuNy2XzqrOD79FwBaCWihqDBzgTGulJA==
10844+
mmgc1-cpp@^1.0.35:
10845+
version "1.0.35"
10846+
resolved "https://registry.yarnpkg.com/mmgc1-cpp/-/mmgc1-cpp-1.0.35.tgz#654140c7e942dc32587cfe6cdc388a453ffa7d16"
10847+
integrity sha512-GVXtpvdQdzd6mNPJe/QqQEAuwUROA3jP5TUiHtbuK27ZfEAM77yyjp5LxzwtvfZ2vq0b0B07SkcpmphEGXsI8g==
1084810848

1084910849
moment@^2.23.0:
1085010850
version "2.24.0"

0 commit comments

Comments
 (0)