Skip to content

Commit

Permalink
Fixes on Image returns
Browse files Browse the repository at this point in the history
  • Loading branch information
olokobayusuf committed May 26, 2024
1 parent 5f79276 commit 5d9e05d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
4 changes: 3 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.0.34
*INCOMPLETE*
+ Added `Image.channels` field for inspecting image channel count.
+ Fixed edge prediction output `Image` having incorrect `width` and `height`.
+ Updated to Function C 0.0.20.

## 0.0.33
+ Fixed edge prediction errors caused by request backpressure while the predictor is being loaded.
Expand Down
2 changes: 1 addition & 1 deletion src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright © 2024 NatML Inc. All Rights Reserved.
*/

import { FunctionConfig } from "../function"
import type { FunctionConfig } from "../function"

export interface GraphPayload<T> {
/**
Expand Down
18 changes: 5 additions & 13 deletions src/services/prediction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class PredictionService {
private fxnc: any;
private readonly FXNC_DATA_ROOT = "/fxn";
private readonly FXNC_CACHE_ROOT = `${this.FXNC_DATA_ROOT}/cache`;
private readonly FXNC_VERSION = "0.0.19";
private readonly FXNC_VERSION = "0.0.20";
private readonly FXNC_LIB_URL_BASE = `https://cdn.fxn.ai/edgefxn/${this.FXNC_VERSION}`;

public constructor (client: GraphClient, storage: StorageService) {
Expand Down Expand Up @@ -251,10 +251,10 @@ export class PredictionService {
return { data, type, shape: value.shape };
}
// Image
if (isImage(value)) {
if (isImage(value)) { // INCOMPLETE // Remove `canvas` dependency
const canvas = createCanvas(value.width, value.height);
const ctx = canvas.getContext("2d");
ctx.putImageData(value as ImageData, 0, 0);
ctx.putImageData(value as unknown as ImageData, 0, 0);
const data = canvas.toDataURL();
return { data, type: "image" };
}
Expand Down Expand Up @@ -718,8 +718,8 @@ export class PredictionService {
case FXNDtype.String: return fxnc.UTF8ToString(pData);
case FXNDtype.List: return JSON.parse(fxnc.UTF8ToString(pData));
case FXNDtype.Dict: return JSON.parse(fxnc.UTF8ToString(pData));
case FXNDtype.Image: return toImage(new Uint8Array(fxnc.HEAPU8.buffer, pData, elementCount), shape);
case FXNDtype.Binary: return toArrayBuffer(new Uint8Array(fxnc.HEAPU8.buffer, pData, elementCount));
case FXNDtype.Image: return { data: cloneTypedArray(new Uint8ClampedArray(fxnc.HEAPU8.buffer, pData, elementCount)), width: shape[1], height: shape[0], channels: shape[2] };
case FXNDtype.Binary: return cloneTypedArray(new Uint8Array(fxnc.HEAPU8.buffer, pData, elementCount)).buffer;
default: throw new Error(`Cannot convert prediction output to value because of unknown type: ${type}`);
}
} finally {
Expand Down Expand Up @@ -820,14 +820,6 @@ function toBooleanArrayOrBoolean (buffer: ArrayBuffer, shape: number[]): boolean
return shape.length > 0 ? array : array[0];
}

function toImage (data: Uint8Array, shape: number[]): Image {
return { data: cloneTypedArray(data), width: shape[2], height: shape[1] };
}

function toArrayBuffer (data: Uint8Array): ArrayBuffer {
return cloneTypedArray(data).buffer;
}

function getTypedArrayDtype (data: TypedArray | ArrayBuffer): FXNDtype {
if (data instanceof BoolArray) return FXNDtype.Bool;
if (data instanceof Float32Array) return FXNDtype.Float32;
Expand Down
2 changes: 1 addition & 1 deletion src/services/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function isTensor (value: any): value is Tensor {
* @param value Input value.
* @returns Whether the input value is an image.
*/
export function isImage (value: any): value is Image {
export function isImage (value: any): value is Image { // CHECK // Channel count
return value != null &&
(value.data instanceof Uint8Array || value.data instanceof Uint8ClampedArray) &&
Number.isInteger(value.width) &&
Expand Down
5 changes: 4 additions & 1 deletion src/types/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export type TypedArray =

/**
* Image.
* This is strucurally equal to `ImageData`.
* @see https://developer.mozilla.org/en-US/docs/Web/API/ImageData
*/
export interface Image {
Expand All @@ -40,6 +39,10 @@ export interface Image {
* Image height.
*/
height: number;
/**
* Image channels.
*/
channels: number;
}

/**
Expand Down

0 comments on commit 5d9e05d

Please sign in to comment.