-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_image_processor.js
More file actions
173 lines (135 loc) Β· 5.45 KB
/
debug_image_processor.js
File metadata and controls
173 lines (135 loc) Β· 5.45 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const tf = require('@tensorflow/tfjs-node');
const fs = require('fs');
const path = require('path');
// Debug image processor for LaxCheck
class ImageDebugger {
constructor() {
this.model = null;
this.classNames = [];
}
async loadModel() {
try {
const modelPath = path.join(process.cwd(), 'model/model.json');
const metadataPath = path.join(process.cwd(), 'model/metadata.json');
console.log('π Looking for model at:', modelPath);
console.log('π Model exists:', fs.existsSync(modelPath));
if (!fs.existsSync(modelPath)) {
console.log('β ML model not found');
return false;
}
// Load model
this.model = await tf.loadLayersModel('file://' + modelPath);
// Load class names from metadata
if (fs.existsSync(metadataPath)) {
const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
this.classNames = metadata.labels || [];
console.log('β
ML model loaded with classes:', this.classNames);
} else {
console.log('β οΈ Metadata not found, using default classes');
this.classNames = ['Unknown'];
}
return true;
} catch (error) {
console.log('β Failed to load ML model:', error.message);
return false;
}
}
analyzeImageFormat(imagePath) {
console.log('\nπ Analyzing image format for:', imagePath);
if (!fs.existsSync(imagePath)) {
console.log('β File does not exist');
return false;
}
const stats = fs.statSync(imagePath);
console.log('π File size:', stats.size, 'bytes');
const imageBuffer = fs.readFileSync(imagePath);
const imageHeader = imageBuffer.subarray(0, 16);
console.log('π File header (hex):', imageHeader.toString('hex'));
console.log('π File header (ascii):', imageHeader.toString('ascii').replace(/[^\x20-\x7E]/g, '.'));
// Check image format signatures
const isJPEG = imageHeader[0] === 0xFF && imageHeader[1] === 0xD8;
const isPNG = imageHeader[0] === 0x89 && imageHeader[1] === 0x50 && imageHeader[2] === 0x4E && imageHeader[3] === 0x47;
const isGIF = imageHeader.subarray(0, 3).toString() === 'GIF';
const isBMP = imageHeader[0] === 0x42 && imageHeader[1] === 0x4D;
const isWebP = imageHeader.subarray(0, 4).toString() === 'RIFF' && imageHeader.subarray(8, 12).toString() === 'WEBP';
console.log('π Format detection:');
console.log(' JPEG:', isJPEG);
console.log(' PNG:', isPNG);
console.log(' GIF:', isGIF);
console.log(' BMP:', isBMP);
console.log(' WebP:', isWebP);
return { isJPEG, isPNG, isGIF, isBMP, isWebP, imageBuffer };
}
async testTensorFlowProcessing(imagePath) {
console.log('\nπ§ͺ Testing TensorFlow processing for:', imagePath);
try {
const imageBuffer = fs.readFileSync(imagePath);
// Try TensorFlow decoding
console.log('π Attempting tf.node.decodeImage...');
const decodedImage = tf.node.decodeImage(imageBuffer, 3);
console.log('β
Successfully decoded image');
console.log('π Image shape:', decodedImage.shape);
// Try resizing
console.log('π Attempting resize to 224x224...');
const resized = tf.image.resizeBilinear(decodedImage, [224, 224]);
console.log('β
Successfully resized image');
// Try normalization
console.log('π Attempting normalization...');
const normalized = resized.div(255.0);
console.log('β
Successfully normalized image');
// Try batching
console.log('π Attempting batching...');
const batched = normalized.expandDims(0);
console.log('β
Successfully batched image');
console.log('π Final tensor shape:', batched.shape);
// Cleanup
decodedImage.dispose();
resized.dispose();
normalized.dispose();
batched.dispose();
return true;
} catch (error) {
console.log('β TensorFlow processing failed:', error.message);
return false;
}
}
async analyzeUploadsDirectory() {
console.log('\nπ Analyzing uploads directory...');
const uploadsDir = path.join(process.cwd(), 'uploads');
if (!fs.existsSync(uploadsDir)) {
console.log('β Uploads directory does not exist');
return;
}
const files = fs.readdirSync(uploadsDir);
console.log('π Found', files.length, 'files in uploads directory');
for (const file of files) {
const filePath = path.join(uploadsDir, file);
console.log('\nπ Processing file:', file);
// Analyze format
this.analyzeImageFormat(filePath);
// Test TensorFlow processing
await this.testTensorFlowProcessing(filePath);
}
}
async runFullDiagnostic() {
console.log('π Starting LaxCheck Image Processing Diagnostic\n');
// Check model loading
console.log('π Step 1: Model Loading');
const modelLoaded = await this.loadModel();
if (!modelLoaded) {
console.log('β Cannot proceed without model');
return;
}
// Analyze uploads directory
console.log('\nπ Step 2: Upload Analysis');
await this.analyzeUploadsDirectory();
console.log('\nβ
Diagnostic complete');
}
}
// Create and run debugger
const debugger = new ImageDebugger();
// If called directly, run diagnostic
if (require.main === module) {
debugger.runFullDiagnostic().catch(console.error);
}
module.exports = ImageDebugger;