Skip to content

Commit

Permalink
maybe a fix
Browse files Browse the repository at this point in the history
  • Loading branch information
BurhanCantCode committed Aug 3, 2024
1 parent d7ffb83 commit b0c543a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ const nextConfig = {
},
}

module.exports = nextConfig
module.exports = nextConfig
43 changes: 31 additions & 12 deletions utils/visionUtils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,24 +28,36 @@ 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');
}
} catch (error: unknown) {
console.error('Error in scanProduct:', error);
throw new Error(`Failed to scan product: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
};
};

0 comments on commit b0c543a

Please sign in to comment.