From 112e9c3bc981363a045905417f4d5479d2bdc0b2 Mon Sep 17 00:00:00 2001 From: litianxiang Date: Wed, 14 Jan 2026 13:31:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E5=89=8D=E7=AB=AF=E5=92=8Cpy?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3SegAnything?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/da/controller/PythonController.java | 13 +++++ .../java/com/ai/da/python/PythonService.java | 48 +++++++++++++++++++ .../primary/WorkspaceRelStyleMapper.xml | 1 + 3 files changed, 62 insertions(+) diff --git a/src/main/java/com/ai/da/controller/PythonController.java b/src/main/java/com/ai/da/controller/PythonController.java index 794a626b..d1b722f4 100644 --- a/src/main/java/com/ai/da/controller/PythonController.java +++ b/src/main/java/com/ai/da/controller/PythonController.java @@ -23,6 +23,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + import jakarta.annotation.Resource; import java.math.BigDecimal; import java.util.Collections; @@ -119,4 +122,14 @@ public class PythonController { return Response.success(superResolutionService.prepareForSR(superResolutionDTO)); } + @CrossOrigin + @Operation(summary = "Seg Anything 转发接口") + @PostMapping("/segAnything") + public Response segAnything(@RequestBody Map payload) { + // 将前端传来的 Map 转为 fastjson JSONObject 并转发给 python 服务 + JSONObject requestJson = (JSONObject) JSON.toJSON(payload); + JSONObject result = pythonService.segAnything(requestJson); + return Response.success(result); + } + } diff --git a/src/main/java/com/ai/da/python/PythonService.java b/src/main/java/com/ai/da/python/PythonService.java index 6cc059eb..8c742199 100644 --- a/src/main/java/com/ai/da/python/PythonService.java +++ b/src/main/java/com/ai/da/python/PythonService.java @@ -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) diff --git a/src/main/resources/mapper/primary/WorkspaceRelStyleMapper.xml b/src/main/resources/mapper/primary/WorkspaceRelStyleMapper.xml index 2ed23821..e65cd83b 100644 --- a/src/main/resources/mapper/primary/WorkspaceRelStyleMapper.xml +++ b/src/main/resources/mapper/primary/WorkspaceRelStyleMapper.xml @@ -16,3 +16,4 @@ +