Skip to content

Commit 6c25e87

Browse files
remove unused stuff
1 parent 83d51e4 commit 6c25e87

File tree

3 files changed

+29
-100
lines changed

3 files changed

+29
-100
lines changed

src/NetInput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { TMediaElement, TNetInput } from './types';
2-
import { Dimensions, getContext2dOrThrow, getElement, getMediaDimensions } from './utils';
1+
import { Dimensions, TMediaElement, TNetInput } from './types';
2+
import { getContext2dOrThrow, getElement, getMediaDimensions } from './utils';
33

44
export class NetInput {
55
private _canvases: HTMLCanvasElement[]

src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
export type TMediaElement = HTMLImageElement | HTMLVideoElement | HTMLCanvasElement
2+
23
export type TNetInputArg = string | TMediaElement
4+
35
export type TNetInput = TNetInputArg | Array<TNetInputArg>
6+
7+
export type Dimensions = {
8+
width: number
9+
height: number
10+
}
11+
12+
export type DrawBoxOptions = {
13+
lineWidth: number
14+
color: string
15+
}
16+
17+
export type DrawTextOptions = {
18+
fontSize: number
19+
fontStyle: string
20+
color: string
21+
}

src/utils.ts

Lines changed: 9 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import { FaceDetectionNet } from './faceDetectionNet/types';
2+
import { DrawBoxOptions, DrawTextOptions } from './types';
3+
4+
export function isFloat(num: number) {
5+
return num % 1 !== 0
6+
}
7+
8+
export function round(num: number) {
9+
return Math.floor(num * 100) / 100
10+
}
211

312
export function getElement(arg: string | any) {
413
if (typeof arg === 'string') {
@@ -22,93 +31,6 @@ export function getMediaDimensions(media: HTMLImageElement | HTMLVideoElement) {
2231
return media
2332
}
2433

25-
export function isFloat(num: number) {
26-
return num % 1 !== 0
27-
}
28-
29-
export function round(num: number) {
30-
return Math.floor(num * 100) / 100
31-
}
32-
33-
export type Dimensions = {
34-
width: number
35-
height: number
36-
}
37-
38-
export function toNetInput(
39-
canvasArg: string | HTMLCanvasElement,
40-
mediaArg: string | HTMLImageElement | HTMLVideoElement,
41-
dims?: Dimensions
42-
): HTMLCanvasElement {
43-
const canvas = getElement(canvasArg)
44-
const media = getElement(mediaArg)
45-
46-
if (!(canvas instanceof HTMLCanvasElement)) {
47-
throw new Error('drawMediaToCanvas - expected canvas to be of type: HTMLCanvasElement')
48-
}
49-
if (!(media instanceof HTMLImageElement || media instanceof HTMLVideoElement)) {
50-
throw new Error('drawMediaToCanvas - expected media to be of type: HTMLImageElement | HTMLVideoElement')
51-
}
52-
53-
const { width, height } = dims || getMediaDimensions(media)
54-
canvas.width = width
55-
canvas.height = height
56-
57-
const ctx = getContext2dOrThrow(canvas)
58-
ctx.drawImage(media, 0, 0, width, height)
59-
return canvas
60-
}
61-
62-
export function mediaToImageData(media: HTMLImageElement | HTMLVideoElement, dims?: Dimensions): ImageData {
63-
if (!(media instanceof HTMLImageElement || media instanceof HTMLVideoElement)) {
64-
throw new Error('mediaToImageData - expected media to be of type: HTMLImageElement | HTMLVideoElement')
65-
}
66-
67-
const canvas = toNetInput(document.createElement('canvas'), media)
68-
const ctx = getContext2dOrThrow(canvas)
69-
70-
const { width, height } = dims || getMediaDimensions(media)
71-
return ctx.getImageData(0, 0, width, height)
72-
}
73-
74-
export function mediaSrcToImageData(
75-
src: string | HTMLImageElement | HTMLVideoElement
76-
): Promise<ImageData> {
77-
return new Promise((resolve, reject) => {
78-
if (typeof src !== 'string') {
79-
if (!(src instanceof HTMLImageElement || src instanceof HTMLVideoElement)) {
80-
return reject('mediaSrcToImageData - expected src to be of type: string | HTMLImageElement | HTMLVideoElement')
81-
}
82-
return resolve(mediaToImageData(src))
83-
}
84-
85-
const img = new Image()
86-
img.onload = () => resolve(mediaToImageData(img))
87-
img.onerror = reject
88-
img.src = src
89-
})
90-
}
91-
92-
export function bufferToImgSrc(buf: Blob): Promise<string> {
93-
return new Promise((resolve, reject) => {
94-
if (!(buf instanceof Blob)) {
95-
return reject('bufferToImgSrc - expected buf to be of type: Blob')
96-
}
97-
98-
const reader = new FileReader()
99-
reader.onload = () => resolve(reader.result)
100-
reader.onerror = reject
101-
reader.readAsDataURL(buf)
102-
})
103-
}
104-
105-
export async function bufferToImageData(buf: Blob): Promise<ImageData> {
106-
if (!(buf instanceof Blob)) {
107-
throw new Error('bufferToImageData - expected buf to be of type: Blob')
108-
}
109-
return mediaSrcToImageData(await bufferToImgSrc(buf))
110-
}
111-
11234
export function bufferToImage(buf: Blob): Promise<HTMLImageElement> {
11335
return new Promise((resolve, reject) => {
11436
if (!(buf instanceof Blob)) {
@@ -127,11 +49,6 @@ export function bufferToImage(buf: Blob): Promise<HTMLImageElement> {
12749
})
12850
}
12951

130-
export type DrawBoxOptions = {
131-
lineWidth: number
132-
color: string
133-
}
134-
13552
export function drawBox(
13653
ctx: CanvasRenderingContext2D,
13754
x: number,
@@ -145,12 +62,6 @@ export function drawBox(
14562
ctx.strokeRect(x, y, w, h)
14663
}
14764

148-
export type DrawTextOptions = {
149-
fontSize: number
150-
fontStyle: string
151-
color: string
152-
}
153-
15465
export function drawText(
15566
ctx: CanvasRenderingContext2D,
15667
x: number,

0 commit comments

Comments
 (0)