七牛 OCR 接口调试 & 七牛鉴权 Token 应用

在参考七牛官方接口的基础上,提供 PHP/Java/Python/Go 语言的实现以及执行结果。此外:

  1. 代码基于各服务端语言的七牛 SDK
  2. 【重要】七牛其他使用鉴权 token的接口,和此接口调用方式类似。如内容审核的7个接口,人脸识别3个接口,以图搜图等。具体实现可以参考下文的【现学现卖】图片内容审核接口
  3. 下面的代码主要提供接入的参考思路,建议根据需要,调试成功后再修改集成到自己的代码中

接口规约

POST /v1/ocr/idcard  Http/1.1
HOST: ai.qiniuapi.com
Content-Type:application/json
Authorization: Qiniu <AccessKey>:<Sign>
{
    "data": {
        "uri": "http://oayjpradp.bkt.clouddn.com/age_gender_test.png"
    }
}

PHP 实现

核心代码

// 鉴权凭证
$url = "http://ai.qiniuapi.com/v1/ocr/idcard";
$method = "POST";
$host = "ai.qiniuapi.com";
$body = "{ \"data\": { \"uri\": \"http://test-pub.iamlj.com/test-idcard.jpg\" } }";
$contentType = "application/json";

$headers = $auth->authorizationV2($url, $method, $body, $contentType);
$headers['Content-Type'] = $contentType;
$headers['Host'] = $host;

$response = Client::post($url, $body, $headers);

执行结果

源代码

demo

Java 实现

核心代码

String url = "http://ai.qiniuapi.com/v1/ocr/idcard";
String host = "ai.qiniuapi.com";

String body = "{ \"data\": { \"uri\": \"http://test-pub.iamlj.com/test-idcard.jpg\" } }";
String contentType = "application/json";

String method = "POST";
String qiniuToken = "Qiniu " + auth.signRequestV2(url, method, body.getBytes(), contentType);
System.out.println(url);
System.out.println(body);
System.out.println(qiniuToken);

StringMap header = new StringMap();
header.put("Host", host);
header.put("Authorization", qiniuToken);
header.put("Content-Type", contentType);
Configuration c = new Configuration(Zone.zone1());
Client client = new Client(c);
Response response = client.post(url, body.getBytes(), header, contentType);

执行结果

源代码

demo

【现学现卖】图片内容审核接口

实现图片内容审核,参考 OCR demo 的代码文件,只需要修改请求 url 和 body,即可调用成功,具体如下:

  1. 接口规约

    POST /v3/image/censor HTTP/1.1
    Host: ai.qiniuapi.com
    Content-Type: application/json
    Authorization: Qiniu YnxrxOSvGotRZpqkZnMzl_euuoqRzOKUd6zwbRju:r6a-o2UpBg6A4puIMnkCExH8lE8=
  2. 核心代码

    String url = "http://ai.qiniuapi.com/v3/image/censor";
    String host = "ai.qiniuapi.com";
    String body = "{ \"data\": { \"uri\": \"https://mars-assets.qnssl.com/resource/gogopher.jpg\" }, \"params\": { \"scenes\": [ \"pulp\", \"terror\", \"politician\" ] } }";
    String contentType = "application/json";
    String method = "POST";
    String qiniuToken = "Qiniu " + auth.signRequestV2(url, method, body.getBytes(), contentType);
    System.out.println(url);
    System.out.println(body);
    System.out.println(qiniuToken);
    StringMap header = new StringMap();
    header.put("Host", host);
    header.put("Authorization", qiniuToken);
    header.put("Content-Type", contentType);
    Configuration c = new Configuration(Zone.zone1());
    Client client = new Client(c);
    Response response = client.post(url, body.getBytes(), header, contentType);
  3. 执行结果

  4. 代码

    demo

Python 实现

核心代码

url = 'http://ai.qiniuapi.com/v1/ocr/idcard'
data = {"data": {"uri": "http://test-pub.iamlj.com/test-idcard.jpg"}}
req = http._post_with_qiniu_mac(url, data, auth)

执行结果

源代码

demo

Go 实现

核心代码

url := "http://ai.qiniuapi.com/v1/ocr/idcard"
method := "POST"
host := "ai.qiniuapi.com"
contentType := "application/json"
bodyUri := OcrIdcardData{Uri: "http://test-pub.iamlj.com/test-idcard.jpg"}
body := OcrIdcard{Data: &bodyUri}

reqData, _ := json.Marshal(body)

req, reqErr := http.NewRequest(method, url, bytes.NewReader(reqData))
if reqErr != nil {
    return
}

req.Header.Add("Content-Type", contentType)
req.Header.Add("Host", host)

qiniuToken, signErr := mac.SignRequestV2(req)
if signErr != nil {
    fmt.Printf(signErr.Error())
}

req.Header.Add("Authorization", "Qiniu "+qiniuToken)

fmt.Println(string(url))
fmt.Println(string(reqData))
fmt.Println(string("Qiniu " + qiniuToken))

resp, respErr := http.DefaultClient.Do(req)
if respErr != nil {
    fmt.Printf(respErr.Error())
}
defer resp.Body.Close()

resData, ioErr := ioutil.ReadAll(resp.Body)

执行结果

源代码

demo

参考文档

七牛OCR身份证识别

posted @ 2019-01-21 09:42  刘靖  阅读(2003)  评论(0编辑  收藏  举报