-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_image_processing.js
More file actions
124 lines (105 loc) · 4.77 KB
/
Copy pathtest_image_processing.js
File metadata and controls
124 lines (105 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const net = require('net');
const fs = require('fs');
const path = require('path');
async function testImageProcessing() {
console.log('🧪 IMAGE PROCESSING TEST');
console.log('='.repeat(50));
// Connect to server socket
const client = net.createConnection('/tmp/rkllm.sock');
await new Promise(resolve => client.on('connect', resolve));
console.log('✅ Connected to server');
let id = 1;
function send(method, params, timeout = 5000) {
return new Promise((resolve, reject) => {
const req = {jsonrpc: '2.0', method, params, id: id++};
console.log(`📤 ${method}`);
const timer = setTimeout(() => {
reject(new Error(`Timeout waiting for response to ${method}`));
}, timeout);
function handleData(data) {
try {
const lines = data.toString().split('\\n').filter(l => l.trim());
for (const line of lines) {
try {
const resp = JSON.parse(line);
if (resp.id === req.id) {
clearTimeout(timer);
client.removeListener('data', handleData);
resolve(resp);
return;
}
} catch(e) {
console.log('Parse error:', e.message, 'Line:', line);
}
}
} catch(e) {
console.log('Data handling error:', e.message);
}
}
client.on('data', handleData);
client.write(JSON.stringify(req) + '\\n');
});
}
try {
// Test 1: Initialize image processor (without actual model for now)
console.log('\\n🔧 1. Testing image processor initialization...');
const initResp = await send('image.init_processor', {
model_path: './models/qwen2vl2b/Qwen2-VL-2B-Instruct.rknn',
core_num: 1
});
if (initResp.error) {
console.log('⚠️ Expected error (no model file):', initResp.error.message);
} else {
console.log('✅ Image processor initialized:', initResp.result);
}
// Test 2: Load and encode demo image to base64
console.log('\\n🖼️ 2. Loading demo.jpg...');
const imagePath = path.join(__dirname, 'tests', 'images', 'demo.jpg');
let imageBase64 = '';
try {
const imageBuffer = fs.readFileSync(imagePath);
imageBase64 = imageBuffer.toString('base64');
console.log(`📊 Image loaded: ${imageBuffer.length} bytes -> ${imageBase64.length} base64 chars`);
} catch (err) {
console.log('❌ Could not load demo.jpg:', err.message);
return;
}
// Test 3: Process image (create small test data instead of actual JPEG)
console.log('\\n🔍 3. Testing image processing API...');
// Create fake RGB data (224x224x3 = 150528 bytes)
const fakeRgbSize = 224 * 224 * 3;
const fakeRgbData = Buffer.alloc(fakeRgbSize, 128); // Gray image
const fakeBase64 = fakeRgbData.toString('base64');
const processResp = await send('image.process', {
image_data: fakeBase64
}, 15000); // Increase timeout to 15 seconds
if (processResp.error) {
console.log('⚠️ Expected error (no model initialized):', processResp.error.message);
} else {
console.log('✅ Image processed:', {
embedding_size: processResp.result.embedding_size,
n_image_tokens: processResp.result.n_image_tokens,
embed_dim: processResp.result.embed_dim
});
}
// Test 4: Cleanup
console.log('\\n🧹 4. Testing cleanup...');
const cleanupResp = await send('image.cleanup_processor');
if (cleanupResp.error) {
console.log('❌ Cleanup error:', cleanupResp.error.message);
} else {
console.log('✅ Cleanup successful:', cleanupResp.result);
}
console.log('\\n🎉 Image processing API test completed!');
console.log('\\n📝 Summary:');
console.log(' • Image processing endpoints are available');
console.log(' • JSON-RPC API is working correctly');
console.log(' • Ready for real vision model integration');
} catch (error) {
console.error('❌ Test failed:', error);
} finally {
client.end();
}
}
// Run the test
testImageProcessing().catch(console.error);