Skip to content

Commit c0fe6b9

Browse files
authored
Use imageUtilities abstractions for darknet and doodlenet (#243)
As discussed in #242.
1 parent f1b9512 commit c0fe6b9

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

src/ImageClassifier/darknet.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as tf from "@tensorflow/tfjs";
77
import getTopKClasses from "../utils/gettopkclasses";
88
import DARKNET_CLASSES from "./DARKNET_CLASSES";
9+
import { isInstanceOfSupportedElement } from "../utils/imageUtilities";
910

1011
const DEFAULTS = {
1112
DARKNET_URL:
@@ -19,19 +20,11 @@ const DEFAULTS = {
1920
function preProcess(img, size) {
2021
let image;
2122
if (!(img instanceof tf.Tensor)) {
22-
if (
23-
img instanceof HTMLImageElement ||
24-
img instanceof HTMLVideoElement ||
25-
img instanceof HTMLCanvasElement ||
26-
img instanceof ImageData
27-
) {
23+
if (isInstanceOfSupportedElement(img)) {
2824
image = tf.browser.fromPixels(img);
2925
} else if (
3026
typeof img === "object" &&
31-
(img.elt instanceof HTMLImageElement ||
32-
img.elt instanceof HTMLVideoElement ||
33-
img.elt instanceof HTMLCanvasElement ||
34-
img.elt instanceof ImageData)
27+
isInstanceOfSupportedElement(img.elt)
3528
) {
3629
image = tf.browser.fromPixels(img.elt); // Handle p5.js image and video.
3730
}

src/ImageClassifier/doodlenet.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as tf from "@tensorflow/tfjs";
77
import getTopKClasses from "../utils/gettopkclasses";
88
import DOODLENET_CLASSES from "./DOODLENET_CLASSES";
9+
import { isInstanceOfSupportedElement } from "../utils/imageUtilities";
910

1011
const DEFAULTS = {
1112
DOODLENET_URL:
@@ -16,19 +17,11 @@ const DEFAULTS = {
1617
function preProcess(img, size) {
1718
let image;
1819
if (!(img instanceof tf.Tensor)) {
19-
if (
20-
img instanceof HTMLImageElement ||
21-
img instanceof HTMLVideoElement ||
22-
img instanceof HTMLCanvasElement ||
23-
img instanceof ImageData
24-
) {
20+
if (isInstanceOfSupportedElement(img)) {
2521
image = tf.browser.fromPixels(img);
2622
} else if (
2723
typeof img === "object" &&
28-
(img.elt instanceof HTMLImageElement ||
29-
img.elt instanceof HTMLVideoElement ||
30-
img.elt instanceof HTMLCanvasElement ||
31-
img.elt instanceof ImageData)
24+
isInstanceOfSupportedElement(img.elt)
3225
) {
3326
image = tf.browser.fromPixels(img.elt); // Handle p5.js image, video and canvas.
3427
}

0 commit comments

Comments
 (0)