やること
WatsonのVisualRecognitionへ画像情報を渡す際、ファイルとして送るのではなく、AWSのS3で作成した期限付きURL情報を渡して結果を取得したい。
実装
Sample1.java
import java.io.File; import java.util.Arrays; import java.util.List; import com.ibm.watson.developer_cloud.visual_recognition.v3.*; import com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyImagesOptions; import com.ibm.watson.developer_cloud.visual_recognition.v3.model.VisualClassification; public class Sample01 { private static final String API_KEY = "作成したAPIキー"; public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "VisualRecognition classify start" ); // インスタンス生成 VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20); service.setApiKey(API_KEY); // classifierIds用 List<String> list = Arrays.asList("分類器ID"); // スコアを取得 ClassifyImagesOptions options = new ClassifyImagesOptions.Builder() .classifierIds(list) .threshold(0.0) .url("https://awsuploadsample.s3-ap-northeast-1.amazonaws.com/dalmation_0012.jpg?response-content-type=image%2Fjpeg&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20170205T071927Z&X-Amz-SignedHeaders=host&X-Amz-Expires=431989&X-Amz-Credential=AKIAIEUCF6U4XJIMGKYQ%2F20170205%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Signature=f9a857f597db9fc07c099aa82c7d12573d9943acd55b3b6e7343ad9cdfa36064") .build(); VisualClassification result = service.classify(options).execute(); // 結果を表示 System.out.println(result); System.out.println( "VisualRecognition classify end" ); } }
↓
VisualRecognition classify start { "images_processed": 1, "images": [ { "classifiers": [ { "classifier_id": "dogs_605755035", "name": "dogs", "classes": [ { "class": "beagle", "score": 0.0529675 }, { "class": "dalmation", "score": 0.536878 } ] } ], "source_url": "https://awsuploadsample.s3-ap-northeast-1.amazonaws.com/dalmation_0012.jpg?response-content-type\u003dimage%2Fjpeg\u0026X-Amz-Algorithm\u003dAWS4-HMAC-SHA256\u0026X-Amz-Date\u003d20170205T071927Z\u0026X-Amz-SignedHeaders\u003dhost\u0026X-Amz-Expires\u003d431989\u0026X-Amz-Credential\u003dAKIAIEUCF6U4XJIMGKYQ%2F20170205%2Fap-northeast-1%2Fs3%2Faws4_request\u0026X-Amz-Signature\u003df9a857f597db9fc07c099aa82c7d12573d9943acd55b3b6e7343ad9cdfa36064", "resolved_url": "https://awsuploadsample.s3-ap-northeast-1.amazonaws.com/dalmation_0012.jpg?response-content-type\u003dimage%2Fjpeg\u0026X-Amz-Algorithm\u003dAWS4-HMAC-SHA256\u0026X-Amz-Date\u003d20170205T071927Z\u0026X-Amz-SignedHeaders\u003dhost\u0026X-Amz-Expires\u003d431989\u0026X-Amz-Credential\u003dAKIAIEUCF6U4XJIMGKYQ%2F20170205%2Fap-northeast-1%2Fs3%2Faws4_request\u0026X-Amz-Signature\u003df9a857f597db9fc07c099aa82c7d12573d9943acd55b3b6e7343ad9cdfa36064" } ] } VisualRecognition classify end
できました(^^)
所感
APIでデータを送信する際、ファイルオブジェクトではなく、URL文字列でもきちんと判定してくれることがわかりました。
ローカル環境にファイルを配置できない場合などに使える感じです。