{"id":84444,"date":"2020-05-15T17:48:16","date_gmt":"2020-05-15T09:48:16","guid":{"rendered":"http:\/\/4563.org\/?p=84444"},"modified":"2020-05-15T17:48:16","modified_gmt":"2020-05-15T09:48:16","slug":"%e6%b1%82%e5%8a%a9%e8%85%be%e8%ae%af%e4%ba%91-api-%e7%bc%96%e5%86%99","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=84444","title":{"rendered":"\u6c42\u52a9\u817e\u8baf\u4e91 API \u7f16\u5199"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u52a9\u817e\u8baf\u4e91 API \u7f16\u5199               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jerryliang <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e3b\u662f\u4e00\u4e2a\u534a\u540a\u5b50\uff0c\u7528 tx \u7684 API \u751f\u6210\u4e86\u4e00\u6bb5 ocr \u7f16\u7801\uff0c\u60f3\u7528 base64\uff0c\u7136\u540e\u6dfb\u52a0\u4e86 python \u56fe\u7247\u8f6c base64 \u7684\u7f16\u7801\u3002<\/p>\n<pre><code>with open(\"1.jpg\",\"rb\") as f:     base64_data = base64.b64encode(f.read())   print(base64_data) print(type(base64_data)) req = models.GeneralBasicOCRRequest()  params = '{\"ImageBase64\":\"base64_data\"}' <\/code><\/pre>\n<p>\u4f46\u662f\u5462\uff0c\u8fd9\u6837\u5b50\u5199\u76f4\u63a5\u63d0\u793a\u6211\u56fe\u7247\u89e3\u6790\u5931\u8d25\u95ee params \u91cc\u7684 base64_data \u600e\u4e48\u53d8\u6210 print(base64_data)\u91cc\u7684 base64.<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1549372\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6587\u6863\u5730\u5740\u4f60\u4e5f\u4e0d\u53d1, \u968f\u4fbf\u7b54\u4e00\u6ce2\u5427:<\/p>\n<p>1. params \u662f\u7ed9 GET \u8bf7\u6c42\u62fc\u51d1 query \u5b57\u7b26\u4e32\u7528\u7684, \u8fd9\u91cc\u547d\u540d\u5982\u679c\u6ca1\u9519\u90a3\u53c2\u6570\u63d0\u4ea4\u65f6\u5019\u53ef\u80fd\u5c31\u9519\u4e86, \u5c31\u6211\u6240\u77e5\u6211\u4e0a\u4f20\u56fe\u7247\u90fd\u662f POST \u7684<\/p>\n<p>2. POST \u8bf7\u6c42\u65f6\u5019\u7684\u5b9e\u4f53\u4e3b\u4f53 data \u90fd\u662f\u8981\u7f16\u7801\u6210 bytes \u7684, \u6240\u4ee5\u8bb0\u5f97 encode \u4e00\u4e0b<\/p>\n<p>3. params = &#8216;{&#8220;ImageBase64&#8243;:&#8221;base64_data&#8221;}&#8217; \u8fd9\u91cc\u8981\u585e\u53d8\u91cf\u8fdb\u53bb\u7684, <\/p>\n<p>\u6240\u4ee5\u8981\u4e48 json.dumps({&#8220;ImageBase64&#8221;:base64_data})<\/p>\n<p>\u8981\u4e48\u5c31 params = &#8216;{&#8220;ImageBase64&#8243;:&#8221;%s&#8221;}&#8217; % base64_data<\/p>\n<p>\u603b\u4e4b\u522b\u5fd8\u4e86 encode                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1549373\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : jerryliang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ClericPy \u55ef\uff0c\u81ea\u5df1 print \u4e86 params \u3002\u662f\u4e00\u4e2a str \u7684\u7c7b\u578b\uff0c\u6545\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b9\u5f0f\u628a\u5b83\u62fc\u63a5\u4e86\uff0c\u7136\u540e\u5c31\u641e\u5b9a\u4e86<br \/> with open(&#8220;1.jpg&#8221;,&#8221;rb&#8221;) as f:<br \/> base64_data = base64.b64encode(f.read()) <br \/> #print(base64_data)<br \/> #print(type(base64_data))<br \/> req = models.GeneralBasicOCRRequest()<br \/> a = &#8220;{&#8220;ImageBase64&#8243;:&#8221;&#8221;<br \/> b = &#8220;&#8221;}&#8221;<br \/> params = a + base64_data + b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u52a9\u817e\u8baf\u4e91 API \u7f16\u5199 \u8cc7\u6df1\u5927\u4f6c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/84444"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84444"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/84444\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84444"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}