接口SegAnything返回值处理

This commit is contained in:
litianxiang
2026-01-15 17:17:08 +08:00
parent 112e9c3bc9
commit 3f5ce6e0e7
2 changed files with 4635 additions and 4610 deletions

View File

@@ -125,11 +125,11 @@ public class PythonController {
@CrossOrigin @CrossOrigin
@Operation(summary = "Seg Anything 转发接口") @Operation(summary = "Seg Anything 转发接口")
@PostMapping("/segAnything") @PostMapping("/segAnything")
public Response<JSONObject> segAnything(@RequestBody Map<String, Object> payload) { public Response<String> segAnything(@RequestBody Map<String, Object> payload) {
// 将前端传来的 Map 转为 fastjson JSONObject 并转发给 python 服务 // 将前端传来的 Map 转为 fastjson JSONObject 并转发给 python 服务
JSONObject requestJson = (JSONObject) JSON.toJSON(payload); JSONObject requestJson = (JSONObject) JSON.toJSON(payload);
JSONObject result = pythonService.segAnything(requestJson); String url = pythonService.segAnything(requestJson);
return Response.success(result); return Response.success(url);
} }
} }

View File

@@ -4109,7 +4109,7 @@ public class PythonService {
* 转发 seg_anything 请求到 python 服务 * 转发 seg_anything 请求到 python 服务
* 请求 body 由调用方组装(包含 user_id, image_path, type, points, labels, box 等) * 请求 body 由调用方组装(包含 user_id, image_path, type, points, labels, box 等)
*/ */
public JSONObject segAnything(JSONObject content) { public String segAnything(JSONObject content) {
OkHttpClient client = new OkHttpClient().newBuilder() OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS) .pingInterval(5, TimeUnit.SECONDS)
@@ -4142,7 +4142,32 @@ public class PythonService {
String responseBody = response.body().string(); String responseBody = response.body().string();
JSONObject responseObject = JSON.parseObject(responseBody); JSONObject responseObject = JSON.parseObject(responseBody);
log.info("PythonService##segAnything response###{}", responseObject); log.info("PythonService##segAnything response###{}", responseObject);
return responseObject; // Extract nested output path and convert to presigned MinIO URL for frontend, return as String
try {
if (responseObject != null) {
JSONObject dataObj = responseObject.getJSONObject("data");
if (dataObj != null) {
JSONObject inner = dataObj.getJSONObject("data");
if (inner != null) {
String output = inner.getString("output");
if (!StringUtil.isNullOrEmpty(output)) {
try {
String presigned = minioUtil.getPreSignedUrl(output, 24 * 60);
return presigned;
} catch (Exception e) {
log.error("Failed to generate presigned url for {}: {}", output, e.getMessage());
throw new BusinessException("segAnything.presign.failed");
}
}
}
}
}
} catch (Exception ex) {
log.error("PythonService##segAnything post-process error###{}", ex.getMessage());
throw new BusinessException("segAnything.interface.exception");
}
throw new BusinessException("segAnything.missing.output");
} }
throw new BusinessException("segAnything.interface.exception"); throw new BusinessException("segAnything.interface.exception");
} catch (IOException | JSONException e) { } catch (IOException | JSONException e) {