Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"fix" jpg segmentations #5406

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions app/packages/looker/src/worker/canvas-decoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HEATMAP } from "@fiftyone/utilities";
import { HEATMAP, SEGMENTATION } from "@fiftyone/utilities";
import { Coloring } from "..";
import { OverlayMask } from "../numpy";
import { isRgbMaskTargets } from "../overlays/util";

const canvasAndCtx = (() => {
if (typeof OffscreenCanvas !== "undefined") {
Expand Down Expand Up @@ -67,7 +69,12 @@ export const recastBufferToMonoChannel = (
return uint8Array.slice(0, totalPixels).buffer;
};

export const decodeWithCanvas = async (blob: Blob, cls: string) => {
export const decodeWithCanvas = async (
blob: Blob,
cls: string,
field: string,
coloring: Coloring
) => {
let channels: number = 4;

if (blob.type === "image/png") {
Expand Down Expand Up @@ -121,6 +128,31 @@ export const decodeWithCanvas = async (blob: Blob, cls: string) => {
channels = 1;
}

// if it's segmentation, we need to recast according to whether or not this field is mapped to RGB targets
if (cls === SEGMENTATION) {
let maskTargets = coloring.maskTargets[field];
if (!maskTargets) {
maskTargets = coloring.defaultMaskTargets;
}
const isRgbMaskTargets_ = isRgbMaskTargets(maskTargets);
Comment on lines +133 to +136
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add type checking for maskTargets access.

The access to coloring.maskTargets[field] should include type checking to ensure maskTargets exists before accessing it.

-    let maskTargets = coloring.maskTargets[field];
-    if (!maskTargets) {
+    let maskTargets = coloring.maskTargets?.[field];
+    if (maskTargets === undefined) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let maskTargets = coloring.maskTargets[field];
if (!maskTargets) {
maskTargets = coloring.defaultMaskTargets;
}
let maskTargets = coloring.maskTargets?.[field];
if (maskTargets === undefined) {
maskTargets = coloring.defaultMaskTargets;
}


if (!isRgbMaskTargets_ && channels > 1) {
// recast to mono channel because we don't need the other channels
targetsBuffer = recastBufferToMonoChannel(
imageData.data,
width,
height,
channels
);
channels = 1;
}

// note: for JPG segmentations with RGB mask targets, we don't need to recast
// although depending on the JPG compression, we might have some artifacts.
// even the slightest change in color can cause the mask to be rendered as
// background color (transparent) instead of the actual mask color
}

return {
buffer: targetsBuffer,
channels,
Expand Down
14 changes: 12 additions & 2 deletions app/packages/looker/src/worker/disk-overlay-decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ describe("decodeOverlayOnDisk", () => {
url: sampleSrcUrl,
options: { priority: "low" },
});
expect(decodeWithCanvas).toHaveBeenCalledWith(mockBlob, SEGMENTATION);
expect(decodeWithCanvas).toHaveBeenCalledWith(
mockBlob,
SEGMENTATION,
field,
COLORING
);
expect(label.mask).toBeDefined();
expect(label.mask.data).toBe(overlayMask);
expect(label.mask.image).toBeInstanceOf(ArrayBuffer);
Expand Down Expand Up @@ -128,7 +133,12 @@ describe("decodeOverlayOnDisk", () => {
url: sampleSrcUrl,
options: { priority: "low" },
});
expect(decodeWithCanvas).toHaveBeenCalledWith(mockBlob, HEATMAP);
expect(decodeWithCanvas).toHaveBeenCalledWith(
mockBlob,
HEATMAP,
field,
COLORING
);
expect(label.map).toBeDefined();
expect(label.map.data).toBe(overlayMask);
expect(label.map.image).toBeInstanceOf(ArrayBuffer);
Expand Down
16 changes: 8 additions & 8 deletions app/packages/looker/src/worker/disk-overlay-decoder.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { getSampleSrc } from "@fiftyone/state/src/recoil/utils";
import {
DETECTION,
DETECTIONS,
HEATMAP,
SEGMENTATION,
} from "@fiftyone/utilities";
import { DETECTION, DETECTIONS } from "@fiftyone/utilities";
import { Coloring, CustomizeColor } from "..";
import { OverlayMask } from "../numpy";
import { Colorscale } from "../state";
import { decodeWithCanvas, recastBufferToMonoChannel } from "./canvas-decoder";
import { decodeWithCanvas } from "./canvas-decoder";
import { enqueueFetch } from "./pooled-fetch";
import { getOverlayFieldFromCls } from "./shared";

Expand Down Expand Up @@ -119,7 +114,12 @@ export const decodeOverlayOnDisk = async (
let overlayMask: OverlayMask;

try {
overlayMask = await decodeWithCanvas(overlayImageBlob, cls);
overlayMask = await decodeWithCanvas(
overlayImageBlob,
cls,
field,
coloring
);
} catch (e) {
console.error(e);
return;
Expand Down
Loading