/* * 取得した画像をComputer Vision APIに送信し、タグ、キャプションを取得する */ private IEnumerator <object> PostToVisionAPI(byte[] imageData) { DisplaySystemMessage("Call Computer Vision API..."); var headers = new Dictionary <string, string>() { { "Ocp-Apim-Subscription-Key", visionAPIKey }, { "Content-Type", "application/octet-stream" } }; WWW www = new WWW(visionURL, imageData, headers); yield return(www); ResponceJson caption = JsonUtility.FromJson <ResponceJson>(www.text); //キャプションが取得できなかった場合は後続処理Skip(日本語だと取れない場合が多い模様) if (caption.description.captions.Length == 0) { DisplaySystemMessage(TRY_AGAIN_STR); tts.StartSpeaking(TRY_AGAIN_STR); yield break; } // 日記生成部品を呼び出す。Tagsは今回のサンプルではDebug出力のみ。 fileIOManager.WriteDiary(caption.description.captions[0].text); DisplaySystemMessage(caption.description.captions[0].text); foreach (Tags tag in caption.tags) { /* ここを自然言語処理でtag集めて文章生成したらより日記風になって楽しくなるはず!! */ Debug.Log("name=" + tag.name + ",confidence" + tag.confidence + "\n"); } }
/// <summary> /// Get data from the Cognitive Services Custom Vision Services /// Stores the response into the responseData string /// </summary> /// <returns> IEnumerator - needs to be called in a Coroutine </returns> IEnumerator GetVisionDataFromImages(byte[] imageData) { var headers = new Dictionary <string, string>() { { "Prediction-Key", apiKey }, { "Content-Type", "application/octet-stream" } }; WWW www = new WWW(customVisionURL, imageData, headers); yield return(www); string responseData = www.text; // Save the response as JSON string Debug.Log(responseData); ResponceJson json = JsonUtility.FromJson <ResponceJson>(responseData); float tmpProbability = 0.0f; string str = ""; for (int i = 0; i < json.Predictions.Length; i++) { Prediction obj = (Prediction)json.Predictions[i]; Debug.Log(obj.Tag + ":" + obj.Probability.ToString("P")); if (tmpProbability < obj.Probability) { str = obj.Probability.ToString("P") + "の確率で" + obj.Tag + "です"; tmpProbability = obj.Probability; } } textObject.text = str; }
/// <summary> /// Get data from the Cognitive Services Custom Vision Services /// Stores the response into the responseData string /// </summary> /// <returns> IEnumerator - needs to be called in a Coroutine </returns> IEnumerator GetVisionDataFromImages(byte[] imageData) { var headers = new Dictionary <string, string>() { { "Prediction-Key", apiKey }, { "Content-Type", "application/octet-stream" } }; WWW www = new WWW(customVisionURL, imageData, headers); yield return(www); string responseData = www.text; // Save the response as JSON string ResponceJson json = JsonConvert.DeserializeObject <ResponceJson>(responseData); float tmpProbability = 0.0f; string str = ""; Prediction bestMatch = json.Predictions[0]; for (int i = 0; i < json.Predictions.Length; i++) { Prediction obj = (Prediction)json.Predictions[i]; Debug.Log(obj.TagName); if (tmpProbability < obj.Probability) { str = obj.Probability.ToString("P") + "の確率で" + obj.TagName + "です"; tmpProbability = obj.Probability; bestMatch = obj; Debug.Log(bestMatch.Probability); } } textObject.text = str; Debug.Log(bestMatch.Probability + ":" + bestMatch.BoundingBox.Left + "," + bestMatch.BoundingBox.Top + "," + bestMatch.BoundingBox.Width + "," + bestMatch.BoundingBox.Height); float tempx = bestMatch.BoundingBox.Left + bestMatch.BoundingBox.Width; float x = (tempx * 722) - 361; float tempy = bestMatch.BoundingBox.Top + bestMatch.BoundingBox.Height; float y = (tempy * 424) - 212; Debug.Log(x + ":" + y); BlockingSphere.transform.localPosition = new Vector3(x, y, 0); }
/* * 取得した画像をCustom Vision APIに送信し、タグと確率を取得する */ private IEnumerator <object> PostToCustomVisionAPI(byte[] imageData) { DisplaySystemMessage("Call Custom Vision API..."); var headers = new Dictionary <string, string>() { { "Prediction-Key", visionAPIKey }, { "Content-Type", "application/octet-stream" } }; WWW www = new WWW(visionURL, imageData, headers); yield return(www); ResponceJson json = JsonUtility.FromJson <ResponceJson>(www.text); float tmpProbability = 0.0f; string str = ""; // probabilityの高い順に格納されているが、念のため全量取得してログ出力 for (int i = 0; i < json.predictions.Length; i++) { Prediction obj = (Prediction)json.predictions[i]; Debug.Log(obj.tagName + ":" + obj.probability.ToString("P")); if (tmpProbability < obj.probability) { str = "It's a " + obj.tagName + "! The Probability is " + obj.probability.ToString("P"); tmpProbability = obj.probability; } // probabilityが閾値未満だったら特定不能とする if (tmpProbability < probabilityThreshold) { str = "Oops! I couldn't identify it. Please Try Again..."; } } DisplaySystemMessage(str); tts.StartSpeaking(str); }