-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_processor.js
97 lines (80 loc) · 2.49 KB
/
image_processor.js
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
// image_processor.js
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const https = require('https');
const crypto = require('crypto');
async function downloadImage(url, tempDir) {
return new Promise((resolve, reject) => {
const fileName = crypto.randomBytes(16).toString('hex') + '.jpg';
const filePath = path.join(tempDir, fileName);
const file = fs.createWriteStream(filePath);
https.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to download image: ${response.statusCode}`));
return;
}
response.pipe(file);
file.on('finish', () => {
file.close();
resolve(filePath);
});
}).on('error', (err) => {
fs.unlink(filePath, () => {});
reject(err);
});
});
}
async function extractTextFromImage(imageUrl) {
const tempDir = path.join(__dirname, 'temp');
// Create temp directory if it doesn't exist
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}
try {
// Download the image
const imagePath = await downloadImage(imageUrl, tempDir);
// Run Python script
const result = await new Promise((resolve, reject) => {
const pythonScript = spawn('python3', [
path.join(__dirname, 'gemini_processor.py'),
imagePath
]);
let outputData = '';
let errorData = '';
pythonScript.stdout.on('data', (data) => {
outputData += data.toString();
});
pythonScript.stderr.on('data', (data) => {
errorData += data.toString();
});
pythonScript.on('close', (code) => {
// Clean up downloaded image
fs.unlink(imagePath, () => {});
if (code !== 0) {
reject(new Error(`Python script failed: ${errorData}`));
return;
}
// Extract the actual text from the output
const match = outputData.match(/Extracted Text:\n([\s\S]*)/);
resolve(match ? match[1].trim() : '');
});
});
return result;
} catch (error) {
throw new Error(`Failed to process image: ${error.message}`);
} finally {
// Clean up temp directory
if (fs.existsSync(tempDir)) {
fs.readdirSync(tempDir).forEach(file => {
const filePath = path.join(tempDir, file);
try {
fs.unlinkSync(filePath);
} catch (err) {
console.error(`Failed to delete temp file ${filePath}:`, err);
}
});
}
}
}
module.exports = extractTextFromImage;