public void processResponse(String jsonResponse) { // The response will be in Json format // therefore it needs to be deserialized into the classes AnalysedObject and TagData AnalysedObject analysedObject = new AnalysedObject(); analysedObject = JsonUtility.FromJson <AnalysedObject>(jsonResponse); if (analysedObject.tags == null) { Debug.Log("analysedObject.tagData is null"); } else { Dictionary <string, float> tagsDictionary = new Dictionary <string, float>(); foreach (TagData td in analysedObject.tags) { TagData tag = td as TagData; tagsDictionary.Add(tag.name, tag.confidence); Debug.Log(tag.name + " " + tag.confidence); } //ResultsLabel.instance.SetTagsToLastLabel(tagsDictionary); } }
/// <summary> /// Begin process of Image Capturing and send To Azure /// Computer Vision service. /// </summary> private async Task ExecuteImageCaptureAndAnalysis() { var isNewCapture = false; if (AppSession.Capabilities.isWindowsMR) { isNewCapture = TakeScreenshotMixedReality(); } else if (AppSession.Capabilities.isHololens) { isNewCapture = TakeScreenshotHololens(); } else //we assume PC { isNewCapture = TakeScreenshotPC(); } if (isNewCapture) { AnalysedObject result = null; var taskResult = await _azureController.PostVisionAnalysisAsync <AnalysedObject>(GetScreenshotFilePath()); imgHandler.SetConfidenceLevel(1); //TODO: update color indicator } }
/// <summary> /// Call the Computer Vision Service to submit the image. /// </summary> public IEnumerator AnalyseLastImageCaptured() { WWWForm webForm = new WWWForm(); using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(visionAnalysisEndpoint, webForm)) { // gets a byte array out of the saved image imageBytes = GetImageAsByteArray(imagePath); unityWebRequest.SetRequestHeader("Content-Type", "application/octet-stream"); unityWebRequest.SetRequestHeader(ocpApimSubscriptionKeyHeader, authorizationKey); // the download handler will help receiving the analysis from Azure unityWebRequest.downloadHandler = new DownloadHandlerBuffer(); // the upload handler will help uploading the byte array with the request unityWebRequest.uploadHandler = new UploadHandlerRaw(imageBytes); unityWebRequest.uploadHandler.contentType = "application/octet-stream"; yield return(unityWebRequest.SendWebRequest()); long responseCode = unityWebRequest.responseCode; try { string jsonResponse = null; jsonResponse = unityWebRequest.downloadHandler.text; // The response will be in Json format // therefore it needs to be deserialized into the classes AnalysedObject and TagData AnalysedObject analysedObject = new AnalysedObject(); analysedObject = JsonUtility.FromJson <AnalysedObject>(jsonResponse); if (analysedObject.tags == null) { Debug.Log("analysedObject.tagData is null"); } else { Dictionary <string, float> tagsDictionary = new Dictionary <string, float>(); foreach (TagData td in analysedObject.tags) { TagData tag = td as TagData; tagsDictionary.Add(tag.name, tag.confidence); } ResultsLabel.instance.SetTagsToLastLabel(tagsDictionary); } } catch (Exception exception) { Debug.Log("Json exception.Message: " + exception.Message); } yield return(null); } }
/// <summary> /// Call the Computer Vision Service to submit the image. /// </summary> public IEnumerator AnalyseLastImageCaptured() { WWWForm webForm = new WWWForm(); // gets a byte array out of the saved image byte[] imageBytes = GetImageAsByteArray(imagePath); // put in base64 format for endpoint and use with field "image" String s = Convert.ToBase64String(imageBytes); webForm.AddField("image", s); // display the image that was taken and being uploaded to the endpoint Texture2D tex = new Texture2D(2, 2); tex.LoadImage(imageBytes); GameObject taken_image_object = ResultsLabel.instance.lastLabelPlaced.transform.Find("TakenImage").gameObject; UnityEngine.UI.RawImage taken_raw_image = taken_image_object.GetComponent <UnityEngine.UI.RawImage>(); taken_raw_image.texture = tex; using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(endpoint, webForm)) { yield return(unityWebRequest.SendWebRequest()); if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError) { print(unityWebRequest.error); } else { // print("finished uploading image"); String json_response_text = unityWebRequest.downloadHandler.text; analysedObject = new AnalysedObject(); analysedObject = JsonUtility.FromJson <AnalysedObject>(json_response_text); masterdictionary = GetDictFromJsonText(analysedObject); // get the first objectid string current_objectid = analysedObject.items_info[0].objectid; // set the title WriteTitle(masterdictionary, current_objectid); // set the information WriteInformation(masterdictionary, current_objectid); // set the image from the base64 string WriteImageToScreenFromBase64(analysedObject.img_str); } } }
public IEnumerator TakeImageAnalysis(Texture2D tex) { WWWForm webForm = new WWWForm(); using (UnityWebRequest webReq = UnityWebRequest.Post(visionEndpoint, webForm)) { byte[] imageBytes = tex.EncodeToPNG(); webReq.SetRequestHeader("Content-Type", "application/octet-stream"); webReq.SetRequestHeader("Accept", "application/json"); webReq.SetRequestHeader(subKeyHeader, authKey); webReq.downloadHandler = new DownloadHandlerBuffer(); webReq.uploadHandler = new UploadHandlerRaw(imageBytes); webReq.uploadHandler.contentType = "application/octet-stream"; yield return(webReq.SendWebRequest()); long responseCode = webReq.responseCode; try { string jsonResponse = null; jsonResponse = webReq.downloadHandler.text; // The response will be in Json format // therefore it needs to be deserialized into the classes AnalysedObject and TagData AnalysedObject analysedObject = new AnalysedObject(); analysedObject = JsonUtility.FromJson <AnalysedFaces>("{\"faces\": " + jsonResponse + "}").faces[0]; if (analysedObject.faceAttributes == null) { Debug.Log("analysedObject.tagData is null"); } else { EmotionManager.Instance.faceAttributes = analysedObject.faceAttributes; EmotionManager.Instance.EmotionsUpdated(); } } catch (Exception exception) { Debug.Log("Json exception.Message: " + exception.Message); } yield return(null); } }
// returns the deserlized object from the json text from the endpoint response public Dictionary <string, Dictionary <string, string> > GetDictFromJsonText(AnalysedObject analysedObject) { Dictionary <string, Dictionary <string, string> > temp_masterdictionary = new Dictionary <string, Dictionary <string, string> >(); foreach (Item i in analysedObject.items_info) { // Debug.Log(i.objectid); Dictionary <string, string> EmployeeList = new Dictionary <string, string>(); foreach (Info info in i.information) { // Debug.Log(info.title); // Debug.Log(info.description); EmployeeList.Add(info.title, info.description); } temp_masterdictionary.Add(i.objectid, EmployeeList); } return(temp_masterdictionary); }
/// <summary> /// Call the Computer Vision Service to submit the image. /// </summary> public IEnumerator AnalyseLastImageCaptured() { WWWForm webForm = new WWWForm(); using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(visionAnalysisEndpoint, webForm)) { // gets a byte array out of the saved image imageBytes = GetImageAsByteArray(imagePath); unityWebRequest.SetRequestHeader("Content-Type", "application/octet-stream"); unityWebRequest.SetRequestHeader(ocpApimSubscriptionKeyHeader, authorizationKey); // the download handler will help receiving the analysis from Azure unityWebRequest.downloadHandler = new DownloadHandlerBuffer(); // the upload handler will help uploading the byte array with the request unityWebRequest.uploadHandler = new UploadHandlerRaw(imageBytes); unityWebRequest.uploadHandler.contentType = "application/octet-stream"; yield return(unityWebRequest.SendWebRequest()); long responseCode = unityWebRequest.responseCode; try { string jsonResponse = null; jsonResponse = unityWebRequest.downloadHandler.text; // The response will be in Json format // therefore it needs to be deserialized into the classes AnalysedObject and TagData AnalysedObject analysedObject = new AnalysedObject(); analysedObject = JsonUtility.FromJson <AnalysedObject>(jsonResponse); if (analysedObject.tags == null) { Debug.Log("analysedObject.tagData is null"); } else { Dictionary <string, string> tagsDictionary = new Dictionary <string, string>(); foreach (TagData td in analysedObject.tags) { TagData tag = td as TagData; if ("fruit".Equals(tag.name, StringComparison.OrdinalIgnoreCase)) { tagsDictionary = new Dictionary <string, string>(); tagsDictionary.Add(tag.name, "calories:52kcal\nfat:0.2g\npolyunsatured fat:0.1g\ncholesterol:0mg\nsodium:1mg\npotassium:107mg\ncarbs:14g\nfood fiber:1.4g\nsugar: 10g\nproteins:0.3g"); break; } else { tagsDictionary.Add(tag.name, tag.confidence.ToString("0.00 \n")); } } ResultsLabel.instance.SetTagsToLastLabel(tagsDictionary); } } catch (Exception exception) { Debug.Log("Json exception.Message: " + exception.Message); } yield return(null); } }
/// <summary> /// Call the Computer Vision Service to submit the image. /// </summary> public IEnumerator AnalyseLastImageCaptured() { lc.LogMessage("Sende Data"); WWWForm webForm = new WWWForm(); using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(visionAnalysisEndpoint, webForm)) { // gets a byte array out of the saved image imageBytes = GetImageAsByteArray(imagePath); unityWebRequest.SetRequestHeader("Content-Type", "application/json"); unityWebRequest.SetRequestHeader(ocpApimSubscriptionKeyHeader, authorizationKey); byte[] bytes = Encoding.ASCII.GetBytes("{((char)34)url((char)34):((char)34)https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS5jb20vaW1hZ2VzL2kvMDAwLzA5Ny85NTkvb3JpZ2luYWwvc2h1dHRlcnN0b2NrXzYzOTcxNjY1LmpwZw==((char)34)}"); unityWebRequest.uploadHandler = new UploadHandlerRaw(bytes); unityWebRequest.uploadHandler.contentType = "application/json"; // the download handler will help receiving the analysis from Azure unityWebRequest.downloadHandler = new DownloadHandlerBuffer(); try { unityWebRequest.SendWebRequest(); }catch (Exception e) { lc.LogMessage(e.Message); } yield return(unityWebRequest.SendWebRequest()); long responseCode = unityWebRequest.responseCode; lc.LogMessage("ResponseCode = " + responseCode); try { string jsonResponse = null; jsonResponse = unityWebRequest.downloadHandler.text; lc.LogMessage("Antwort: " + responseCode + " " + jsonResponse); // The response will be in Json format // therefore it needs to be deserialized into the classes AnalysedObject and TagData AnalysedObject analysedObject = new AnalysedObject(); analysedObject = JsonUtility.FromJson <AnalysedObject>(jsonResponse); if (analysedObject.tags == null) { Debug.Log("analysedObject.tagData is null"); } else { Dictionary <string, float> tagsDictionary = new Dictionary <string, float>(); foreach (TagData td in analysedObject.tags) { TagData tag = td as TagData; tagsDictionary.Add(tag.name, tag.confidence); } ResultsLabel.instance.SetTagsToLastLabel(tagsDictionary); } } catch (Exception exception) { Debug.Log("Json exception.Message: " + exception.Message); } } }