質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

1回答

698閲覧

Computer Vision APIでファイル添付でできるようにしたい

riroholll

総合スコア57

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2017/09/09 14:42

編集2017/09/09 15:23
<!DOCTYPE html> <html> <head> <title>Analyze Sample</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> </head> <body> <script type="text/javascript"> function processImage() { // ********************************************** // *** Update or verify the following values. *** // ********************************************** // Replace the subscriptionKey string value with your valid subscription key. var subscriptionKey = "13hc77781f7e4b19b5fcdd72a8df7156"; // Replace or verify the region. // // You must use the same region in your REST API call as you used to obtain your subscription keys. // For example, if you obtained your subscription keys from the westus region, replace // "westcentralus" in the URI below with "westus". // // NOTE: Free trial subscription keys are generated in the westcentralus region, so if you are using // a free trial subscription key, you should not need to change this region. var uriBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze"; // Request parameters. var params = { "visualFeatures": "Categories,Description,Color", "details": "", "language": "en", }; // Display the image. var sourceImageUrl = document.getElementById("inputImage").value; document.querySelector("#sourceImage").src = sourceImageUrl; // Perform the REST API call. $.ajax({ url: uriBase + "?" + $.param(params), // Request headers. beforeSend: function(xhrObj){ xhrObj.setRequestHeader("Content-Type","application/json"); xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey); }, type: "POST", // Request body. data: '{"url": ' + '"' + sourceImageUrl + '"}', }) .done(function(data) { // Show formatted JSON on webpage. $("#responseTextArea").val(JSON.stringify(data, null, 2)); }) .fail(function(jqXHR, textStatus, errorThrown) { // Display error message. var errorString = (errorThrown === "") ? "Error. " : errorThrown + " (" + jqXHR.status + "): "; errorString += (jqXHR.responseText === "") ? "" : jQuery.parseJSON(jqXHR.responseText).message; alert(errorString); }); }; </script> <h1>Analyze image:</h1> Enter the URL to an image of a natural or artificial landmark, then click the <strong>Analyze image</strong> button. <br><br> Image to analyze: <input type="text" name="inputImage" id="inputImage" value="http://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg" /> <button onclick="processImage()">Analyze image</button> <br><br> <div id="wrapper" style="width:1020px; display:table;"> <div id="jsonOutput" style="width:600px; display:table-cell;"> Response: <br><br> <textarea id="responseTextArea" class="UIInput" style="width:580px; height:400px;"></textarea> </div> <div id="imageDiv" style="width:420px; display:table-cell;"> Source image: <br><br> <img id="sourceImage" width="400" /> </div> </div> </body> </html>
Image to analyze: <input type="text" name="inputImage" id="inputImage" value="http://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg" />
type="text"を type="file"

にして、fileを添付して分析をできるようにしたいのですがうまくいきません。どうすればよいでしょうか。

引用 https://docs.microsoft.com/ja-jp/azure/cognitive-services/computer-vision/quickstarts/javascript

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kei344

2017/09/09 15:08

コードの引用元は質問文に明記してください。
guest

回答1

0

【Cognitive Services APIs Reference】
https://westcentralus.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa

「Request body」の項目を見たら application/json はURLのみを渡す仕様で application/octet-streammultipart/form-data でバイナリを渡すようです。

multipart/form-data の方がやりやすそうな気がします。


#####'application/octet-stream'

【ajax - Sending binary data in javascript over HTTP - Stack Overflow】
https://stackoverflow.com/questions/19959072/sending-binary-data-in-javascript-over-http

#####'multipart/form-data'

【jQuery Ajaxでファイルを送りたい! - かもメモ】
http://chaika.hatenablog.com/entry/2014/04/12/001012

【FormData オブジェクトの利用 - ウェブデベロッパーガイド | MDN】
https://developer.mozilla.org/ja/docs/Web/Guide/Using_FormData_Objects


【jQuery.ajax() | jQuery API Documentation】
http://api.jquery.com/jQuery.ajax/

投稿2017/09/09 18:40

kei344

総合スコア69407

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問