From 9abb63fa2794c60cfbd4e7ef79c7f727e82dd4dd Mon Sep 17 00:00:00 2001 From: Jash Date: Thu, 19 Nov 2026 12:42:39 +0530 Subject: [PATCH] fix: training editor drops detection results for a frame that is no longer shown refs #6819 --- tests/js/training-detect-frame.test.js | 56 ++++++++++++++++++++++++++ web/skins/classic/views/js/training.js | 3 ++ 2 files changed, 59 insertions(+) create mode 100644 tests/js/training-detect-frame.test.js diff --git a/tests/js/training-detect-frame.test.js b/tests/js/training-detect-frame.test.js new file mode 100644 index 0000000000..2f286c2f26 --- /dev/null +++ b/tests/js/training-detect-frame.test.js @@ -0,0 +1,56 @@ +'use strict'; + +// Detection results were applied to whatever frame was showing when the +// reply arrived. Moving to the next frame during a slow detect put the +// previous frame's boxes on the new frame, marked dirty and ready to save. + +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const vm = require('vm'); + +const src = fs.readFileSync(process.env.TRAINING_JS || + path.join(__dirname, '../../web/skins/classic/views/js/training.js'), 'utf8'); +const match = src.match(/AnnotationEditor\.prototype\.detect = function\(\) \{[\s\S]*?\n\};\n/); +assert.ok(match, 'detect not found in training.js'); + +let reply = null; +const $j = () => ({prop() {}}); +$j.ajax = () => { + const p = { + done(cb) { + reply = cb; return p; + }, + fail() { + return p; + }, + }; + return p; +}; +const sandbox = {$j, thisUrl: '?', logAjaxFail() {}, AnnotationEditor: function() {}}; +vm.createContext(sandbox); +vm.runInContext(match[0], sandbox); + +function editorOn(frame) { + const e = Object.create(sandbox.AnnotationEditor.prototype); + Object.assign(e, { + hasDetectScript: true, currentFrameId: frame, eventId: 1, monitorId: 2, + annotations: [], dirty: false, translations: {}, + _setStatus() {}, _pushUndo() {}, _updateSidebar() {}, _render() {}, + }); + return e; +} +const found = {result: 'Ok', response: {detections: [{label: 'person', confidence: 0.9, bbox: [1, 2, 3, 4]}]}}; + +let editor = editorOn('5'); +editor.detect(); +editor.currentFrameId = '6'; +reply(found); +assert.strictEqual(editor.annotations.length, 0, 'frame 5 detections are not put on frame 6'); +assert.strictEqual(editor.dirty, false, 'and frame 6 is not marked dirty'); + +editor = editorOn('5'); +editor.detect(); +reply(found); +assert.strictEqual(editor.annotations.length, 1, 'detections still land on the frame they were run on'); +console.log('ok - detect results stay with their frame'); diff --git a/web/skins/classic/views/js/training.js b/web/skins/classic/views/js/training.js index c6f50e7c23..d567bdcb1f 100644 --- a/web/skins/classic/views/js/training.js +++ b/web/skins/classic/views/js/training.js @@ -1201,6 +1201,8 @@ AnnotationEditor.prototype.detect = function() { ); $j('#annotationDetectBtn').prop('disabled', true); + // Detection can be slow; the user may move to another frame meanwhile. + const fid = this.currentFrameId; $j.ajax({ url: thisUrl + '?request=training&action=detect', @@ -1213,6 +1215,7 @@ AnnotationEditor.prototype.detect = function() { dataType: 'json' }).done(function(data) { $j('#annotationDetectBtn').prop('disabled', false); + if (self.currentFrameId !== fid) return; if (data.result === 'Error') { self._setStatus(data.message, 'error');