From 4e15ae287b7a23ae10df8c3dbbf20d01ce1d5f38 Mon Sep 17 00:00:00 2001 From: haozq <1611483981@qq.com> Date: Wed, 22 Oct 2025 10:16:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AlgorithmService.java | 120 +++++++++++++----- .../service/impl/ImageCaptionServiceImpl.java | 5 +- .../mapper/business/ImageCaptionMapper.xml | 1 + 3 files changed, 90 insertions(+), 36 deletions(-) diff --git a/bonus-business/src/main/java/com/bonus/business/service/impl/AlgorithmService.java b/bonus-business/src/main/java/com/bonus/business/service/impl/AlgorithmService.java index 8def68c..e2d64ff 100644 --- a/bonus-business/src/main/java/com/bonus/business/service/impl/AlgorithmService.java +++ b/bonus-business/src/main/java/com/bonus/business/service/impl/AlgorithmService.java @@ -7,16 +7,22 @@ import com.bonus.business.domain.ImageRecognize; import com.bonus.file.config.SysFile; import lombok.extern.slf4j.Slf4j; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.*; @Service @Slf4j public class AlgorithmService { + + public static final String setContentType="application/json"; /** * 请求路径 */ @@ -33,41 +39,64 @@ public class AlgorithmService { //图片进行标注 public List getImageList(List fileList, String operaName) { List list=new ArrayList<>(); + Map entityMaps=new HashMap<>(); try{ - /** - * 数据存储 - */ List imageList=new ArrayList<>(); for (SysFile sysFile : fileList) { imageList.add(sysFile.getUrl()); + entityMaps.put(sysFile.getUrl(),sysFile); } List types= Arrays.asList(operaName.split(",")); - CloseableHttpClient client = HttpClients.createDefault(); - HttpPost httpPost = new HttpPost(URL+IMAGE_TYPE); Map map=new HashMap<>(); map.put("images",imageList); map.put("types",types); map.put("return_image",true); String json= JSON.toJSONString(map); - String responseBody = client.execute(httpPost, httpResponse -> EntityUtils.toString(httpResponse.getEntity())); - log.info("数据回调返回-->"); - log.info(responseBody); - StringEntity entity = new StringEntity(json); - httpPost.setEntity(entity); - httpPost.setHeader("Accept", "application/json"); - httpPost.setHeader("Content-type", "application/json"); + JSONObject jsonObject= javaHttpPost(IMAGE_TYPE,json); + String code=jsonObject.getString("code"); + if(code.equals("200")){ + JSONArray array=jsonObject.getJSONArray("data"); + for (int i=0;i map=new HashMap<>(); map.put("images",imageList); String json= JSON.toJSONString(map); - - StringEntity entity = new StringEntity(json); - httpPost.setEntity(entity); - httpPost.setHeader("Accept", "application/json"); - httpPost.setHeader("Content-type", "application/json"); - String responseBody = client.execute(httpPost, httpResponse -> EntityUtils.toString(httpResponse.getEntity())); - JSONObject jsonObject=JSON.parseObject(responseBody); + JSONObject jsonObject= javaHttpPost(IMAGE_RECOGNIZE,json); String code=jsonObject.getString("code"); //接口通了 if(code.equals("200")){ @@ -111,20 +132,51 @@ public class AlgorithmService { SysFile sysFile=entityMaps.get(path); //综合得分 vo.setOverallScore(score); - sysFile.setType(operaName); + if(score==null){ + vo.setOverallScore("0"); + } + vo.setType("2"); + vo.setContentImage(operaName); + vo.setImageId(sysFile.getId()); + vo.setImagePath(sysFile.getUrl()); + list.add(vo); + } + }else{ + // 识别失败 + for (SysFile sysFile : fileList) { + ImageRecognize vo = new ImageRecognize(); + vo.setOverallScore("0"); + vo.setType("2"); vo.setContentImage(operaName); vo.setImageId(sysFile.getId()); vo.setImagePath(sysFile.getUrl()); list.add(vo); } } - log.info("图像评估识别接口调用范湖-->"); - log.info(responseBody); }catch (Exception e){ log.error(e.getMessage(),e); } return list; + } + + + //调用第三方接口 + public JSONObject javaHttpPost(String path, String json) throws IOException { + CloseableHttpClient client = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(URL+path); + StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8); + entity.setContentType(setContentType); + httpPost.setEntity(entity); + httpPost.setHeader("Accept", setContentType); + httpPost.setHeader("Content-type", setContentType); + String responseBody = client.execute(httpPost, httpResponse -> EntityUtils.toString(httpResponse.getEntity())); + log.info("接口调用返回-->"); + log.info(responseBody); + return JSON.parseObject(responseBody); + } + + } diff --git a/bonus-business/src/main/java/com/bonus/business/service/impl/ImageCaptionServiceImpl.java b/bonus-business/src/main/java/com/bonus/business/service/impl/ImageCaptionServiceImpl.java index c74b5ce..94b9895 100644 --- a/bonus-business/src/main/java/com/bonus/business/service/impl/ImageCaptionServiceImpl.java +++ b/bonus-business/src/main/java/com/bonus/business/service/impl/ImageCaptionServiceImpl.java @@ -63,7 +63,8 @@ public class ImageCaptionServiceImpl implements ImageCaptionService { if(StringUtils.isEmpty(vo.getParam())){ return AjaxResult.error("请至少选择一个算法!"); } - String userId= SecurityUtils.getUserId().toString(); + String userId= "1"; + // String userId= SecurityUtils.getUserId().toString(); String operaName=vo.getParam(); //操作日期 List fileList=new ArrayList<>(); @@ -177,7 +178,7 @@ public class ImageCaptionServiceImpl implements ImageCaptionService { // 批量操作的数据容器 for (ImageRecognize imageRecognize : list) { // 类型判断与计数,使用工具类确保空字符串也被视为无类型 - if (StringUtils.isBlank(imageRecognize.getType())) { + if (StringUtils.isBlank(imageRecognize.getContentImage()) || "无标记".equals(imageRecognize.getContentImage())) { untypedCount++; } else { typedCount++; diff --git a/bonus-business/src/main/resources/mapper/business/ImageCaptionMapper.xml b/bonus-business/src/main/resources/mapper/business/ImageCaptionMapper.xml index 6ca71c2..444e53a 100644 --- a/bonus-business/src/main/resources/mapper/business/ImageCaptionMapper.xml +++ b/bonus-business/src/main/resources/mapper/business/ImageCaptionMapper.xml @@ -88,6 +88,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and tuo.id=#{id} + ORDER BY tuo.oper_time desc