|
|
@@ -1,6 +1,6 @@
|
|
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
|
|
import * as onnx from 'onnxruntime-node';
|
|
|
-import sharp from 'sharp';
|
|
|
+import { Jimp } from 'jimp';
|
|
|
import * as path from 'path';
|
|
|
import { MPOB_CLASSES, HEALTH_ALERT_CLASSES } from '../constants/mpob-standards';
|
|
|
import { DetectionResult } from '../interfaces/palm-analysis.interface';
|
|
|
@@ -29,26 +29,21 @@ export class ScannerProvider implements OnModuleInit {
|
|
|
* Preprocesses the image buffer: resize to 640x640, transpose HWC to CHW, and normalize.
|
|
|
*/
|
|
|
async preprocess(imageBuffer: Buffer): Promise<onnx.Tensor> {
|
|
|
- // Proper Sharp RGB extraction
|
|
|
- const resized = await sharp(imageBuffer)
|
|
|
- .resize(640, 640, { fit: 'fill' })
|
|
|
- .removeAlpha()
|
|
|
- .raw()
|
|
|
- .toBuffer({ resolveWithObject: true });
|
|
|
-
|
|
|
- const { width, height, channels } = resized.info;
|
|
|
- const pixels = resized.data; // Uint8Array [R, G, B, R, G, B...]
|
|
|
+ const img = await Jimp.read(imageBuffer);
|
|
|
+ img.resize({ w: 640, h: 640 });
|
|
|
|
|
|
+ const pixels = img.bitmap.data; // RGBA: [R, G, B, A, R, G, B, A, ...]
|
|
|
+ const width = img.width;
|
|
|
+ const height = img.height;
|
|
|
const imageSize = width * height;
|
|
|
const floatData = new Float32Array(3 * imageSize);
|
|
|
|
|
|
- // HWC to CHW Transposition
|
|
|
- // pixels: [R1, G1, B1, R2, G2, B2...]
|
|
|
+ // HWC (RGBA) to CHW (RGB) transposition — stride 4, skip alpha
|
|
|
// floatData: [R1, R2, ..., G1, G2, ..., B1, B2, ...]
|
|
|
for (let i = 0; i < imageSize; i++) {
|
|
|
- floatData[i] = pixels[i * 3] / 255.0; // R
|
|
|
- floatData[i + imageSize] = pixels[i * 3 + 1] / 255.0; // G
|
|
|
- floatData[i + 2 * imageSize] = pixels[i * 3 + 2] / 255.0; // B
|
|
|
+ floatData[i] = pixels[i * 4] / 255.0; // R
|
|
|
+ floatData[i + imageSize] = pixels[i * 4 + 1] / 255.0; // G
|
|
|
+ floatData[i + 2 * imageSize] = pixels[i * 4 + 2] / 255.0; // B
|
|
|
}
|
|
|
|
|
|
return new onnx.Tensor('float32', floatData, [1, 3, 640, 640]);
|