diff --git a/next.config.js b/next.config.js index 42b6cd2..3792dd4 100644 --- a/next.config.js +++ b/next.config.js @@ -42,4 +42,4 @@ const nextConfig = { }, } -module.exports = nextConfig +module.exports = nextConfig \ No newline at end of file diff --git a/utils/visionUtils.ts b/utils/visionUtils.ts index 2ff87c9..414f9e1 100644 --- a/utils/visionUtils.ts +++ b/utils/visionUtils.ts @@ -1,11 +1,18 @@ import vision from '@google-cloud/vision'; -import path from 'path'; -const client = new vision.ImageAnnotatorClient({ - credentials: process.env.GOOGLE_APPLICATION_CREDENTIALS - ? JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS) - : undefined, -}); +let client: vision.ImageAnnotatorClient; + +try { + const credentials = { + client_email: process.env.EMAIL, + private_key: process.env.PRIVATE_KEY.replace(/\\n/g, '\n'), + }; + client = new vision.ImageAnnotatorClient({ credentials }); +} catch (error) { + console.error('Error parsing Google Cloud credentials:', error); + // Initialize with default credentials as a fallback + client = new vision.ImageAnnotatorClient(); +} export const scanProduct = async (imageData: string): Promise<{ name: string; quantity: number; allLabels: string[] }> => { try { @@ -21,19 +28,31 @@ export const scanProduct = async (imageData: string): Promise<{ name: string; qu const labels = result.labelAnnotations || []; + // Filter out generic labels const specificLabels = labels.filter(label => label.description && !['Food', 'Produce', 'Natural foods', 'Fruit', 'Vegetable'].includes(label.description) ); if (specificLabels.length > 0) { - const productName = specificLabels[0]?.description || 'Unknown Product'; + // Use the most confident specific label + const productName = specificLabels[0].description || 'Unknown Product'; - console.log('All labels:', labels.map(l => `${l.description || 'Unknown'} (${l.score || 'N/A'})`).join(', ')); + // Log all labels for debugging + console.log('All labels:', labels.map(l => `${l.description} (${l.score})`).join(', ')); - return { name: productName, quantity: 1, allLabels: labels.map(label => label.description || '').filter(Boolean) }; + return { + name: productName, + quantity: 1, + allLabels: labels.map(label => label.description || '').filter(Boolean) + }; } else if (labels.length > 0) { - const productName = labels[0]?.description || 'Unknown Product'; - return { name: productName, quantity: 1, allLabels: labels.map(label => label.description || '').filter(Boolean) }; + // If no specific labels, use the most confident general label + const productName = labels[0].description || 'Unknown Product'; + return { + name: productName, + quantity: 1, + allLabels: labels.map(label => label.description || '').filter(Boolean) + }; } else { throw new Error('No labels detected in the image'); } @@ -41,4 +60,4 @@ export const scanProduct = async (imageData: string): Promise<{ name: string; qu console.error('Error in scanProduct:', error); throw new Error(`Failed to scan product: ${error instanceof Error ? error.message : 'Unknown error'}`); } -}; +}; \ No newline at end of file