feat: optimize scan OCR with sharp preprocessing and 1024px resizing
This commit is contained in:
@@ -6,6 +6,7 @@ import { createClient } from '@/lib/supabase/server';
|
||||
import { createHash } from 'crypto';
|
||||
import { trackApiUsage } from '@/services/track-api-usage';
|
||||
import { checkCreditBalance, deductCredits } from '@/services/credit-service';
|
||||
import sharp from 'sharp';
|
||||
|
||||
// Native Schema Definition for Gemini API
|
||||
const metadataSchema = {
|
||||
@@ -100,14 +101,23 @@ export async function scanLabel(input: any): Promise<AnalysisResponse> {
|
||||
};
|
||||
}
|
||||
|
||||
// Step 3: Base64 Encoding
|
||||
// Step 3: AI-Specific Image Optimization (Grayscale, Normalize, Resize)
|
||||
const startOptimization = performance.now();
|
||||
const optimizedBuffer = await sharp(buffer)
|
||||
.resize(1024, 1024, { fit: 'inside', withoutEnlargement: true })
|
||||
.grayscale()
|
||||
.normalize()
|
||||
.toBuffer();
|
||||
const endOptimization = performance.now();
|
||||
|
||||
// Step 4: Base64 Encoding
|
||||
const startEncoding = performance.now();
|
||||
const base64Data = buffer.toString('base64');
|
||||
const mimeType = file.type || 'image/webp';
|
||||
const uploadSize = buffer.length;
|
||||
const base64Data = optimizedBuffer.toString('base64');
|
||||
const mimeType = 'image/webp';
|
||||
const uploadSize = optimizedBuffer.length;
|
||||
const endEncoding = performance.now();
|
||||
|
||||
// Step 4: Model Initialization & Step 5: API Call
|
||||
// Step 5: Model Initialization & Step 6: API Call
|
||||
const startAiTotal = performance.now();
|
||||
let jsonData;
|
||||
let validatedData;
|
||||
@@ -164,6 +174,7 @@ export async function scanLabel(input: any): Promise<AnalysisResponse> {
|
||||
data: validatedData,
|
||||
perf: {
|
||||
imagePrep: endImagePrep - startImagePrep,
|
||||
optimization: endOptimization - startOptimization,
|
||||
cacheCheck: endCacheCheck - startCacheCheck,
|
||||
encoding: endEncoding - startEncoding,
|
||||
modelInit: endModelInit - startModelInit,
|
||||
|
||||
Reference in New Issue
Block a user