File size: 1,618 Bytes
c594524
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```javascript
const express = require('express');
const router = express.Router();
const multer = require('multer');
const { createWorker } = require('tesseract.js');
const { pipeline } = require('stream/promises');
const FormData = require('form-data');
const fs = require('fs');
const axios = require('axios');

const upload = multer({ dest: 'uploads/' });
const worker = createWorker();

// Initialize Tesseract worker
(async () => {
    await worker.load();
    await worker.loadLanguage('eng');
    await worker.initialize('eng');
})();

router.post('/analyze', upload.single('image'), async (req, res) => {
    try {
        // 1. Perform OCR on the image first
        const { data: { text } } = await worker.recognize(req.file.path);
        
        // 2. Send to LLM for analysis (using LLaMA.cpp as example)
        const form = new FormData();
        form.append('image', fs.createReadStream(req.file.path));
        form.append('text_context', text);
        
        const llmResponse = await axios.post('http://localhost:8080/analyze-image', form, {
            headers: form.getHeaders()
        });

        // Clean up the uploaded file
        fs.unlinkSync(req.file.path);

        res.json({
            description: llmResponse.data.description,
            items: llmResponse.data.items.map(item => ({
                label: item.label,
                confidence: item.confidence
            }))
        });
    } catch (error) {
        console.error('Error processing image:', error);
        res.status(500).json({ error: 'Error processing image' });
    }
});

module.exports = router;
```