Skip to content

Commit 0e4501e

Browse files
authored
fix: Merge pull request #65 from waoai/fix/cross-origin-and-bb-mask
Fix for Cross Origin Image Loading and Bounding Box Masking
2 parents dbffd24 + d313a8e commit 0e4501e

File tree

4 files changed

+95
-18
lines changed

4 files changed

+95
-18
lines changed

src/FullImageSegmentationAnnotator/index.story.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,50 @@ storiesOf("FullImageSegmentationAnnotator.Basic", module)
2222
// "https://a.allegroimg.com/s128/113a6e/09d2c0ed4f278610e555c95b1d50/Rama-BIANCHI-OLTRE-XR4-DISC-carbon-Vision-ACR-51cm-Dedykowany-a-do-kolarstwo-szosowe",
2323
// src: "https://i.imgur.com/Dv5lkQZ.png",
2424
src: orange,
25+
regions: [
26+
[1, 40, 280],
27+
[1, 10, 10],
28+
[1, 240, 300],
29+
]
30+
.map(([cls, y, x], i) => ({
31+
cls: ["fg", "bg"][cls],
32+
color: "hsl(162,100%,50%)",
33+
editingLabels: false,
34+
highlighted: false,
35+
id: "a" + i,
36+
type: "point",
37+
x: x / 320,
38+
y: y / 249,
39+
}))
40+
.concat([
41+
{
42+
cls: "fg",
43+
color: "hsl(162,100%,50%)",
44+
editingLabels: false,
45+
highlighted: false,
46+
id: "bb",
47+
type: "box",
48+
x: 0.3,
49+
y: 0.25,
50+
w: 0.35,
51+
h: 0.38,
52+
},
53+
]),
54+
},
55+
]}
56+
regionClsList={["bg", "fg"]}
57+
onExit={action("onExit")}
58+
/>
59+
</div>
60+
))
61+
.add("S3 CORS domain", () => (
62+
<div style={{ width: "100vw", height: "100vh" }}>
63+
<FullImageSegmentationAnnotator
64+
images={[
65+
{
66+
name: "Seve's Desk",
67+
src:
68+
"https://s3.amazonaws.com/datasets.workaround.online/faces/010041.jpg",
2569
regions: [
2670
[0, 100, 125],
2771
[0, 100, 150],
@@ -70,7 +114,7 @@ storiesOf("FullImageSegmentationAnnotator.Basic", module)
70114
})),
71115
},
72116
]}
73-
regionClsList={["bg", "orange" , "hand"]}
117+
regionClsList={["bg", "orange", "hand"]}
74118
onExit={action("onExit")}
75119
/>
76120
</div>

src/ImageMask/index.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React, { useState, useEffect, useMemo, useRef } from "react"
44
import { colorInts } from "../colors"
55
import { useDebounce } from "react-use"
6+
import loadImage from "./load-image"
67

78
import MMGC_INIT from "mmgc1-cpp"
89

@@ -24,33 +25,19 @@ export const ImageMask = ({
2425

2526
useEffect(() => {
2627
if (!imageSrc) return
27-
const canvas = document.createElement("canvas")
28-
const ctx = canvas.getContext("2d")
2928

30-
const image = new Image()
31-
image.crossOrigin = "anonymous"
32-
image.src = imageSrc
33-
image.onload = () => {
34-
canvas.width = image.naturalWidth
35-
canvas.height = image.naturalHeight
36-
ctx.drawImage(image, 0, 0)
37-
const imageData = ctx.getImageData(
38-
0,
39-
0,
40-
image.naturalWidth,
41-
image.naturalHeight
42-
)
29+
loadImage(imageSrc).then((imageData) => {
4330
superPixelsGenerated.current = false
4431
setSampleImageData(imageData)
45-
}
32+
})
4633
}, [imageSrc])
4734

4835
useDebounce(
4936
() => {
5037
if (hide) return
5138
if (!canvasRef) return
5239
if (!sampleImageData) return
53-
if (regions.filter((cp) => cp.cls).length < 3) return
40+
if (regions.filter((cp) => cp.cls).length < 2) return
5441
if (!mmgc.setImageSize) return
5542
const context = canvasRef.getContext("2d")
5643

@@ -101,6 +88,19 @@ export const ImageMask = ({
10188
)
10289
}
10390
const classPolygons = regions
91+
.map((r) => {
92+
if (r.type !== "box") return r
93+
return {
94+
...r,
95+
type: "polygon",
96+
points: [
97+
[r.x, r.y],
98+
[r.x + r.w, r.y],
99+
[r.x + r.w, r.y + r.h],
100+
[r.x, r.y + r.h],
101+
],
102+
}
103+
})
104104
.filter((r) => r.type === "polygon")
105105
.filter((r) => r.cls)
106106
for (const polygon of classPolygons) {

src/ImageMask/load-image.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const loadImage = (imageSrc) => {
2+
// Check if image is already loaded in a page element
3+
let image = Array.from(document.getElementsByTagName("img")).find(
4+
(img) => img.src === imageSrc
5+
)
6+
7+
const canvas = document.createElement("canvas")
8+
const ctx = canvas.getContext("2d")
9+
10+
if (!image) {
11+
image = new Image()
12+
image.crossOrigin = "anonymous"
13+
image.src = imageSrc
14+
}
15+
16+
return new Promise((resolve, reject) => {
17+
image.onload = () => {
18+
canvas.width = image.naturalWidth
19+
canvas.height = image.naturalHeight
20+
ctx.drawImage(image, 0, 0)
21+
const imageData = ctx.getImageData(
22+
0,
23+
0,
24+
image.naturalWidth,
25+
image.naturalHeight
26+
)
27+
resolve(imageData)
28+
}
29+
})
30+
}
31+
32+
export default loadImage

src/VideoOrImageCanvasBackground/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export default ({
119119
ref={imageRef}
120120
style={stylePosition}
121121
onLoad={onImageLoaded}
122+
crossOrigin="anonymous"
122123
/>
123124
) : (
124125
<Video

0 commit comments

Comments
 (0)