From dd6c84e67f733929a3e710265c09e55573018782 Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Sat, 2 Aug 2025 15:13:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E4=BF=AE=E6=94=B9=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E8=AF=8D=E5=92=8C=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WmsImageRecognitionController.java | 10 +- .../com/klp/domain/vo/ImageRecognitionVo.java | 21 ++++ .../impl/ImageRecognitionServiceImpl.java | 99 +++++++++++++++++-- .../com/klp/test/ImageRecognitionTest.java | 7 ++ 4 files changed, 122 insertions(+), 15 deletions(-) diff --git a/klp-wms/src/main/java/com/klp/controller/WmsImageRecognitionController.java b/klp-wms/src/main/java/com/klp/controller/WmsImageRecognitionController.java index a65bb356..82cfa0e0 100644 --- a/klp-wms/src/main/java/com/klp/controller/WmsImageRecognitionController.java +++ b/klp-wms/src/main/java/com/klp/controller/WmsImageRecognitionController.java @@ -111,11 +111,11 @@ public class WmsImageRecognitionController extends BaseController { if (imgUrl == null || imgUrl.trim().isEmpty()) { return R.fail("图片URL不能为空"); } - + ImageRecognitionBo bo = new ImageRecognitionBo(); bo.setImageUrl(imgUrl); bo.setRecognitionType("text"); - + ImageRecognitionVo result = iImageRecognitionService.recognizeText(bo); return R.ok(result); } @@ -129,12 +129,12 @@ public class WmsImageRecognitionController extends BaseController { if (imgUrl == null || imgUrl.trim().isEmpty()) { return R.fail("图片URL不能为空"); } - + ImageRecognitionBo bo = new ImageRecognitionBo(); bo.setImageUrl(imgUrl); bo.setRecognitionType("bom"); - + ImageRecognitionVo result = iImageRecognitionService.recognizeBom(bo); return R.ok(result); } -} \ No newline at end of file +} diff --git a/klp-wms/src/main/java/com/klp/domain/vo/ImageRecognitionVo.java b/klp-wms/src/main/java/com/klp/domain/vo/ImageRecognitionVo.java index 36429503..bbf0a03a 100644 --- a/klp-wms/src/main/java/com/klp/domain/vo/ImageRecognitionVo.java +++ b/klp-wms/src/main/java/com/klp/domain/vo/ImageRecognitionVo.java @@ -49,6 +49,11 @@ public class ImageRecognitionVo implements Serializable { */ private List bomItems; + /** + * 识别结果属性列表 + */ + private List attributes; + /** * 识别置信度 */ @@ -110,4 +115,20 @@ public class ImageRecognitionVo implements Serializable { */ private String remark; } + + /** + * 属性信息 + */ + @Data + public static class AttributeVo { + /** + * 属性名称 + */ + private String attrKey; + + /** + * 属性值 + */ + private String attrValue; + } } \ No newline at end of file diff --git a/klp-wms/src/main/java/com/klp/service/impl/ImageRecognitionServiceImpl.java b/klp-wms/src/main/java/com/klp/service/impl/ImageRecognitionServiceImpl.java index 60fd988a..f011bca3 100644 --- a/klp-wms/src/main/java/com/klp/service/impl/ImageRecognitionServiceImpl.java +++ b/klp-wms/src/main/java/com/klp/service/impl/ImageRecognitionServiceImpl.java @@ -114,7 +114,7 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService { result.setRecognitionType("bom"); result.setRecognizedText(aiResponse); - // 解析BOM信息 + // 解析识别结果 try { Map structuredResult = parseBomResponse(aiResponse); result.setStructuredResult(structuredResult); @@ -123,8 +123,12 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService { List bomItems = extractBomItems(structuredResult); result.setBomItems(bomItems); + // 提取属性列表 + List attributes = extractAttributes(structuredResult); + result.setAttributes(attributes); + } catch (Exception e) { - log.warn("解析BOM响应失败: {}", e.getMessage()); + log.warn("解析识别响应失败: {}", e.getMessage()); result.setRecognizedText(aiResponse); } @@ -341,13 +345,13 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService { */ private String buildBomPrompt(ImageRecognitionBo bo) { StringBuilder prompt = new StringBuilder(); - prompt.append("请仔细分析这张图片中的BOM(物料清单)信息,并提取以下内容:\n\n"); + prompt.append("请仔细分析这张图片中的内容,并提取所有相关信息:\n\n"); prompt.append("【识别要求】\n"); - prompt.append("1. 识别图片中的所有物料信息,包括原材料ID、名称、数量、单位等\n"); - prompt.append("2. 如果图片中包含表格,请按表格结构提取信息\n"); - prompt.append("3. 如果图片中包含列表,请按列表格式提取信息\n"); - prompt.append("4. 确保数量信息的准确性,包括数字和单位\n"); - prompt.append("5. 识别规格、备注等附加信息\n\n"); + prompt.append("1. 如果图片包含BOM(物料清单)信息,请提取原材料ID、名称、数量、单位等\n"); + prompt.append("2. 如果图片包含其他类型的信息(如检验报告、产品信息等),请提取所有相关属性\n"); + prompt.append("3. 识别图片中的所有文字信息,包括标题、内容、表格数据等\n"); + prompt.append("4. 提取关键信息点,如产品名称、规格参数、检验结果等\n"); + prompt.append("5. 确保信息的准确性和完整性\n\n"); if (bo.getProductId() != null) { prompt.append("【产品信息】\n"); @@ -372,10 +376,16 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService { prompt.append(" \"remark\": \"备注\"\n"); prompt.append(" }\n"); prompt.append(" ],\n"); - prompt.append(" \"summary\": \"BOM清单总结\",\n"); + prompt.append(" \"attributes\": [\n"); + prompt.append(" {\n"); + prompt.append(" \"attrKey\": \"属性名称\",\n"); + prompt.append(" \"attrValue\": \"属性值\"\n"); + prompt.append(" }\n"); + prompt.append(" ],\n"); + prompt.append(" \"summary\": \"内容总结\",\n"); prompt.append(" \"totalItems\": 总项目数\n"); prompt.append("}\n\n"); - prompt.append("如果图片中没有BOM信息,请返回空数组。"); + prompt.append("请将识别到的所有信息整理成属性数组,每个属性包含attrKey(属性名称)和attrValue(属性值)。"); return prompt.toString(); } @@ -487,4 +497,73 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService { return bomItems; } + + /** + * 提取属性列表 + */ + private List extractAttributes(Map structuredResult) { + List attributes = new ArrayList<>(); + + try { + List> attrList = (List>) structuredResult.get("attributes"); + if (attrList != null) { + for (Map attr : attrList) { + ImageRecognitionVo.AttributeVo attribute = new ImageRecognitionVo.AttributeVo(); + attribute.setAttrKey((String) attr.get("attrKey")); + attribute.setAttrValue((String) attr.get("attrValue")); + attributes.add(attribute); + } + } + + // 如果没有attributes字段,尝试从其他字段生成属性 + if (attributes.isEmpty()) { + attributes = generateAttributesFromResult(structuredResult); + } + } catch (Exception e) { + log.warn("提取属性失败: {}", e.getMessage()); + } + + return attributes; + } + + /** + * 从识别结果生成属性列表 + */ + private List generateAttributesFromResult(Map structuredResult) { + List attributes = new ArrayList<>(); + + try { + // 提取summary作为内容总结 + String summary = (String) structuredResult.get("summary"); + if (summary != null && !summary.isEmpty()) { + ImageRecognitionVo.AttributeVo summaryAttr = new ImageRecognitionVo.AttributeVo(); + summaryAttr.setAttrKey("内容总结"); + summaryAttr.setAttrValue(summary); + attributes.add(summaryAttr); + } + + // 提取totalItems + Object totalItems = structuredResult.get("totalItems"); + if (totalItems != null) { + ImageRecognitionVo.AttributeVo totalAttr = new ImageRecognitionVo.AttributeVo(); + totalAttr.setAttrKey("总项目数"); + totalAttr.setAttrValue(String.valueOf(totalItems)); + attributes.add(totalAttr); + } + + // 提取bomItems数量 + List> bomItems = (List>) structuredResult.get("bomItems"); + if (bomItems != null && !bomItems.isEmpty()) { + ImageRecognitionVo.AttributeVo bomCountAttr = new ImageRecognitionVo.AttributeVo(); + bomCountAttr.setAttrKey("BOM项目数"); + bomCountAttr.setAttrValue(String.valueOf(bomItems.size())); + attributes.add(bomCountAttr); + } + + } catch (Exception e) { + log.warn("生成属性失败: {}", e.getMessage()); + } + + return attributes; + } } \ No newline at end of file diff --git a/klp-wms/src/main/java/com/klp/test/ImageRecognitionTest.java b/klp-wms/src/main/java/com/klp/test/ImageRecognitionTest.java index b60ee46d..25b646df 100644 --- a/klp-wms/src/main/java/com/klp/test/ImageRecognitionTest.java +++ b/klp-wms/src/main/java/com/klp/test/ImageRecognitionTest.java @@ -104,6 +104,13 @@ public class ImageRecognitionTest implements CommandLineRunner { item.getSpecification()); } } + + if (result.getAttributes() != null) { + log.info("属性数量: {}", result.getAttributes().size()); + for (ImageRecognitionVo.AttributeVo attr : result.getAttributes()) { + log.info("属性: {} = {}", attr.getAttrKey(), attr.getAttrValue()); + } + } } catch (Exception e) { log.error("BOM识别测试异常", e); }