閱讀520 返回首頁    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介紹_文字識別_人工智能圖像類-阿裏雲