@@ -2,14 +2,13 @@ module.exports = function (RED) {
22 /* Initial Setup */
33 // Simulate real HTML
44 const { JSDOM } = require ( 'jsdom' )
5- var dom = new JSDOM ( '<!doctype html><html></html> ' )
5+ var dom = new JSDOM ( '' )
66 global . document = dom . window . document
7- // Require basic libraries
8- const tmImage = require ( '@teachablemachine/image' )
7+ global . HTMLVideoElement = dom . window . HTMLVideoElement
98 const canvas = require ( 'canvas' )
9+ // Require basic libraries
1010 global . fetch = require ( 'node-fetch' )
11- // Teachable Machine needs global scope of HTMLVideoElement class to do a check
12- global . HTMLVideoElement = class HTMLVideoElement { }
11+ const tmImage = require ( '@teachablemachine/image' )
1312
1413 function setNodeStatus ( node , status ) {
1514 switch ( status ) {
@@ -79,8 +78,8 @@ module.exports = function (RED) {
7978 }
8079
8180 function getBestPrediction ( predictions ) {
82- let className = ''
83- let probability = 0
81+ var className = ''
82+ var probability = 0
8483 for ( let i = 0 ; i < predictions . length ; i ++ ) {
8584 if ( predictions [ i ] . probability > probability ) {
8685 className = predictions [ i ] . className
@@ -97,7 +96,7 @@ module.exports = function (RED) {
9796 }
9897
9998 function changeKeyResults ( results ) {
100- const out = [ ]
99+ var out = [ ]
101100 for ( let i = 0 ; i < results . length ; i ++ ) {
102101 out . push ( {
103102 class : results [ i ] . className ,
@@ -110,35 +109,36 @@ module.exports = function (RED) {
110109 // Converts the image, makes inference and treats predictions
111110 async function inference ( msg ) {
112111 setNodeStatus ( node , 'infering' )
113- const image = new canvas . Image ( )
112+ var image = new canvas . Image ( )
114113 image . src = msg . image
115- msg . classes = node . model . getClassLabels ( )
116- const predictions = await node . model . predict ( image )
114+
115+ var predictions = await node . model . predict ( image )
117116
118117 predictions . sort ( byProbabilty )
119- const percentage = predictions [ 0 ] . probability . toFixed ( 2 ) * 100
120- const bestPredictionText = percentage . toString ( ) + '% - ' + predictions [ 0 ] . className
118+ var percentage = ( predictions [ 0 ] . probability * 100 ) . toFixed ( 0 )
119+ var bestPredictionText = percentage . toString ( ) + '% - ' + predictions [ 0 ] . className
121120
122121 if ( node . output === 'best' ) {
123122 msg . payload = [ { class : predictions [ 0 ] . className , score : predictions [ 0 ] . probability } ]
124123 setNodeStatus ( node , bestPredictionText )
125124 } else if ( node . output === 'all' ) {
126- let filteredPredictions = predictions
125+ var filteredPredictions = predictions
127126 filteredPredictions = node . activeThreshold ? filteredPredictions . filter ( prediction => prediction . probability > node . threshold / 100 ) : filteredPredictions
128127 filteredPredictions = node . activeMaxResults ? filteredPredictions . slice ( 0 , node . maxResults ) : filteredPredictions
129128 filteredPredictions = changeKeyResults ( filteredPredictions )
130129
131130 if ( filteredPredictions . length > 0 ) {
132131 setNodeStatus ( node , bestPredictionText )
133132 } else {
134- const statusText = 'score < ' + node . threshold + '%'
133+ var statusText = 'score < ' + node . threshold + '%'
135134 setNodeStatus ( node , statusText )
136135 msg . payload = [ ]
137136 node . send ( msg )
138137 return
139138 }
140139 msg . payload = filteredPredictions
141140 }
141+ msg . classes = node . model . getClassLabels ( )
142142 node . send ( msg )
143143 }
144144
0 commit comments