添加第三方接口

This commit is contained in:
haozq 2025-10-22 10:16:07 +08:00
parent ef985589d5
commit 4e15ae287b
3 changed files with 90 additions and 36 deletions

View File

@ -7,16 +7,22 @@ import com.bonus.business.domain.ImageRecognize;
import com.bonus.file.config.SysFile; import com.bonus.file.config.SysFile;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
@Service @Service
@Slf4j @Slf4j
public class AlgorithmService { public class AlgorithmService {
public static final String setContentType="application/json";
/** /**
* 请求路径 * 请求路径
*/ */
@ -33,41 +39,64 @@ public class AlgorithmService {
//图片进行标注 //图片进行标注
public List<ImageRecognize> getImageList(List<SysFile> fileList, String operaName) { public List<ImageRecognize> getImageList(List<SysFile> fileList, String operaName) {
List<ImageRecognize> list=new ArrayList<>(); List<ImageRecognize> list=new ArrayList<>();
Map<String,SysFile> entityMaps=new HashMap<>();
try{ try{
/**
* 数据存储
*/
List<String> imageList=new ArrayList<>(); List<String> imageList=new ArrayList<>();
for (SysFile sysFile : fileList) { for (SysFile sysFile : fileList) {
imageList.add(sysFile.getUrl()); imageList.add(sysFile.getUrl());
entityMaps.put(sysFile.getUrl(),sysFile);
} }
List<String> types= Arrays.asList(operaName.split(",")); List<String> types= Arrays.asList(operaName.split(","));
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(URL+IMAGE_TYPE);
Map<String ,Object> map=new HashMap<>(); Map<String ,Object> map=new HashMap<>();
map.put("images",imageList); map.put("images",imageList);
map.put("types",types); map.put("types",types);
map.put("return_image",true); map.put("return_image",true);
String json= JSON.toJSONString(map); String json= JSON.toJSONString(map);
String responseBody = client.execute(httpPost, httpResponse -> EntityUtils.toString(httpResponse.getEntity())); JSONObject jsonObject= javaHttpPost(IMAGE_TYPE,json);
log.info("数据回调返回-->"); String code=jsonObject.getString("code");
log.info(responseBody); if(code.equals("200")){
StringEntity entity = new StringEntity(json); JSONArray array=jsonObject.getJSONArray("data");
httpPost.setEntity(entity); for (int i=0;i<array.size();i++){
httpPost.setHeader("Accept", "application/json"); ImageRecognize vo = new ImageRecognize();
httpPost.setHeader("Content-type", "application/json"); //获取路径及采集数据
String path=array.getJSONObject(i).getString("image");
String type=array.getJSONObject(i).getString("type");
String imagePath=array.getJSONObject(i).getString("annotated_image_path");
if(type==null || type.isEmpty()){
//获取到图片地址
SysFile sysFile=entityMaps.get(path);
vo.setType("1");
//vo.setContentImage("无标记");
vo.setImageId(sysFile.getId());
vo.setImagePath(path);
list.add(vo);
}else{
//获取到图片地址
SysFile sysFile=entityMaps.get(path);
vo.setType("1");
vo.setContentImage(type);
vo.setImageId(sysFile.getId());
vo.setImagePath(imagePath);
list.add(vo);
}
}
}else{
//调用失败
for (SysFile sysFile : fileList) {
ImageRecognize vo = new ImageRecognize();
// vo.setContentImage("无标记");
vo.setImageId(sysFile.getId());
vo.setType("1");
vo.setImagePath(sysFile.getUrl());
list.add(vo);
}
}
}catch (Exception e){ }catch (Exception e){
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
}
//SHUJU
for (SysFile sysFile : fileList) {
ImageRecognize vo = new ImageRecognize();
sysFile.setType(operaName);
vo.setContentImage(operaName);
vo.setImageId(sysFile.getId());
vo.setType("1");
vo.setImagePath(sysFile.getUrl());
list.add(vo);
} }
return list; return list;
} }
@ -87,18 +116,10 @@ public class AlgorithmService {
imageList.add(sysFile.getUrl()); imageList.add(sysFile.getUrl());
entityMaps.put(sysFile.getUrl(),sysFile); entityMaps.put(sysFile.getUrl(),sysFile);
} }
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(URL+IMAGE_RECOGNIZE);
Map<String ,Object> map=new HashMap<>(); Map<String ,Object> map=new HashMap<>();
map.put("images",imageList); map.put("images",imageList);
String json= JSON.toJSONString(map); String json= JSON.toJSONString(map);
JSONObject jsonObject= javaHttpPost(IMAGE_RECOGNIZE,json);
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);
String code=jsonObject.getString("code"); String code=jsonObject.getString("code");
//接口通了 //接口通了
if(code.equals("200")){ if(code.equals("200")){
@ -111,20 +132,51 @@ public class AlgorithmService {
SysFile sysFile=entityMaps.get(path); SysFile sysFile=entityMaps.get(path);
//综合得分 //综合得分
vo.setOverallScore(score); 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.setContentImage(operaName);
vo.setImageId(sysFile.getId()); vo.setImageId(sysFile.getId());
vo.setImagePath(sysFile.getUrl()); vo.setImagePath(sysFile.getUrl());
list.add(vo); list.add(vo);
} }
} }
log.info("图像评估识别接口调用范湖-->");
log.info(responseBody);
}catch (Exception e){ }catch (Exception e){
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }
return list; 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);
} }
} }

View File

@ -63,7 +63,8 @@ public class ImageCaptionServiceImpl implements ImageCaptionService {
if(StringUtils.isEmpty(vo.getParam())){ if(StringUtils.isEmpty(vo.getParam())){
return AjaxResult.error("请至少选择一个算法!"); return AjaxResult.error("请至少选择一个算法!");
} }
String userId= SecurityUtils.getUserId().toString(); String userId= "1";
// String userId= SecurityUtils.getUserId().toString();
String operaName=vo.getParam(); String operaName=vo.getParam();
//操作日期 //操作日期
List<SysFile> fileList=new ArrayList<>(); List<SysFile> fileList=new ArrayList<>();
@ -177,7 +178,7 @@ public class ImageCaptionServiceImpl implements ImageCaptionService {
// 批量操作的数据容器 // 批量操作的数据容器
for (ImageRecognize imageRecognize : list) { for (ImageRecognize imageRecognize : list) {
// 类型判断与计数使用工具类确保空字符串也被视为无类型 // 类型判断与计数使用工具类确保空字符串也被视为无类型
if (StringUtils.isBlank(imageRecognize.getType())) { if (StringUtils.isBlank(imageRecognize.getContentImage()) || "无标记".equals(imageRecognize.getContentImage())) {
untypedCount++; untypedCount++;
} else { } else {
typedCount++; typedCount++;

View File

@ -88,6 +88,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="id!=null and id!=''"> <if test="id!=null and id!=''">
and tuo.id=#{id} and tuo.id=#{id}
</if> </if>
ORDER BY tuo.oper_time desc
</select> </select>
<!--依据操作记录查询查询详情--> <!--依据操作记录查询查询详情-->