1
1
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
+ }
2
11
3
12
export function getElement ( arg : string | any ) {
4
13
if ( typeof arg === 'string' ) {
@@ -22,93 +31,6 @@ export function getMediaDimensions(media: HTMLImageElement | HTMLVideoElement) {
22
31
return media
23
32
}
24
33
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
-
112
34
export function bufferToImage ( buf : Blob ) : Promise < HTMLImageElement > {
113
35
return new Promise ( ( resolve , reject ) => {
114
36
if ( ! ( buf instanceof Blob ) ) {
@@ -127,11 +49,6 @@ export function bufferToImage(buf: Blob): Promise<HTMLImageElement> {
127
49
} )
128
50
}
129
51
130
- export type DrawBoxOptions = {
131
- lineWidth : number
132
- color : string
133
- }
134
-
135
52
export function drawBox (
136
53
ctx : CanvasRenderingContext2D ,
137
54
x : number ,
@@ -145,12 +62,6 @@ export function drawBox(
145
62
ctx . strokeRect ( x , y , w , h )
146
63
}
147
64
148
- export type DrawTextOptions = {
149
- fontSize : number
150
- fontStyle : string
151
- color : string
152
- }
153
-
154
65
export function drawText (
155
66
ctx : CanvasRenderingContext2D ,
156
67
x : number ,
0 commit comments