Skip to content

Commit dfa182c

Browse files
committed
bug fixes, release 1.1.4
1 parent ec9cd21 commit dfa182c

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [1.1.4] - 2020-06-05
5+
### Fixed
6+
- Better usage of `HTMLVideoElement` class imported from `dom.window`
7+
- Better accuracy representation in the node status without decimals
8+
9+
### Changed
10+
- Some variable types from `const` to `var`
11+
412
## [1.1.3] - 2020-06-05
513
### Fixed
614
- Prediction does not work when save_image's box is checked - [#14](https://github.com/bonastreyair/node-red-contrib-teachable-machine/issues/14)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-teachable-machine",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Simplifies integration with Teachable Machine models from Google",
55
"dependencies": {
66
"@tensorflow/tfjs": "1.3.1",

teachable_machine.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)