public IEnumerator Capture() { while (true) { //texture2D.Apply(false); // Not required. Because we do not need to be uploaded it to GPU byte[] jpg = img; string base64 = System.Convert.ToBase64String(jpg); #if UNITY_WEBGL Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); #else AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { string url = this.url + this.apiKey; byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (string.IsNullOrEmpty(www.error)) { //Debug.Log(www.text.Replace("\n", "").Replace(" ", "")); AnnotateImageResponses responses = JsonUtility.FromJson <AnnotateImageResponses>(www.text); // SendMessage, BroadcastMessage or someting like that. Debug.Log("got results"); TextOcr(responses); } else { Debug.Log("Error: " + www.error); } } } #endif } }
//Use this for initialization void Start() { track_dOrientation = this.GetComponent <TrackDeviceOrientation> (); headers = new Dictionary <string, string>(); headers.Add("Content-Type", "application/json; charset=UTF-8"); #if UNITY_EDITOR if (apiKey == null || apiKey == "") { Debug.LogError("No API key. Please set your API key into the \"WebCamHolder->MainCamera->VisionAPICaller(Script)\" component."); } #endif apiRequests = new AnnotateImageRequests(); apiRequests.requests = new List <AnnotateImageRequest> (); defineRequest = new AnnotateImageRequest(); defineRequest.image = new Image(); defineRequest.features = new List <Feature>(); }
private IEnumerator Capture() { while (true) { if (this.apiKey == null) { yield return(null); } yield return(new WaitForSeconds(captureIntervalSeconds)); Color[] pixels = webcamTexture.GetPixels(); if (pixels.Length == 0) { yield return(null); } if (texture2D == null || webcamTexture.width != texture2D.width || webcamTexture.height != texture2D.height) { texture2D = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32, false); } texture2D.SetPixels(pixels); // texture2D.Apply(false); // Not required. Because we do not need to be uploaded it to GPU byte[] jpg = texture2D.EncodeToJPG(); string base64 = System.Convert.ToBase64String(jpg); #if UNITY_WEBGL Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); #else AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { Debug.Log(this.url); Debug.Log(this.apiKey); string url = this.url + this.apiKey; Debug.Log(url); byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); Debug.Log(www.text); if (www.error == null) { Debug.Log(www.text.Replace("\n", "").Replace(" ", "")); AnnotateImageResponses responses = JsonUtility.FromJson <AnnotateImageResponses>(www.text); // SendMessage, BroadcastMessage or someting like that. Sample_OnAnnotateImageResponses(responses); if (responses.responses.Count > 0 && responses.responses[0].labelAnnotations.Count > 0) { // make a new text object and set the text to it Destroy(currentLabel); currentLabel = Instantiate(label, textSpawnPoint.position, textSpawnPoint.rotation, canvas.transform) as GameObject; currentLabel.GetComponent <Text>().text = responses.responses[0].labelAnnotations[0].description; // translate it translate.Process(curr_lang, currentLabel.GetComponent <Text>().text); } } else { Debug.Log("Error: " + www.error); } } } #endif } }
public IEnumerator Capture(System.Action <string> callback) { while (true) { //if (this.apiKey == null) // yield return null; //yield return new WaitForSeconds(captureIntervalSeconds); //wait for 5.0 seconds Color[] pixels = webcamTexture.GetPixels(); if (pixels.Length == 0) { yield return(null); } if (texture2D == null || webcamTexture.width != texture2D.width || webcamTexture.height != texture2D.height) { texture2D = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32, false); } texture2D.SetPixels(pixels); // texture2D.Apply(false); // Not required. Because we do not need to be uploaded it to GPU byte[] jpg = texture2D.EncodeToJPG(); string base64 = System.Convert.ToBase64String(jpg); #if UNITY_WEBGL Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); #else AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { string url = this.url + this.apiKey; byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (string.IsNullOrEmpty(www.error)) { string result = www.text; if (result != "") { result = result.Substring(result.IndexOf("description") + "description : ".Length, result.IndexOf("boundingPoly") - result.IndexOf("description") - "description : ".Length - 4); //result = result.Replace(@"\n", @"/"); } Debug.Log("JSON format: " + www.text); //JSON format callback(result); yield break; // StopCoroutine("Capture"); // AnnotateImageResponses responses = JsonUtility.FromJson<AnnotateImageResponses>(www.text); //// SendMessage, BroadcastMessage or someting like that. //Sample_OnAnnotateImageResponses(responses); } else { Debug.Log("Error: " + www.error); } } } #endif } }
//private void PrintDebugMessage(string msg) //{ // this.debugMessage.text = msg; //} IEnumerator BeginProcessing() { this.debug.Print("in beginprocessing"); //timestamp the beginning of the processing cycle this._beginProcessingTime = Time.time; //we have the source image. //we need to create a request and invoke the machine vision service //convert the source image into a base64-encoded string byte[] jpg = this._sourceImage.EncodeToJPG(); string base64 = System.Convert.ToBase64String(jpg); //create the request collection object AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); //create the request object AnnotateImageRequest request = new AnnotateImageRequest(); //configure the request object request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); //we are only interested in web detections feature.type = FeatureType.WEB_DETECTION.ToString(); //we only want a single response feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); //creat a MachineVisionClient to post the request MachineVisionClient client = new MachineVisionClient(); //send the request yield return(client.SendWebRequest(requests)); //the MachineVisionClient will send the request and create response object //get a reference to the response object MachineVisionResponse res = client.response; //check if an error was encountered during the request if (res.isError) { //an error occurred during processing Debug.Log("Web detection object is null."); //return a null web detection object this._webDetection = null; //set the DPpedia result to null this.output = new DBpediaResult(); yield break; } //if we get here, then we have successully returned a web detection from the Google Vision API this._webDetection = res.annotateImageResponse.webDetection; //now run the dbpedia queries yield return(StartCoroutine(RunDBPediaQueries())); if (this._dbpediaResult == null) { //the retrieval of results from dbpedia failed, so create a null dbpedia result this.output = new DBpediaResult(); } else { this.output = this._dbpediaResult; } //if we get here then we have a result to process //timestamp the end processing time this._endProcessingTime = Time.time; //mark the processing as completed this._completed = true; this.debug.Print("db-result: " + this._dbpediaResult.Success.ToString()); }
private IEnumerator Capture() { while (true) { if (this.apiKey == null) { yield return(null); } yield return(new WaitForSeconds(captureIntervalSeconds)); Color[] pixels = webcamTexture.GetPixels(); if (pixels.Length == 0) { yield return(null); } if (texture2D == null || webcamTexture.width != texture2D.width || webcamTexture.height != texture2D.height) { texture2D = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32, false); } texture2D.SetPixels(pixels); texture2D.Apply(false); // Not required. Because we do not need to be uploaded it to GPU debugTexture.texture = texture2D; byte[] jpg = texture2D.EncodeToJPG(); string base64 = System.Convert.ToBase64String(jpg); #if UNITY_WEBGL Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); #else AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = "WEB_DETECTION"; feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { Debug.Log(this.url); Debug.Log(this.apiKey); string url = this.url + this.apiKey; Debug.Log(url); byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); Debug.Log(www.text); if (www.error == null) { Debug.Log(www.text.Replace("\n", "").Replace(" ", "")); AnnotateImageResponses responses = JsonUtility.FromJson <AnnotateImageResponses>(www.text); // SendMessage, BroadcastMessage or someting like that. Sample_OnAnnotateImageResponses(responses); if (responses.responses.Count > 0 && responses.responses[0].webDetection.webEntities.Count > 0) { string bestLabel = string.Empty; // parse through and try to find suitable word foreach (WebEntity annotation in responses.responses[0].webDetection.webEntities) { // check if this is one of the banned words --> go to next label if (!bannedWords.Contains(annotation.description.ToLower())) { bestLabel = annotation.description; break; } } // all words were banned, so we need to reprocess if (bestLabel == string.Empty) { StartCoroutine("Capture"); yield return(null); } // set src text to found word srcText.text = bestLabel; StartCoroutine("GoogleSearchAPI"); // make a new text object and set the text to it //Destroy(currentLabel); //currentLabel = Instantiate (label, textSpawnPoint.position, textSpawnPoint.rotation, canvas.transform) as GameObject; //currentLabel.GetComponent<Text>().text = bestLabel; //currentLabel.GetComponent<Text>().text = responses.responses[0].labelAnnotations[0].description; // translate it translate.Process(curr_lang, bestLabel); } } else { Debug.Log("Error: " + www.error); } } } #endif } }
public IEnumerator SendAPIRequest() { receivedResult = false; while (!receivedResult) { if (this.apiKey == null) { Debug.Log("No API key"); yield return(null); } string base64 = System.Convert.ToBase64String(jpg); #if UNITY_WEBGL Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); #else AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { Debug.Log("Sending Request to Google Vision API"); string url = this.url + this.apiKey; byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (string.IsNullOrEmpty(www.error)) { receivedResult = true; //Debug.Log("Parsing Data"); //Debug.Log("WWW Text: " + www.text.Replace("\n", "").Replace(" ", "")); var response = JSON.Parse(www.text); var nameList = GetDescriptionListFromJSON(response); _TextDetection.ReceiveTextList(nameList); } else { Debug.Log("Error: " + www.error); } } } else { Debug.Log("JSON Data string is empty!"); } #endif yield return(new WaitForSeconds(5.0f)); } }
private IEnumerator Capture() { while (true) { if (this.apiKey == null) { yield return(null); } yield return(new WaitForSeconds(captureIntervalSeconds)); Color[] pixels = webcamTexture.GetPixels(); if (pixels.Length == 0) { yield return(null); } if (texture2D == null || webcamTexture.width != texture2D.width || webcamTexture.height != texture2D.height) { texture2D = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32, false); } texture2D.SetPixels(pixels); // texture2D.Apply(false); // Not required. Because we do not need to be uploaded it to GPU byte[] jpg = texture2D.EncodeToJPG(); string base64 = System.Convert.ToBase64String(jpg); #if UNITY_WEBGL Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); #else AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { string url = this.url + this.apiKey; byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (string.IsNullOrEmpty(www.error)) { Debug.Log(www.text.Replace("\n", "").Replace(" ", "")); responses = JsonUtility.FromJson <AnnotateImageResponses>(www.text); Debug.Log(responses.responses); //inisialisasi variable untuk di check bool check = false; //fungsi untuk check nya //jika ada response if (responses.responses[0].textAnnotations != null) { //check untuk setiap keyword foreach (string keyword in keywords) { //operasi OR, klo ada true di 1 kali saja, end result true check = check || responses.responses[0].textAnnotations.Exists(textAnotation => textAnotation.description == keyword); } } if (check) { messageText.enabled = true; } else { messageText.enabled = false; } // SendMessage, BroadcastMessage or someting like that. //Sample_OnAnnotateImageResponses(responses); } else { Debug.Log("Error: " + www.error); } } } #endif } }
private IEnumerator SingleCapture() { if (this.apiKey == null) { yield return(null); } Color[] pixels = webcamTexture.GetPixels(); if (pixels.Length == 0) { yield return(null); } if (texture2D == null || webcamTexture.width != texture2D.width || webcamTexture.height != texture2D.height) { texture2D = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32, false); } texture2D.SetPixels(pixels); // texture2D.Apply(false); // Not required. Because we do not need to be uploaded it to GPU byte[] jpg = texture2D.EncodeToJPG(); string base64 = System.Convert.ToBase64String(jpg); #if UNITY_WEBGL Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); #else AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { string url = this.url + this.apiKey; byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (string.IsNullOrEmpty(www.error)) { //Debug.Log(www.text.Replace("\n", "").Replace(" ", "")); AnnotateImageResponses responses = JsonUtility.FromJson <AnnotateImageResponses>(www.text); // SendMessage, BroadcastMessage or someting like that. Sample_OnAnnotateImageResponses(responses); } else { Debug.Log("Error: " + www.error); } } } #endif }
private IEnumerator Capture() { while (true) { if (this.apiKey == null) { yield return(null); } yield return(new WaitForSeconds(captureIntervalSeconds)); Color[] pixels = webcamTexture.GetPixels(); if (pixels.Length == 0) { yield return(null); } if (texture2D == null || webcamTexture.width != texture2D.width || webcamTexture.height != texture2D.height) { //create new texture captured form webcam texture2D = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32, false); } texture2D.SetPixels(pixels); //convert texture type to jpg byte[] jpg = texture2D.EncodeToJPG(); // convert jpg image to string string base64 = System.Convert.ToBase64String(jpg); // configure json object AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); //convert rquests object to json data string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { string url = this.url + this.apiKey; //convert json data string to bytes byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (string.IsNullOrEmpty(www.error)) { string textAnnotation = GetTextAnnotation(www.text); if (textAnnotation != "") { question.text = GetDescription(textAnnotation); } } else { Debug.Log("Error: " + www.error); } } } } }
private IEnumerator Capture() { if (this.apiKey == null) { yield return(null); } // Take a screenshot, this will freeze the main thread for a short while Color[] pixels = backCam.GetPixels(); if (pixels.Length == 0) { yield return(null); } if (texture2D == null || backCam.width != texture2D.width || backCam.height != texture2D.height) { texture2D = new Texture2D(backCam.width, backCam.height, TextureFormat.RGBA32, false); } // Encode the screenshotted data texture2D.SetPixels(pixels); byte[] jpg = texture2D.EncodeToJPG(); string base64 = System.Convert.ToBase64String(jpg); loadingCircle.SetActive(true); // Create a new request for the API AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest> (); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature> (); // Make sure the right type of request is sent Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { string url = this.url + this.apiKey; byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (string.IsNullOrEmpty(www.error)) { Debug.Log("www text" + www.text); AnnotateImageResponses responses = JsonUtility.FromJson <AnnotateImageResponses> (www.text); Debug.Log("eerste log na annotate" + responses); parseDetectedText(responses); } else { Debug.Log("Error: " + www.error); } } } }
IEnumerator GetVisionFromImages() { // 解析開始 Debug.Log("GetVisionFromImages スタート!"); text.text = "解析開始"; if (apiKey == null || apiKey == "") { Debug.LogError("No API key. Please set your API key into the \"Web Cam Texture To Cloud Vision(Script)\" component."); } // 撮影したデータを取得しbase64に変換する byte[] bytes = File.ReadAllBytes(fileName); string base64 = Convert.ToBase64String(bytes); var headers = new Dictionary <string, string>() { { "Content-Type", "application/json;" } }; AnnotateImageRequests requests = new AnnotateImageRequests(); requests.requests = new List <AnnotateImageRequest>(); AnnotateImageRequest request = new AnnotateImageRequest(); request.image = new Image(); request.image.content = base64; request.features = new List <Feature>(); Feature feature = new Feature(); feature.type = this.featureType.ToString(); feature.maxResults = this.maxResults; request.features.Add(feature); requests.requests.Add(request); string jsonData = JsonUtility.ToJson(requests, false); if (jsonData != string.Empty) { // Google Vision APIに解析を依頼する string url = this.url + this.apiKey; byte[] postData = System.Text.Encoding.UTF8.GetBytes(jsonData); using (WWW www = new WWW(url, postData, headers)) { yield return(www); if (www.error == null) { responseData = www.text; Debug.Log("GetEmotionFromImages 終了!"); text.text = ""; Debug.Log(responseData.Replace("\n", "").Replace(" ", "")); AnnotateImageResponses responses = JsonUtility.FromJson <AnnotateImageResponses>(responseData); Sample_OnAnnotateImageResponses(responses); } else { Debug.Log("Error: " + www.error); } } } }