You've already forked FrameTour-BE
fix(facebody): 优化人脸搜索失败时的重试机制
All checks were successful
ZhenTu-BE/pipeline/head This commit looks good
All checks were successful
ZhenTu-BE/pipeline/head This commit looks good
- 提取error_code到局部变量避免重复调用 - 增加对错误码22204的处理逻辑,当URL图片无法访问时尝试下载并转换为base64后重试 - 添加重试成功后的完整响应处理流程 - 记录详细的重试过程日志便于问题排查 - 保留原始错误响应的日志记录 - 确保所有异常路径都返回有效的响应对象
This commit is contained in:
@@ -314,7 +314,8 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
|||||||
} catch (InterruptedException ignored) {
|
} catch (InterruptedException ignored) {
|
||||||
}
|
}
|
||||||
JSONObject response = client.search(faceUrl, "URL", dbName, options);
|
JSONObject response = client.search(faceUrl, "URL", dbName, options);
|
||||||
if (response.getInt("error_code") == 0) {
|
int errorCode = response.getInt("error_code");
|
||||||
|
if (errorCode == 0) {
|
||||||
resp.setOriginalFaceScore(100f);
|
resp.setOriginalFaceScore(100f);
|
||||||
JSONObject resultObj = response.getJSONObject("result");
|
JSONObject resultObj = response.getJSONObject("result");
|
||||||
if (resultObj == null) {
|
if (resultObj == null) {
|
||||||
@@ -337,7 +338,52 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
|||||||
resp.setFirstMatchRate(result.getFirst().getScore());
|
resp.setFirstMatchRate(result.getFirst().getScore());
|
||||||
}
|
}
|
||||||
return resp;
|
return resp;
|
||||||
|
} else if (errorCode == 222204) {
|
||||||
|
// error_code: 222204 表示无法正常访问URL图片,尝试下载并转换为base64后重试
|
||||||
|
log.warn("搜索人脸时无法正常访问URL图片,错误码: 222204,尝试下载图片转base64后重试,URL: {}", faceUrl);
|
||||||
|
String base64Image = downloadImageAsBase64(faceUrl);
|
||||||
|
if (base64Image != null) {
|
||||||
|
try {
|
||||||
|
searchFaceLimiter.acquire();
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
JSONObject retryResponse = client.search(base64Image, "BASE64", dbName, options);
|
||||||
|
if (retryResponse.getInt("error_code") == 0) {
|
||||||
|
log.info("使用base64重试搜索人脸成功");
|
||||||
|
resp.setOriginalFaceScore(100f);
|
||||||
|
JSONObject resultObj = retryResponse.getJSONObject("result");
|
||||||
|
if (resultObj == null) {
|
||||||
|
resp.setFirstMatchRate(0f);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
JSONArray userList = resultObj.getJSONArray("user_list");
|
||||||
|
List<SearchFaceResultItem> result = new ArrayList<>();
|
||||||
|
for (int i = 0; i < userList.length(); i++) {
|
||||||
|
JSONObject user = userList.getJSONObject(i);
|
||||||
|
SearchFaceResultItem item = new SearchFaceResultItem();
|
||||||
|
item.setDbName(dbName);
|
||||||
|
item.setFaceId(user.getString("user_id"));
|
||||||
|
item.setExtData(user.getString("user_info"));
|
||||||
|
item.setScore(user.getBigDecimal("score").divide(BigDecimal.valueOf(100), 6, RoundingMode.HALF_UP).floatValue());
|
||||||
|
result.add(item);
|
||||||
|
}
|
||||||
|
resp.setResult(result);
|
||||||
|
if (!result.isEmpty()) {
|
||||||
|
resp.setFirstMatchRate(result.getFirst().getScore());
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
} else {
|
||||||
|
log.warn("使用base64重试搜索人脸仍失败!{}", retryResponse);
|
||||||
|
resp.setOriginalFaceScore(0f);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("下载图片转base64失败,无法重试,URL: {}", faceUrl);
|
||||||
|
resp.setOriginalFaceScore(0f);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
log.warn("搜索人脸失败,错误码: {}, 响应: {}", errorCode, response);
|
||||||
resp.setOriginalFaceScore(0f);
|
resp.setOriginalFaceScore(0f);
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user