对接前端和py接口SegAnything

This commit is contained in:
litianxiang
2026-01-14 13:31:33 +08:00
parent 71211bfbc3
commit 112e9c3bc9
3 changed files with 62 additions and 0 deletions

View File

@@ -4105,6 +4105,54 @@ public class PythonService {
//生成失败
throw new BusinessException("segProduct.interface.exception");
}
/**
* 转发 seg_anything 请求到 python 服务
* 请求 body 由调用方组装(包含 user_id, image_path, type, points, labels, box 等)
*/
public JSONObject segAnything(JSONObject content) {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
String url = accessPythonIp + ":" + accessPythonPort + "/api/seg_anything";
log.info("segAnything 请求地址: {} 参数:{}", url, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Response response;
try {
response = client.newCall(request).execute();
} catch (IOException ioException) {
log.error("PythonService##segAnything异常###{}", ExceptionUtil.getThrowableList(ioException));
throw new BusinessException("segAnything.interface.exception");
}
if (response.isSuccessful()) {
try {
if (Objects.nonNull(response.body())) {
String responseBody = response.body().string();
JSONObject responseObject = JSON.parseObject(responseBody);
log.info("PythonService##segAnything response###{}", responseObject);
return responseObject;
}
throw new BusinessException("segAnything.interface.exception");
} catch (IOException | JSONException e) {
log.error("PythonService##segAnything异常###{}", e.getMessage());
throw new BusinessException("segAnything.interface.exception");
}
}
log.error("PythonService##segAnything异常response###{}", response);
throw new BusinessException("segAnything.interface.exception");
}
public Boolean poseTransformation(JSONObject content, String apiUri) {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)