Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
1d21a2785a
@ -1,62 +1,69 @@
|
||||
package com.heyu.api.controller.doc;
|
||||
|
||||
|
||||
import com.heyu.api.baidu.handle.doc.BDocClassifyHandle;
|
||||
import com.heyu.api.baidu.request.doc.BDocClassifyRequest;
|
||||
import com.heyu.api.baidu.response.doc.BDocClassifyResp;
|
||||
import com.heyu.api.controller.BaseController;
|
||||
import com.heyu.api.data.annotation.CacheResult;
|
||||
import com.heyu.api.data.annotation.NotIntercept;
|
||||
import com.heyu.api.data.utils.ApiR;
|
||||
import com.heyu.api.data.utils.R;
|
||||
import com.heyu.api.resp.doc.DocClassifyResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import com.heyu.api.data.utils.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/***
|
||||
* https://console.bce.baidu.com/support/?_=1740667714316×tamp=1742716755429#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E6%96%87%E6%A1%A3%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86&api=rest%2F2.0%2Focr%2Fv1%2Fdoc_classify&method=post
|
||||
*
|
||||
* https://cloud.baidu.com/doc/OCR/s/qlor1ahik
|
||||
*
|
||||
* 文件检测分类
|
||||
*
|
||||
* 接口描述
|
||||
* 对图片中的文档、卡证、票据等含文字的主体进行检测、分类,可同时支持一张图片中多张主体的情况,返回每个主体的类别及位置信息。
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/doc")
|
||||
@NotIntercept
|
||||
public class DocClassifyController {
|
||||
|
||||
@Autowired
|
||||
private BDocClassifyHandle bDocClassifyHandle;
|
||||
public class DocClassifyController extends BaseController {
|
||||
|
||||
private static final String DOC_CLASSIFY_URI = "/rest/2.0/ocr/v1/doc_classify";
|
||||
|
||||
/**
|
||||
* 文件检测分类,对图片中的文档、卡证、票据等含文字的主体进行检测、分类
|
||||
*
|
||||
* 请求参数:
|
||||
* <pre>
|
||||
* {
|
||||
* "imageBase64": "图像base64编码(与imageUrl二选一)",
|
||||
* "imageUrl": "图片完整URL(与imageBase64二选一)"
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
@RequestMapping("/classify")
|
||||
@CacheResult
|
||||
public R classify(BDocClassifyRequest request) {
|
||||
List<DocClassifyResp> respList = new ArrayList<>();
|
||||
ApiR<BDocClassifyResp> bR = bDocClassifyHandle.handle(request);
|
||||
if (bR.isSuccess()) {
|
||||
BDocClassifyResp bDocClassifyResp = bR.getData();
|
||||
if (CollectionUtils.isNotEmpty(bDocClassifyResp.getWordsResult())) {
|
||||
for (BDocClassifyResp.WordsResultDTO wordsResultDTO : bDocClassifyResp.getWordsResult()) {
|
||||
DocClassifyResp resp = new DocClassifyResp();
|
||||
resp.setProbablity(wordsResultDTO.getProbablity());
|
||||
resp.setType(wordsResultDTO.getType());
|
||||
respList.add(resp);
|
||||
public R<BDocClassifyResp> classify(BDocClassifyRequest request) {
|
||||
if (request == null || (isBlank(request.getImageBase64()) && isBlank(request.getImageUrl()))) {
|
||||
return R.error("image 和 url 不能都为空");
|
||||
}
|
||||
return R.ok().setData(respList);
|
||||
String content = buildContent(request);
|
||||
Map<String, Object> result = requestBaidu(DOC_CLASSIFY_URI, content);
|
||||
if (result == null) {
|
||||
return R.error("识别失败");
|
||||
}
|
||||
BDocClassifyResp resp = com.alibaba.fastjson.JSONObject.parseObject(
|
||||
com.alibaba.fastjson.JSONObject.toJSONString(result), BDocClassifyResp.class);
|
||||
return R.ok().setData(resp);
|
||||
}
|
||||
return R.error(bR.getErrorMsg());
|
||||
|
||||
private String buildContent(BDocClassifyRequest request) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (StringUtils.isNotBlank(request.getImageBase64())) {
|
||||
sb.append("&image=").append(request.getImageBase64());
|
||||
}
|
||||
if (StringUtils.isNotBlank(request.getImageUrl())) {
|
||||
sb.append("&url=").append(request.getImageUrl());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
package com.heyu.api.controller.doc;
|
||||
|
||||
import com.ApiInterfaceApplicationTests;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.heyu.api.baidu.request.doc.BDocClassifyRequest;
|
||||
import com.heyu.api.baidu.response.doc.BDocClassifyResp;
|
||||
import com.heyu.api.data.utils.R;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* 文件检测分类 - 在线图片验证
|
||||
*
|
||||
* 调用本地 /doc/classify 接口,对图片中的文档、卡证、票据进行检测分类。
|
||||
*
|
||||
* 假设(Given): 目标图片可访问,百度 API 可用
|
||||
* 当(When): 调用 classify 接口,传入图片 URL
|
||||
* 则(Then): 返回检测结果
|
||||
*
|
||||
* @author zhengli
|
||||
* @since 20260531_zl
|
||||
*/
|
||||
public class DocClassifyControllerTest extends ApiInterfaceApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private DocClassifyController docClassifyController;
|
||||
|
||||
private static final String TARGET_IMAGE_URL = "https://www.opsky.com.cn/upload/20211224/KXfgvm2MFRAXKbPu5LK.png";
|
||||
|
||||
@Test
|
||||
void classifyByUrlTest() {
|
||||
// 1. 构造请求
|
||||
BDocClassifyRequest request = new BDocClassifyRequest();
|
||||
request.setImageUrl(TARGET_IMAGE_URL);
|
||||
|
||||
// 2. 打印请求参数
|
||||
System.out.println("\n========== 请求参数 ==========");
|
||||
System.out.println(JSON.toJSONString(request));
|
||||
System.out.println("==============================");
|
||||
|
||||
// 3. 调用本地 /doc/classify 接口
|
||||
R<BDocClassifyResp> result = docClassifyController.classify(request);
|
||||
|
||||
// 4. 打印响应结果
|
||||
System.out.println("\n========== 响应结果 ==========");
|
||||
System.out.println(JSON.toJSONString(result));
|
||||
System.out.println("==============================");
|
||||
|
||||
// 5. 断言
|
||||
assertNotNull(result, "返回结果不能为空");
|
||||
assertEquals("200", result.getCode(), "接口应返回成功");
|
||||
assertNotNull(result.getData(), "返回数据不能为空");
|
||||
|
||||
// 6. 打印接口返回值
|
||||
System.out.println("\n========== /doc/classify 接口返回 ==========");
|
||||
System.out.println("{\"code\":\"" + result.getCode() + "\",");
|
||||
System.out.println(" \"msg\":\"" + result.getMsg() + "\",");
|
||||
System.out.println(" \"data\":" + JSON.toJSONString(result.getData()));
|
||||
System.out.println("}");
|
||||
System.out.println("================================================");
|
||||
}
|
||||
|
||||
@Test
|
||||
void classifyParamEmptyTest() {
|
||||
// 1. 构造请求 - image 和 url 都为空
|
||||
BDocClassifyRequest request = new BDocClassifyRequest();
|
||||
|
||||
// 2. 调用接口
|
||||
R<BDocClassifyResp> result = docClassifyController.classify(request);
|
||||
|
||||
// 3. 断言
|
||||
assertNotNull(result, "返回结果不能为空");
|
||||
assertEquals("400", result.getCode(), "参数为空时应返回失败");
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
- 商售后支持不能出现联系方式
|
||||
- 任何地方不能出现绝对化字眼
|
||||
- 商品介绍不能出现三方品牌
|
||||
- 商品上架完成后,并更新文档【doc/百度智能云-文字识别-API文档-API列表.md】状态;
|
||||
|
||||
#### 二、特殊商品资质文案
|
||||
```
|
||||
|
||||
@ -3,5 +3,5 @@
|
||||
- 1.1、接口开发时,所有的逻辑用controller 来实现;
|
||||
- 1.2、接口实现规范参考:RecognizeDrivingLicenseController.java;
|
||||
- 1.3、接口开发自测完成后记得更新文档【doc/百度智能云-文字识别-API文档-API列表.md】状态;
|
||||
- 1.4、商品上架完成后,并更新文档【doc/百度智能云-文字识别-API文档-API列表.md】状态;
|
||||
- 1.5、接口使用body传参数,ContentType= application/x-www-form-urlencoded;
|
||||
- 1.4、接口使用body传参数,ContentType= application/x-www-form-urlencoded;
|
||||
- 1.5、集成测试参考:com/heyu/api/controller/doc/ForgeryDetectionControllerTest.java;
|
||||
Loading…
x
Reference in New Issue
Block a user