82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
/**
|
||
* Test Vision Extractor on Carrier Images
|
||
*/
|
||
require('dotenv').config();
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
|
||
const { extractProductFeatures, buildGoldenDescription } = require('./lib/vision-extractor');
|
||
|
||
// R2客户端配置
|
||
const r2Client = new S3Client({
|
||
region: 'auto',
|
||
endpoint: `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
|
||
credentials: {
|
||
accessKeyId: process.env.R2_ACCESS_KEY_ID,
|
||
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY,
|
||
},
|
||
forcePathStyle: true,
|
||
});
|
||
|
||
async function uploadToR2(filePath) {
|
||
const buffer = fs.readFileSync(filePath);
|
||
const fileName = `vision-test-${path.basename(filePath)}`;
|
||
|
||
console.log(` 上传 ${path.basename(filePath)}...`);
|
||
|
||
await r2Client.send(new PutObjectCommand({
|
||
Bucket: process.env.R2_BUCKET_NAME || 'ai-flow',
|
||
Key: fileName,
|
||
Body: buffer,
|
||
ContentType: 'image/jpeg',
|
||
}));
|
||
|
||
return process.env.R2_PUBLIC_DOMAIN
|
||
? `${process.env.R2_PUBLIC_DOMAIN}/${fileName}`
|
||
: `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com/${process.env.R2_BUCKET_NAME}/${fileName}`;
|
||
}
|
||
|
||
async function main() {
|
||
const materialDir = path.join(__dirname, '素材/登机包');
|
||
|
||
// 选择几张代表性图片
|
||
// 假设 P1191464.JPG 是主图,1023_0151.JPG 是细节图
|
||
const targetFiles = ['P1191464.JPG', '1023_0151.JPG'];
|
||
|
||
for (const file of targetFiles) {
|
||
const filePath = path.join(materialDir, file);
|
||
if (!fs.existsSync(filePath)) {
|
||
console.log(`❌ 文件不存在: ${file}`);
|
||
continue;
|
||
}
|
||
|
||
console.log(`\n🔍 分析图片: ${file}`);
|
||
|
||
try {
|
||
// 1. 上传图片获取URL
|
||
const imageUrl = await uploadToR2(filePath);
|
||
console.log(` URL: ${imageUrl}`);
|
||
|
||
// 2. 调用Vision提取特征
|
||
const result = await extractProductFeatures(imageUrl);
|
||
|
||
if (result) {
|
||
console.log('\n📋 提取结果:');
|
||
console.log(JSON.stringify(result, null, 2));
|
||
|
||
console.log('\n✨ Golden Description:');
|
||
console.log(buildGoldenDescription(result, 'pet travel carrier'));
|
||
}
|
||
|
||
} catch (e) {
|
||
console.error(`❌ 分析失败: ${e.message}`);
|
||
}
|
||
}
|
||
}
|
||
|
||
main().catch(console.error);
|
||
|
||
|
||
|
||
|