package com.klp.test; import com.klp.domain.bo.ImageRecognitionBo; import com.klp.domain.vo.ImageRecognitionVo; import com.klp.service.IImageRecognitionService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * 图片识别功能测试类 * * @author klp * @date 2025-01-27 */ @Slf4j @Component @RequiredArgsConstructor public class ImageRecognitionTest implements CommandLineRunner { private final IImageRecognitionService imageRecognitionService; @Override public void run(String... args) throws Exception { log.info("开始测试图片识别功能..."); // 测试AI连接 testAiConnection(); // 测试文字识别 testTextRecognition(); // 测试BOM识别 testBomRecognition(); log.info("图片识别功能测试完成"); } /** * 测试AI连接 */ private void testAiConnection() { log.info("测试AI连接..."); try { Map result = imageRecognitionService.testAiConnection(); if (Boolean.TRUE.equals(result.get("success"))) { log.info("AI连接测试成功: {}", result.get("message")); } else { log.error("AI连接测试失败: {}", result.get("message")); } } catch (Exception e) { log.error("AI连接测试异常", e); } } /** * 测试文字识别 */ private void testTextRecognition() { log.info("测试文字识别..."); try { ImageRecognitionBo bo = new ImageRecognitionBo(); bo.setImageUrl("https://via.placeholder.com/400x300/000000/FFFFFF?text=Test+Text"); bo.setRecognitionType("text"); bo.setEnableVoting(false); ImageRecognitionVo result = imageRecognitionService.recognizeText(bo); log.info("文字识别结果: {}", result.getRecognizedText()); log.info("识别状态: {}", result.getStatus()); log.info("处理时间: {}ms", result.getProcessingTime()); } catch (Exception e) { log.error("文字识别测试异常", e); } } /** * 测试BOM识别 */ private void testBomRecognition() { log.info("测试BOM识别..."); try { ImageRecognitionBo bo = new ImageRecognitionBo(); bo.setImageUrl("https://via.placeholder.com/400x300/000000/FFFFFF?text=BOM+Test"); bo.setRecognitionType("bom"); bo.setEnableVoting(false); bo.setProductId(1L); ImageRecognitionVo result = imageRecognitionService.recognizeBom(bo); log.info("BOM识别结果: {}", result.getRecognizedText()); log.info("识别状态: {}", result.getStatus()); log.info("处理时间: {}ms", result.getProcessingTime()); if (result.getBomItems() != null) { log.info("BOM项目数量: {}", result.getBomItems().size()); for (ImageRecognitionVo.BomItemVo item : result.getBomItems()) { log.info("BOM项目: {} - {} {} {}", item.getRawMaterialName(), item.getQuantity(), item.getUnit(), 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); } } }