閱讀675 返回首頁    go 阿裏雲


英文識別__API介紹_文字識別_人工智能圖像類-阿裏雲

本頁麵主要介紹服務對應的接口和返回結果中的關鍵字段的含義,請在閱讀本頁麵之前,了解請求數據格式介紹,了解輸入輸出的通用數據格式。 此外,在本頁最後,附上了英文識別服務調用的程序示例,以供參考。

請求接口

  • 雲市場接口

    • 請求方法: POST
    • 請求url: 在雲市場搜索印刷文字識別-英文識別,在API接口中找到調用地址
  • 數加接口

    • 請求方法: POST
    • 請求url: 打開管理控製台,接口名稱選擇ocr_english,查看請求地址

請求參數格式

參數名稱 參數類型 描述 默認值
image string dataType為50, dataValue是base64編碼後的圖像數據 空字符串
  1. {
  2. "inputs": [
  3. {
  4. "image": {
  5. "dataType": 50, #50表示image的數據類型為字符串
  6. "dataValue": "base64_image_string" #圖片以base64編碼的string
  7. }
  8. }]
  9. }

返回結果格式

識別結果主要包含三個字段:

  • success:識別正確與否, true表示成功,false表示失敗,類型為布爾型
  • error_msg:如果識別過程發生錯誤,顯示錯誤原因,否則為空””,類型為字符串
  • result: 識別結果,類型為字符串
  1. {
  2. "outputs": [
  3. {
  4. "outputLabel": "ocr_babel",
  5. "outputMulti": {},
  6. "outputValue": {
  7. "dataType": 50,
  8. "dataValue": "{
  9. "error_msg" : "", #錯誤信息
  10. "result" : "love", #識別英文字符
  11. "success" : true #success為識別結果:true表示成功,false表示失敗
  12. }"
  13. }
  14. }]
  15. }

程序示例

雲市場

雲市場搜索印刷文字識別-英文識別,在API接口中找到請求示例

數加平台

  1. import org.apache.commons.codec.binary.Base64;
  2. import org.json.JSONObject;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. public class English {
  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 imgFile = "english.jpg";
  37. String serviceURL = "your-service-url";
  38. String akID = "your access key id";
  39. String akSecret = "your access key secret";
  40. // 對圖像進行base64編碼
  41. String imgBase64 = "";
  42. try {
  43. File file = new File(imgFile);
  44. byte[] content = new byte[(int) file.length()];
  45. FileInputStream finputstream = new FileInputStream(file);
  46. finputstream.read(content);
  47. finputstream.close();
  48. imgBase64 = new String(Base64.encodeBase64(content));
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. return;
  52. }
  53. // 拚裝請求body的json字符串
  54. JSONObject requestObj = new JSONObject();
  55. try {
  56. JSONObject obj = new JSONObject();
  57. JSONArray inputArray = new JSONArray();
  58. obj.put("image", getParam(50, imgBase64));
  59. inputArray.put(obj);
  60. requestObj.put("inputs", inputArray);
  61. } catch (JSONException e) {
  62. e.printStackTrace();
  63. }
  64. String body = requestObj.toString();
  65. //Sender代碼參考 https://help.aliyun.com/document_detail/shujia/OCR/ocr-api/sender.html
  66. String result = Sender.sendPost(serviceURL, body, akID, akSecret);
  67. System.out.println(result);
  68. // 解析請求結果
  69. try {
  70. JSONObject resultObj = new JSONObject(result);
  71. JSONArray outputArray = resultObj.getJSONArray("outputs");
  72. String output = outputArray.getJSONObject(0).getJSONObject("outputValue").getString("dataValue"); // 取出結果json字符串
  73. JSONObject out = new JSONObject(output);
  74. if (out.getBoolean("success")) {
  75. String r = out.getString("result"); // 獲取識別結果
  76. System.out.printf(" english : %s n", r);
  77. } else {
  78. System.out.println("error" + out.getString("error_msg"));
  79. }
  80. } catch (JSONException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. }

最後更新:2016-11-24 11:23:48

  上一篇:go 門店識別__API介紹_文字識別_人工智能圖像類-阿裏雲
  下一篇:go 名片識別__API介紹_文字識別_人工智能圖像類-阿裏雲