七牛 OCR 介面除錯 & 七牛鑑權 Token 應用
阿新 • • 發佈:2019-01-09
在參考七牛官方介面的基礎上,提供 PHP/Java/Python/Go 語言的實現以及執行結果。此外:
- 程式碼基於各服務端語言的七牛 SDK
- 【重要】七牛其他使用鑑權 token的介面,和此介面呼叫方式類似。如內容稽核的7個介面,人臉識別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);
執行結果
原始碼
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);
執行結果
原始碼
【現學現賣】圖片內容稽核介面
實現圖片內容稽核,參考 OCR demo 的程式碼檔案,只需要修改請求 url 和 body,即可呼叫成功,具體如下:
介面規約
POST /v3/image/censor HTTP/1.1 Host: ai.qiniuapi.com
Content-Type: application/json
Authorization: Qiniu YnxrxOSvGotRZpqkZnMzl_euuoqRzOKUd6zwbRju:r6a-o2UpBg6A4puIMnkCExH8lE8=核心程式碼
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);執行結果
程式碼
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)
執行結果
原始碼
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)