1
- import * as tf from ' @tensorflow/tfjs'
2
- import { load } from ' ../src/index'
3
- const fs = require ( 'fs' ) ;
4
- const jpeg = require ( 'jpeg-js' ) ;
5
-
1
+ import * as tf from " @tensorflow/tfjs" ;
2
+ import { load } from " ../src/index" ;
3
+ import { exec } from "child_process" ;
4
+ const fs = require ( "fs" ) ;
5
+ const jpeg = require ( "jpeg-js" ) ;
6
6
7
7
// Fix for JEST
8
- const globalAny : any = global
9
- globalAny . fetch = require ( 'node-fetch' )
10
- const timeoutMS = 10000
11
- const NUMBER_OF_CHANNELS = 3
12
-
8
+ const globalAny : any = global ;
9
+ globalAny . fetch = require ( "node-fetch" ) ;
10
+ const timeoutMS = 10000 ;
11
+ const NUMBER_OF_CHANNELS = 3 ;
13
12
14
13
const readImage = ( path : string ) => {
15
- const buf = fs . readFileSync ( path )
16
- const pixels = jpeg . decode ( buf , true )
17
- return pixels
18
- }
14
+ const buf = fs . readFileSync ( path ) ;
15
+ const pixels = jpeg . decode ( buf , true ) ;
16
+ return pixels ;
17
+ } ;
19
18
20
19
// @ts -ignore
21
20
const imageByteArray = ( image , numChannels : number ) => {
22
- const pixels = image . data
21
+ const pixels = image . data ;
23
22
const numPixels = image . width * image . height ;
24
23
const values = new Int32Array ( numPixels * numChannels ) ;
25
24
@@ -29,22 +28,43 @@ const imageByteArray = (image, numChannels: number) => {
29
28
}
30
29
}
31
30
32
- return values
33
- }
31
+ return values ;
32
+ } ;
34
33
35
34
// @ts -ignore
36
35
const imageToInput = ( image , numChannels : number ) => {
37
- const values = imageByteArray ( image , numChannels )
38
- const outShape = [ image . height , image . width , numChannels ] as [ number , number , number ] ;
39
- const input = tf . tensor3d ( values , outShape , 'int32' ) ;
40
-
41
- return input
42
- }
43
-
44
- it ( "Snapshots" , async ( ) => {
45
- const model = await load ( )
46
- const logo = readImage ( `${ __dirname } /../_art/nsfwjs_logo.jpg` )
47
- const input = imageToInput ( logo , NUMBER_OF_CHANNELS )
48
- const predictions = await model . classify ( input )
49
- expect ( predictions ) . toMatchSnapshot ( )
50
- } , timeoutMS )
36
+ const values = imageByteArray ( image , numChannels ) ;
37
+ const outShape = [ image . height , image . width , numChannels ] as [
38
+ number ,
39
+ number ,
40
+ number
41
+ ] ;
42
+ const input = tf . tensor3d ( values , outShape , "int32" ) ;
43
+
44
+ return input ;
45
+ } ;
46
+
47
+ it (
48
+ "Snapshots" ,
49
+ async ( ) => {
50
+ const model = await load ( ) ;
51
+ const logo = readImage ( `${ __dirname } /../_art/nsfwjs_logo.jpg` ) ;
52
+ const input = imageToInput ( logo , NUMBER_OF_CHANNELS ) ;
53
+ const predictions = await model . classify ( input ) ;
54
+ expect ( predictions ) . toMatchSnapshot ( ) ;
55
+ } ,
56
+ timeoutMS
57
+ ) ;
58
+
59
+ it (
60
+ "Bundles and minifies" ,
61
+ ( done ) => {
62
+ const cmd = "yarn scriptbundle && yarn minbundle" ;
63
+ exec ( cmd , ( err ) => {
64
+ if ( err ) done . fail ( "Failed to bundle and minify" ) ;
65
+ // All good!
66
+ done ( ) ;
67
+ } ) ;
68
+ } ,
69
+ timeoutMS * 6
70
+ ) ;
0 commit comments