/**
* AI图像识别集成模块
* 在OpenClaw环境中调用AI能力进行图片识别
*/
/**
* 通过OpenClaw AI分析图片
*
* @param {string} imagePath - 图片绝对路径
* @param {string} question - 提问内容
* @param {Object} options - 配置选项
* @returns {Promise<Object>} 分析结果
*/
async function analyzeImageWithAI(imagePath, question = "请描述这张图片的内容", options = {}) {
try {
console.log(`📸 AI分析图片: ${imagePath}`);
console.log(`提问: ${question}`);
// 验证图片存在
const fs = await import('fs');
if (!fs.existsSync(imagePath)) {
throw new Error(`图片不存在: ${imagePath}`);
}
// 获取图片基本信息
const stats = fs.statSync(imagePath);
const fileSize = stats.size;
console.log(`图片大小: ${(fileSize / 1024).toFixed(2)} KB`);
// 在OpenClaw环境中,我们可以直接通过系统与AI交互
// 这里返回调用结构,实际识别由外部agent完成
return {
success: true,
imagePath: imagePath,
fileSize: fileSize,
question: question,
status: 'recognition_requested',
message: '请在OpenClaw agent环境中调用此函数,AI将直接分析图片'
};
} catch (error) {
console.error(`❌ AI分析失败: ${error.message}`);
return {
success: false,
error: error.message,
imagePath: imagePath,
question: question
};
}
}
/**
* 批量分析图片
*
* @param {Array<string>} imagePaths - 图片路径数组
* @param {string} question - 提问内容
* @returns {Promise<Array<Object>>} 分析结果数组
*/
async function batchAnalyzeImages(imagePaths, question) {
const results = [];
for (const imagePath of imagePaths) {
const result = await analyzeImageWithAI(imagePath, question);
results.push(result);
}
return results;
}
/**
* 分析图片并提取结构化信息
*
* @param {string} imagePath - 图片路径
* @param {Array<string>} fields - 要提取的字段
* @returns {Promise<Object>} 结构化数据
*/
async function extractImageData(imagePath, fields = []) {
try {
console.log(`🔍 提取图片结构化数据: ${imagePath}`);
// 构建提问
let question = "请详细分析这张图片,并提取以下信息:";
if (fields.length > 0) {
fields.forEach(field => {
question += `\n- ${field}`;
});
} else {
question += "\n- 图片中的主要物体和元素";
question += "\n- 场景描述";
question += "\n- 颜色和构图";
question += "\n- 任何文字或数字信息";
}
return await analyzeImageWithAI(imagePath, question);
} catch (error) {
console.error(`❌ 数据提取失败: ${error.message}`);
return {
success: false,
error: error.message
};
}
}
// 导出函数
export {
analyzeImageWithAI,
batchAnalyzeImages,
extractImageData
};
export default {
analyzeImageWithAI,
batchAnalyzeImages,
extractImageData
};