阅读520 返回首页    go 阿里云 go 技术社区[云栖]


名片识别__API介绍_文字识别_人工智能图像类-阿里云

本页面主要介绍服务对应的接口和返回结果中的关键字段的含义,请在阅读本页面之前,了解请求数据格式介绍,了解输入输出的通用数据格式。 此外,在本页最后,附上了名片识别服务调用的程序示例,以供参考。

请求接口

  • 云市场接口

    • 请求方法: POST
    • 请求url: 在云市场搜索印刷文字识别-名片识别,在API接口中找到调用地址
  • 数加接口

    • 请求方法: POST
    • 请求url: 打开管理控制台,接口名称选择ocr_business_card,查看请求地址

请求参数

参数名称 参数类型 描述 默认值
image string dataType为50, dataValue是base64编码后的图像数据, 空字符串

POST请求body 示例如下:

  1. {
  2. "inputs": [
  3. {
  4. "image": {
  5. "dataType": 50, #50表示image的数据类型为字符串
  6. "dataValue": "base64_image_string" #图片以base64编码的string/oss图片链接
  7. }
  8. }]
  9. }

返回结果格式

返回结果格式如下:

  1. {
  2. "outputs": [
  3. {
  4. "outputLabel": "ocr_businesscard",
  5. "outputMulti": {},
  6. "outputValue": {
  7. "dataType": 50,
  8. "dataValue": "{
  9. "name": "张三", #姓名
  10. "company": ["阿里巴巴", "阿里巴巴有限公司" ], #公司结果数组,数组可能为空
  11. "department": ["市场部" ], #部门结果数组,数组可能为空
  12. "title" : [ "经理" ], #职位结果数组,数组可能为空
  13. "tel_cell" : ["15234563443"], #手机结果数组,数组可能为空
  14. "tel_work": ["057185212345"], #座机结果数组,数组可能为空
  15. "addr": ["浙江省杭州市西湖区文一西路969号"], #地址结果数组,数组可能为空
  16. "email": [], #邮箱结果数组 ,数组可能为空
  17. "request_id" : 20160822_32423dfsa23432f #请求对应的唯一表示
  18. "success": true #识别成功与否 true/false
  19. }"
  20. }
  21. }]
  22. }

返回结果是json格式,存放在dataValue字段对应的json String(不是json object)中,主要有如下字段:

  • name: 姓名, 类型为字符串
  • company: 公司, 类型为字符串数组,如果没有该字段,数组为空
  • department: 部门, 类型为字符串数组,如果没有该字段,数组为空
  • title: 职位, 类型为字符串数组,如果没有该字段,数组为空
  • tel_cell: 手机, 类型为字符串数组,如果没有该字段,数组为空
  • tel_work: 座机, 类型为字符串数组,如果没有该字段,数组为空
  • addr: 地址, 类型为字符串数组,如果没有该字段,数组为空
  • email(暂无): 邮箱, 类型为字符串数组,如果没有该字段,数组为空
  • request_id: 请求对应的唯一表示, 用户可以保存该字段用以进行问题追溯,类型为字符串
  • success: 识别流程是否出现异常, false表示识别失败,true表示识别成功, 类型为布尔型

程序示例

云市场

云市场搜索印刷文字识别-名片识别,在API接口中找到请求示例

数加平台

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import sun.misc.BASE64Encoder;
  5. import org.json.JSONObject;
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. public class BusinessCard {
  9. /*
  10. * 获取参数的json对象
  11. */
  12. public static JSONObject getParam(int type, JSONObject dataValue) {
  13. JSONObject obj = new JSONObject();
  14. try {
  15. obj.put("dataType", type);
  16. obj.put("dataValue", dataValue);
  17. } catch (JSONException e) {
  18. e.printStackTrace();
  19. }
  20. return obj;
  21. }
  22. /*
  23. * 获取参数的json对象
  24. */
  25. public static JSONObject getParam(int type, String dataValue) {
  26. JSONObject obj = new JSONObject();
  27. try {
  28. obj.put("dataType", type);
  29. obj.put("dataValue", dataValue);
  30. } catch (JSONException e) {
  31. e.printStackTrace();
  32. }
  33. return obj;
  34. }
  35. public static void main(String[] args) {
  36. String img_file = "businesscard.jpg";
  37. String service_url = "your-service-url";
  38. String ak_id = "your access key id";
  39. String ak_secret = "your access key secret";
  40. // 对图像进行base64编码
  41. String img_base64 = "";
  42. try {
  43. File file = new File(img_file);
  44. byte[] content = new byte[(int) file.length()];
  45. FileInputStream finputstream = new FileInputStream(file);
  46. finputstream.read(content);
  47. finputstream.close();
  48. img_base64 = (new BASE64Encoder()).encode(content);
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. return;
  52. }
  53. // 拼装请求body的json字符串
  54. JSONObject request_obj = new JSONObject();
  55. try {
  56. JSONObject obj = new JSONObject();
  57. JSONArray input_array = new JSONArray();
  58. obj.put("image", getParam(50, img_base64));
  59. JSONObject config_obj = new JSONObject();
  60. config_obj.put("is_oss_data", false);
  61. obj.put("configure", getParam(50, config_obj.toString()));
  62. input_array.put(obj);
  63. request_obj.put("inputs", input_array);
  64. } catch (JSONException e) {
  65. e.printStackTrace();
  66. }
  67. String body = request_obj.toString();
  68. // Sender代码参考
  69. // https://help.aliyun.com/document_detail/shujia/OCR/ocr-api/sender.html
  70. String result = Sender.sendPost(service_url, body, ak_id, ak_secret);
  71. System.out.println(result);
  72. // 解析请求结果
  73. try {
  74. JSONObject result_obj = new JSONObject(result);
  75. JSONArray output_array = result_obj.getJSONArray("outputs");
  76. // 取出结果json字符串
  77. String output = output_array.getJSONObject(0)
  78. .getJSONObject("outputValue").getString("dataValue"); //请注意,dataValue字段对应的是一个字符串,不是json object
  79. JSONObject out = new JSONObject(output);
  80. if (out.getBoolean("success")) {
  81. // code to get result
  82. // 获取姓名, 只有姓名不是数组
  83. String name = out.getString("name");
  84. // 获取所有公司结果
  85. JSONArray company_array = out.getJSONArray("company");
  86. // 获取手机号数组
  87. JSONArray tel_cell_array = out.getJSONArray("tel_cell");
  88. // 请求对应的唯一表示,可以用于事后debug问题
  89. JSONArray request_id_array = out.getJSONArray("request_id");
  90. StringBuilder companys = new StringBuilder();
  91. StringBuilder cells = new StringBuilder();
  92. // 所有公司
  93. for (int i = 0; i < company_array.length() - 1; ++i) {
  94. companys.append(company_array.getString(i) + ",");
  95. }
  96. companys.append(company_array.getString(company_array.length() - 1));
  97. // 所有手机号
  98. for (int i = 0; i < tel_cell_array.length() - 1; ++i) {
  99. cells.append(tel_cell_array.getString(i) + ",");
  100. }
  101. cells.append(tel_cell_array.getString(tel_cell_array.length() - 1));
  102. System.out
  103. .println("name is " + name + ", Companys is "
  104. + companys.toString() + ",Cells is "
  105. + cells.toString());
  106. } else {
  107. System.out.println("predict error");
  108. }
  109. } catch (JSONException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. }

最后更新:2016-11-23 16:04:18

  上一篇:go 英文识别__API介绍_文字识别_人工智能图像类-阿里云
  下一篇:go 发送请求示例代码__API介绍_文字识别_人工智能图像类-阿里云