对接前端和py接口SegAnything
This commit is contained in:
@@ -23,6 +23,9 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -119,4 +122,14 @@ public class PythonController {
|
|||||||
return Response.success(superResolutionService.prepareForSR(superResolutionDTO));
|
return Response.success(superResolutionService.prepareForSR(superResolutionDTO));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
|
@Operation(summary = "Seg Anything 转发接口")
|
||||||
|
@PostMapping("/segAnything")
|
||||||
|
public Response<JSONObject> segAnything(@RequestBody Map<String, Object> payload) {
|
||||||
|
// 将前端传来的 Map 转为 fastjson JSONObject 并转发给 python 服务
|
||||||
|
JSONObject requestJson = (JSONObject) JSON.toJSON(payload);
|
||||||
|
JSONObject result = pythonService.segAnything(requestJson);
|
||||||
|
return Response.success(result);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4105,6 +4105,54 @@ public class PythonService {
|
|||||||
//生成失败
|
//生成失败
|
||||||
throw new BusinessException("segProduct.interface.exception");
|
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) {
|
public Boolean poseTransformation(JSONObject content, String apiUri) {
|
||||||
OkHttpClient client = new OkHttpClient().newBuilder()
|
OkHttpClient client = new OkHttpClient().newBuilder()
|
||||||
.connectTimeout(30, TimeUnit.SECONDS)
|
.connectTimeout(30, TimeUnit.SECONDS)
|
||||||
|
|||||||
@@ -16,3 +16,4 @@
|
|||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user