| import { ControlWavePlugin } from '../plugin-loader.js'; |
|
|
| export default class SignXPlugin extends ControlWavePlugin { |
| constructor() { |
| super({ |
| id: 'signx-inference', |
| name: 'SignX', |
| version: '1.0.0', |
| author: 'ControlWave Team', |
| description: 'Sign language recognition inference powered by SignX', |
| needsViser: false, |
| order: 100 |
| }); |
|
|
| this.currentVideoPath = null; |
| this.isInferring = false; |
| this.videoEl = null; |
| this.logsPre = null; |
| this.currentJobId = null; |
| this.logCursor = 0; |
| this.logPollingTimer = null; |
| this.runButtonOriginalText = null; |
| } |
|
|
| async render(container) { |
| container.innerHTML = ''; |
| container.className = 'tab-content active'; |
|
|
| |
| const style = document.createElement('style'); |
| style.textContent = ` |
| .signx-root { |
| display: flex; |
| flex-direction: column; |
| gap: 20px; |
| width: 100%; |
| height: 100%; |
| padding: 20px; |
| box-sizing: border-box; |
| } |
| |
| .signx-top { |
| display: flex; |
| gap: 20px; |
| width: 100%; |
| flex: 1; |
| } |
| |
| .signx-left, .signx-right, .signx-bottom { |
| background: #fff; |
| border-radius: 12px; |
| box-shadow: 0 4px 16px rgba(15, 23, 42, 0.08); |
| padding: 20px; |
| display: flex; |
| flex-direction: column; |
| } |
| |
| .signx-left { |
| flex: 1.2; |
| } |
| |
| .signx-right { |
| flex: 1; |
| overflow-y: auto; |
| } |
| |
| .signx-bottom { |
| width: 100%; |
| min-height: 200px; |
| } |
| |
| .signx-video-frame { |
| position: relative; |
| width: 100%; |
| padding-top: 56.25%; |
| background: #0f172a; |
| border-radius: 10px; |
| overflow: hidden; |
| } |
| |
| .signx-bottom-title { |
| font-size: 18px; |
| font-weight: 600; |
| color: #0f172a; |
| margin-bottom: 12px; |
| } |
| |
| .signx-results-grid { |
| display: grid; |
| grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); |
| gap: 16px; |
| width: 100%; |
| } |
| |
| .signx-results-grid .signx-result-section { |
| margin: 0; |
| } |
| |
| .signx-result-section.full-width { |
| grid-column: 1 / -1; |
| } |
| |
| @media (max-width: 1200px) { |
| .signx-top { |
| flex-direction: column; |
| } |
| .signx-results-grid { |
| grid-template-columns: 1fr; |
| } |
| } |
| |
| .signx-video-frame video { |
| position: absolute; |
| top: 0; |
| left: 0; |
| width: 100%; |
| height: 100%; |
| object-fit: contain; |
| background: #0f172a; |
| } |
| |
| .signx-video-placeholder { |
| position: absolute; |
| inset: 0; |
| display: flex; |
| align-items: center; |
| justify-content: center; |
| color: #94a3b8; |
| font-size: 15px; |
| text-align: center; |
| padding: 20px; |
| } |
| |
| .signx-video-controls { |
| margin-top: 15px; |
| display: flex; |
| gap: 10px; |
| flex-direction: column; |
| } |
| |
| .signx-section-title { |
| font-size: 18px; |
| font-weight: 600; |
| color: #0f172a; |
| margin-bottom: 16px; |
| padding-bottom: 10px; |
| border-bottom: 2px solid #2196F3; |
| } |
| |
| .signx-form-group { |
| margin-bottom: 15px; |
| } |
| |
| .signx-form-group label { |
| display: block; |
| margin-bottom: 5px; |
| font-weight: 500; |
| color: #475569; |
| font-size: 13px; |
| } |
| |
| .signx-form-group select { |
| width: 100%; |
| padding: 8px 12px; |
| border: 1px solid #cbd5e1; |
| border-radius: 6px; |
| font-size: 14px; |
| background: white; |
| cursor: pointer; |
| transition: all 0.2s; |
| } |
| |
| .signx-form-group select:focus { |
| outline: none; |
| border-color: #2196F3; |
| box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1); |
| } |
| |
| .signx-btn { |
| width: 100%; |
| padding: 10px 20px; |
| border: none; |
| border-radius: 6px; |
| font-size: 14px; |
| font-weight: 500; |
| cursor: pointer; |
| transition: all 0.2s; |
| } |
| |
| .signx-btn-primary { |
| background: #2196F3; |
| color: white; |
| } |
| |
| .signx-btn-primary:hover:not(:disabled) { |
| background: #1976D2; |
| transform: translateY(-1px); |
| box-shadow: 0 4px 8px rgba(33, 150, 243, 0.3); |
| } |
| |
| .signx-btn:disabled { |
| background: #cbd5e1; |
| cursor: not-allowed; |
| opacity: 0.6; |
| } |
| |
| .signx-status { |
| margin-top: 20px; |
| padding: 12px 15px; |
| background: #f1f5f9; |
| border-radius: 6px; |
| border-left: 3px solid #2196F3; |
| font-size: 13px; |
| line-height: 1.6; |
| } |
| |
| .signx-status.info { |
| border-left-color: #2196F3; |
| background: #E3F2FD; |
| } |
| |
| .signx-status.success { |
| border-left-color: #4CAF50; |
| background: #E8F5E9; |
| } |
| |
| .signx-status.error { |
| border-left-color: #f44336; |
| background: #FFEBEE; |
| } |
| |
| .signx-status.loading { |
| border-left-color: #FF9800; |
| background: #FFF3E0; |
| } |
| |
| .signx-logs { |
| margin-top: 20px; |
| } |
| |
| .signx-logs pre { |
| background: #0f172a; |
| color: #cbd5e1; |
| border-radius: 8px; |
| padding: 14px; |
| font-size: 12px; |
| line-height: 1.6; |
| max-height: 400px; |
| overflow-y: auto; |
| white-space: pre-wrap; |
| word-wrap: break-word; |
| font-family: 'Consolas', 'Monaco', 'Courier New', monospace; |
| } |
| |
| .signx-result-section { |
| margin-top: 20px; |
| padding: 15px; |
| background: #f8fafc; |
| border-radius: 8px; |
| border: 1px solid #e2e8f0; |
| } |
| |
| .signx-result-section h4 { |
| margin: 0 0 10px 0; |
| color: #0f172a; |
| font-size: 14px; |
| font-weight: 600; |
| } |
| |
| .signx-result-content { |
| font-family: monospace; |
| font-size: 13px; |
| line-height: 1.6; |
| color: #1e293b; |
| background: white; |
| padding: 10px; |
| border-radius: 4px; |
| max-height: 150px; |
| overflow-y: auto; |
| } |
| |
| .spinner { |
| display: inline-block; |
| width: 14px; |
| height: 14px; |
| border: 2px solid #f3f3f3; |
| border-top: 2px solid #2196F3; |
| border-radius: 50%; |
| animation: spin 1s linear infinite; |
| margin-right: 8px; |
| vertical-align: middle; |
| } |
| |
| @keyframes spin { |
| 0% { transform: rotate(0deg); } |
| 100% { transform: rotate(360deg); } |
| } |
| |
| .signx-analysis-item { |
| margin-top: 20px; |
| padding-top: 20px; |
| border-top: 1px solid #e2e8f0; |
| } |
| |
| .signx-analysis-item:first-child { |
| margin-top: 0; |
| padding-top: 0; |
| border-top: none; |
| } |
| |
| .signx-analysis-item h5 { |
| margin: 0 0 10px 0; |
| color: #475569; |
| font-size: 13px; |
| font-weight: 600; |
| } |
| |
| .signx-analysis-item img { |
| display: block; |
| max-width: 100%; |
| height: auto; |
| border: 1px solid #e2e8f0; |
| border-radius: 4px; |
| transition: transform 0.2s, box-shadow 0.2s; |
| } |
| |
| .signx-analysis-item img:hover { |
| transform: scale(1.02); |
| box-shadow: 0 4px 12px rgba(0,0,0,0.1); |
| } |
| |
| .signx-link-button { |
| display: inline-block; |
| padding: 8px 16px; |
| background: #2196F3; |
| color: white; |
| text-decoration: none; |
| border-radius: 4px; |
| font-size: 13px; |
| font-weight: 500; |
| transition: background 0.2s; |
| } |
| |
| .signx-link-button:hover { |
| background: #1976D2; |
| } |
| |
| .signx-keyframes-grid { |
| display: grid; |
| grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); |
| gap: 10px; |
| margin-top: 10px; |
| } |
| |
| .signx-keyframe-preview { |
| text-align: center; |
| } |
| |
| .signx-keyframe-preview img { |
| width: 100%; |
| height: auto; |
| border: 1px solid #e2e8f0; |
| border-radius: 4px; |
| } |
| |
| .signx-keyframe-preview span { |
| display: block; |
| margin-top: 4px; |
| font-size: 11px; |
| color: #64748b; |
| } |
| `; |
|
|
| container.appendChild(style); |
|
|
| |
| const root = document.createElement('div'); |
| root.className = 'signx-root'; |
| root.innerHTML = ` |
| <div class="signx-top"> |
| <div class="signx-left"> |
| <h3 class="signx-section-title">Video Preview</h3> |
| <div class="signx-video-frame"> |
| <video id="signx-video" controls loop></video> |
| <div class="signx-video-placeholder" id="signx-video-placeholder"> |
| Select a video to preview it here |
| </div> |
| </div> |
| <div class="signx-video-controls"> |
| <div class="signx-form-group"> |
| <label>Video Folder</label> |
| <select id="signx-video-folder"> |
| <option value="videos">videos (test)</option> |
| <option value="good_videos" selected>good_videos (recommended)</option> |
| </select> |
| </div> |
| <div class="signx-form-group"> |
| <label>Video File</label> |
| <select id="signx-video-file"> |
| <option value="">Loading...</option> |
| </select> |
| </div> |
| <button id="signx-run-inference" class="signx-btn signx-btn-primary" disabled> |
| Run Inference |
| </button> |
| </div> |
| </div> |
| |
| <div class="signx-right"> |
| <h3 class="signx-section-title">Status & Logs</h3> |
| |
| <div id="signx-status" class="signx-status info"> |
| Select a video file to begin |
| </div> |
| |
| <div class="signx-logs"> |
| <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #475569; font-size: 13px;"> |
| Inference Logs |
| </label> |
| <pre id="signx-logs">(waiting for inference job...)</pre> |
| </div> |
| </div> |
| </div> |
| |
| <div class="signx-bottom"> |
| <div class="signx-bottom-title">Inference Outputs & Visualizations</div> |
| <div id="signx-results-container" class="signx-results-grid"></div> |
| </div> |
| `; |
|
|
| container.appendChild(root); |
|
|
| |
| this.videoEl = document.getElementById('signx-video'); |
| this.videoPlaceholder = document.getElementById('signx-video-placeholder'); |
| this.logsPre = document.getElementById('signx-logs'); |
|
|
| |
| if (this.videoEl) { |
| this.videoEl.style.display = 'none'; |
| } |
|
|
| |
| this.setupEventListeners(); |
|
|
| |
| this.loadVideoList(); |
| } |
|
|
| setupEventListeners() { |
| const folderSelect = document.getElementById('signx-video-folder'); |
| const fileSelect = document.getElementById('signx-video-file'); |
| const runButton = document.getElementById('signx-run-inference'); |
|
|
| if (folderSelect) { |
| folderSelect.addEventListener('change', () => { |
| this.loadVideoList(); |
| }); |
| } |
|
|
| if (fileSelect) { |
| fileSelect.addEventListener('change', (e) => { |
| this.currentVideoPath = e.target.value; |
| const runButton = document.getElementById('signx-run-inference'); |
| if (runButton) { |
| runButton.disabled = !this.currentVideoPath; |
| } |
|
|
| if (this.currentVideoPath) { |
| this.updateStatus('Selected video: ' + this.currentVideoPath.split('/').pop(), 'info'); |
| this.updateVideo(this.currentVideoPath); |
| } |
| }); |
| } |
|
|
| if (runButton) { |
| runButton.addEventListener('click', () => { |
| this.runInference(); |
| }); |
| } |
| } |
|
|
| async loadVideoList() { |
| const folderSelect = document.getElementById('signx-video-folder'); |
| const fileSelect = document.getElementById('signx-video-file'); |
|
|
| if (!folderSelect || !fileSelect) { |
| console.error('SignX: unable to locate folder or file selectors'); |
| return; |
| } |
|
|
| const folder = folderSelect.value; |
| console.log(`SignX: loading video list, folder=${folder}`); |
| fileSelect.innerHTML = '<option value="">Loading...</option>'; |
|
|
| try { |
| const data = await this.sendPluginMessage('list_videos', { folder: folder }); |
|
|
| console.log('SignX: received response:', data); |
|
|
| if (data && data.status === 'success') { |
| fileSelect.innerHTML = '<option value="">Select a video file</option>'; |
|
|
| if (data.videos && data.videos.length > 0) { |
| data.videos.forEach(video => { |
| const option = document.createElement('option'); |
| option.value = video.path; |
| option.textContent = video.name; |
| fileSelect.appendChild(option); |
| }); |
| console.log(`SignX: loaded ${data.videos.length} videos`); |
| this.updateStatus(`Found ${data.videos.length} video files`, 'info'); |
| } else { |
| console.warn('SignX: video list is empty'); |
| this.updateStatus('No video files found', 'info'); |
| } |
| } else { |
| console.error('SignX: response status not success:', data); |
| fileSelect.innerHTML = '<option value="">Failed to load</option>'; |
| this.updateStatus('Failed to load video list: ' + (data ? data.message : 'Unknown error'), 'error'); |
| } |
| } catch (error) { |
| console.error('SignX: error while loading videos:', error); |
| fileSelect.innerHTML = '<option value="">Failed to load</option>'; |
| this.updateStatus('Failed to load video list: ' + error.message, 'error'); |
| } |
| } |
|
|
| async updateVideo(videoPath) { |
| if (!this.videoEl || !this.videoPlaceholder) { |
| return; |
| } |
|
|
| try { |
| const videoUrl = videoPath.replace(/.*\/plugins\/SignX/, '/plugins/SignX'); |
|
|
| console.log('SignX: loading video URL:', videoUrl); |
|
|
| this.videoEl.src = videoUrl; |
| this.videoEl.style.display = 'block'; |
| this.videoPlaceholder.style.display = 'none'; |
|
|
| this.videoEl.onloadeddata = () => { |
| console.log('SignX: video loaded'); |
| }; |
|
|
| this.videoEl.onerror = () => { |
| this.videoEl.style.display = 'none'; |
| this.videoPlaceholder.style.display = 'flex'; |
| this.videoPlaceholder.textContent = 'Video failed to load'; |
| console.error('SignX: failed to load video URL:', videoUrl); |
| }; |
|
|
| this.videoEl.load(); |
| } catch (error) { |
| this.videoEl.style.display = 'none'; |
| this.videoPlaceholder.style.display = 'flex'; |
| this.videoPlaceholder.textContent = 'Video failed to load'; |
| console.error('SignX: video load error:', error); |
| } |
| } |
|
|
| async runInference() { |
| if (!this.currentVideoPath || this.isInferring) { |
| return; |
| } |
|
|
| const runButton = document.getElementById('signx-run-inference'); |
| if (!runButton) { |
| return; |
| } |
|
|
| this.isInferring = true; |
| this.runButtonOriginalText = runButton.innerHTML; |
| runButton.disabled = true; |
| runButton.innerHTML = '<span class="spinner"></span>Running...'; |
|
|
| this.updateStatus('Running SignX inference. This may take a few minutes...', 'loading'); |
| if (this.logsPre) { |
| this.logsPre.textContent = 'Starting inference...\n'; |
| } |
|
|
| try { |
| const response = await this.sendPluginMessage('run_inference', { |
| video_path: this.currentVideoPath |
| }); |
|
|
| if (response.status === 'running' && response.job_id) { |
| this.currentJobId = response.job_id; |
| this.logCursor = 0; |
| this.startLogPolling(); |
| return; |
| } |
|
|
| if (response.status === 'success') { |
| this.updateStatus('Inference complete!', 'success'); |
| this.displayResults(response); |
| } else { |
| const errMsg = response.message || 'Unknown error'; |
| this.updateStatus('Inference failed: ' + errMsg, 'error'); |
| if (response.logs && this.logsPre) { |
| this.logsPre.textContent = Array.isArray(response.logs) ? response.logs.join('\n') : response.logs; |
| } |
| } |
| } catch (error) { |
| console.error('SignX: inference failed:', error); |
| this.updateStatus('Inference failed: ' + error.message, 'error'); |
| if (this.logsPre) { |
| this.logsPre.textContent = error.message; |
| } |
| } finally { |
| if (!this.currentJobId) { |
| this.resetRunButton(); |
| this.stopLogPolling(); |
| } |
| } |
| } |
|
|
| startLogPolling() { |
| this.stopLogPolling(); |
| this.pollJobStatus(); |
| this.logPollingTimer = setInterval(() => { |
| this.pollJobStatus(); |
| }, 1200); |
| } |
|
|
| stopLogPolling() { |
| if (this.logPollingTimer) { |
| clearInterval(this.logPollingTimer); |
| this.logPollingTimer = null; |
| } |
| } |
|
|
| async pollJobStatus() { |
| if (!this.currentJobId) { |
| return; |
| } |
|
|
| try { |
| const response = await this.sendPluginMessage('get_job_status', { |
| job_id: this.currentJobId, |
| last_index: this.logCursor |
| }); |
|
|
| if (response.status !== 'success') { |
| throw new Error(response.message || 'Failed to fetch job status'); |
| } |
|
|
| if (Array.isArray(response.logs) && response.logs.length > 0) { |
| this.appendLogs(response.logs); |
| } |
|
|
| if (typeof response.next_index === 'number') { |
| this.logCursor = response.next_index; |
| } else if (response.logs) { |
| this.logCursor += response.logs.length; |
| } |
|
|
| const jobStatus = response.job_status; |
| if (jobStatus === 'success' && response.result) { |
| this.updateStatus('Inference complete!', 'success'); |
| this.displayResults(response.result); |
| this.finishCurrentJob(); |
| } else if (jobStatus === 'error') { |
| const errMsg = response.error_message || 'Inference failed'; |
| this.updateStatus(`Inference failed: ${errMsg}`, 'error'); |
| this.finishCurrentJob(); |
| } else if (jobStatus === 'running') { |
| this.updateStatus('Running inference...', 'loading'); |
| } |
| } catch (error) { |
| console.error('SignX: failed to poll job status', error); |
| this.updateStatus(`Log polling failed: ${error.message}`, 'error'); |
| this.finishCurrentJob(); |
| } |
| } |
|
|
| appendLogs(newLines) { |
| if (!this.logsPre || !newLines) { |
| return; |
| } |
|
|
| const text = Array.isArray(newLines) ? newLines.join('\n') : newLines; |
| if (!text) { |
| return; |
| } |
|
|
| if (!this.logsPre.textContent || this.logsPre.textContent === '(waiting for inference job...)') { |
| this.logsPre.textContent = ''; |
| } |
|
|
| if (this.logsPre.textContent && !this.logsPre.textContent.endsWith('\n')) { |
| this.logsPre.textContent += '\n'; |
| } |
|
|
| this.logsPre.textContent += text + '\n'; |
| this.logsPre.scrollTop = this.logsPre.scrollHeight; |
| } |
|
|
| resetRunButton() { |
| const runButton = document.getElementById('signx-run-inference'); |
| if (runButton) { |
| runButton.disabled = false; |
| runButton.innerHTML = this.runButtonOriginalText || 'Run Inference'; |
| } |
| this.runButtonOriginalText = null; |
| this.isInferring = false; |
| } |
|
|
| finishCurrentJob() { |
| this.stopLogPolling(); |
| this.currentJobId = null; |
| this.resetRunButton(); |
| } |
|
|
| displayResults(data) { |
| const resultsContainer = document.getElementById('signx-results-container'); |
|
|
| if (!resultsContainer) return; |
|
|
| resultsContainer.innerHTML = ''; |
|
|
| if (data.logs && data.logs.length > 0 && this.logsPre && !this.logsPre.textContent) { |
| this.logsPre.textContent = data.logs.join('\n'); |
| } |
|
|
| if (data.output_clean) { |
| resultsContainer.appendChild( |
| this.createResultCard( |
| 'Recognition Result (Gloss Sequence)', |
| this.escapeHtml(data.output_clean) |
| ) |
| ); |
| } |
|
|
| if (data.execution_time) { |
| resultsContainer.appendChild( |
| this.createResultCard( |
| 'Execution Time', |
| `${data.execution_time.toFixed(2)} s` |
| ) |
| ); |
| } |
|
|
| if (data.translation_path) { |
| resultsContainer.appendChild( |
| this.createResultCard( |
| 'Translation File', |
| `<code>${this.escapeHtml(data.translation_path)}</code>` |
| ) |
| ); |
| } |
|
|
| if (data.analysis_images && data.analysis_images.length > 0) { |
| data.analysis_images.forEach(file => { |
| const card = document.createElement('div'); |
| card.className = 'signx-result-section'; |
| if (file.type === 'keyframes') { |
| card.classList.add('full-width'); |
| } |
|
|
| let body = ''; |
| if (file.type === 'image') { |
| body = ` |
| <img src="${file.url}" |
| alt="${file.name}" |
| onclick="window.open('${file.url}', '_blank')" |
| style="cursor: pointer; max-width: 100%; border-radius: 6px;"> |
| `; |
| } else if (file.type === 'html') { |
| body = ` |
| <a href="${file.url}" target="_blank" class="signx-link-button"> |
| Open interactive visualization → |
| </a> |
| `; |
| } else if (file.type === 'keyframes') { |
| const previews = (file.previews || []).map(preview => ` |
| <div class="signx-keyframe-preview"> |
| <img src="${preview.url}" alt="${preview.name}" |
| onclick="window.open('${preview.url}', '_blank')" |
| style="cursor: pointer;"> |
| <span>${preview.name}</span> |
| </div> |
| `).join(''); |
| body = ` |
| <div class="signx-keyframes-grid"> |
| ${previews || '<span>No keyframes available.</span>'} |
| </div> |
| `; |
| } |
|
|
| card.innerHTML = ` |
| <h4>${file.name}</h4> |
| <div class="signx-result-content"> |
| ${body} |
| </div> |
| `; |
| resultsContainer.appendChild(card); |
| }); |
| } |
|
|
| if (data.analysis_dir) { |
| resultsContainer.appendChild( |
| this.createResultCard( |
| 'Full Analysis Path', |
| `<code>${this.escapeHtml(data.analysis_dir)}</code>`, |
| { fullWidth: true } |
| ) |
| ); |
| } |
| } |
|
|
| updateStatus(message, type = 'info') { |
| const statusBox = document.getElementById('signx-status'); |
| if (!statusBox) return; |
|
|
| statusBox.className = 'signx-status ' + type; |
|
|
| if (type === 'loading') { |
| statusBox.innerHTML = `<span class="spinner"></span>${message}`; |
| } else { |
| statusBox.textContent = message; |
| } |
| } |
|
|
| escapeHtml(text) { |
| const div = document.createElement('div'); |
| div.textContent = text; |
| return div.innerHTML; |
| } |
|
|
| |
| |
| |
| async sendPluginMessage(action, data) { |
| if (!window.controlWaveWebSocket || !window.controlWaveWebSocket.isConnected) { |
| throw new Error('WebSocket not connected'); |
| } |
|
|
| console.log(`SignX: sending request action=${action}`, data); |
|
|
| const response = await window.controlWaveWebSocket.sendRequest('signx', { |
| action: action, |
| data: data |
| }); |
|
|
| console.log('SignX: received response', response); |
| return response; |
| } |
|
|
| createResultCard(title, contentHtml, options = {}) { |
| const card = document.createElement('div'); |
| card.className = 'signx-result-section'; |
| if (options.fullWidth) { |
| card.classList.add('full-width'); |
| } |
| card.innerHTML = ` |
| <h4>${title}</h4> |
| <div class="signx-result-content">${contentHtml}</div> |
| `; |
| return card; |
| } |
| } |
|
|