Exemplo n.º 1
0
    // 画像データを与えるとGoogleCloudVisionでラベル検知したやつを返す。
    public static responseBody RequestVisionAPI(string base64String)
    {
        // 参考:https://qiita.com/jyuko/items/e6115a5dfc959f52591d

        string apiKey = "AIzaSyALq6mj-H1c2HKrXubWzhsPtUCxni_Z5_I";
        string url    = "https://vision.googleapis.com/v1/images:annotate?key=" + apiKey;

        // 送信用データを作成

        // 1.requestBodyを作成
        var requests = new requestBody();

        requests.requests = new List <AnnotateImageRequest>();

        // 2.requestBody > requestを作成
        var request = new AnnotateImageRequest();

        request.image         = new Image();
        request.image.content = base64String;

        // 3.requestBody > request > featureを作成
        request.features = new List <Feature>();
        var feature = new Feature();

        feature.type       = FeatureType.LABEL_DETECTION.ToString();
        feature.maxResults = 10;
        request.features.Add(feature);

        requests.requests.Add(request);

        // JSONに変換
        string jsonRequestBody = JsonUtility.ToJson(requests);

        return(JsonUtility.FromJson <responseBody>(Communication(url, jsonRequestBody)));
    }
Exemplo n.º 2
0
        /// <summary>
        /// Takes in the parameters necessary to create a valid Session Model
        /// Converts the array contaning the position of appearances in the search to a strin g
        /// calls the corresponding repo to save the session to the database
        /// </summary>
        /// <returns></returns>
        public Session saveSession(requestBody request, string requestedUrl, int[] appearances)
        {
            string appearancesString = "";

            for (int i = 0; i < appearances.Length; i++)
            {
                if (i + 1 != appearances.Length)
                {
                    appearancesString += appearances[i].ToString() + ",";
                }
                else
                {
                    appearancesString += appearances[i].ToString();
                }
            }
            Session session = new Session()
            {
                requestTime     = DateTime.Now,
                keyWords        = request.keyWords,
                requestedUrl    = requestedUrl,
                query           = request.query,
                appearedList    = appearancesString,
                numberOfResults = request.numOfResults
            };

            sessionRepo.saveSession(session);

            return(session);
        }
Exemplo n.º 3
0
    /// <summary>
    /// 画像データを与えるとGoogleCloudVisionでラベル検知したやつを返す。
    /// </summary>
    /// <param name="imageData"></param>
    /// <returns></returns>
    public static EntityAnnotation[] RequestVisionAPI(string base64String)
    {
        // 参考:https://qiita.com/jyuko/items/e6115a5dfc959f52591d

        string apiKey = "AIzaSyAQe-ZtCVwWx0xIco4b9U3dpbk83MoLv_c";
        string url    = "https://vision.googleapis.com/v1/images:annotate?key=" + apiKey;

        // 送信用データを作成

        // 1.requestBodyを作成
        var requests = new requestBody();

        requests.requests = new List <AnnotateImageRequest>();

        // 2.requestBody > requestを作成
        var request = new AnnotateImageRequest();

        request.image         = new Image();
        request.image.content = base64String;

        // 3.requestBody > request > featureを作成
        request.features = new List <Feature>();
        var feature = new Feature();

        feature.type       = FeatureType.LABEL_DETECTION.ToString();
        feature.maxResults = 10;
        request.features.Add(feature);

        requests.requests.Add(request);

        // JSONに変換
        string jsonRequestBody = JsonUtility.ToJson(requests);

        // ヘッダを"application/json"にして投げる
        var webRequest = new UnityWebRequest(url, "POST");

        byte[] postData = Encoding.UTF8.GetBytes(jsonRequestBody);
        webRequest.uploadHandler   = new UploadHandlerRaw(postData);
        webRequest.downloadHandler = new DownloadHandlerBuffer();
        webRequest.SetRequestHeader("Content-Type", "application/json");

        webRequest.SendWebRequest();
        // 受信するまで待機
        while (!webRequest.isDone)
        {
        }

        var buff = JsonUtility.FromJson <responseBody>(webRequest.downloadHandler.text);

        return(CheckFunction.removeAnnotation(buff.responses[0].labelAnnotations.ToArray()));
    }
Exemplo n.º 4
0
        /// <summary>
        ///Entry point to deal with  the check request for the controller.
        /// This method takes in the request body from the controller creates the URL for the corresponding google
        /// Processes the response then returns a Dictionary containing:
        /// The key words for the google search and the query/ URl from the body
        /// The google search URL
        /// An array indicating where the key words appeared in the search
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Dictionary <string, object> check(requestBody request)
        {
            List <int>    appearancesInSearch = new List <int>();
            List <string> urlList             = new List <string>();
            List <string> rawResultsList;

            string requestURL = createURL(request.keyWords, request.numOfResults);
            String rawhtml    = getPage(requestURL);

            string formattedhtml = formatPage(rawhtml);

            rawResultsList = seperateResult(formattedhtml, request.numOfResults);


            for (int i = 0; i < request.numOfResults; i++)
            {
                string resultUrl = getURL(rawResultsList[i]);
                urlList.Add(resultUrl);
                if (resultUrl.Trim().Contains(request.query.Trim()) || resultUrl.Trim() == request.query.Trim())
                {
                    appearancesInSearch.Add(i + 1);
                }
            }

            Session session = saveSession(request, requestURL, appearancesInSearch.ToArray());

            saveResults(session, urlList.ToArray());

            Dictionary <string, object> response = new Dictionary <string, object>();

            response.Add("appeared", appearancesInSearch);
            response.Add("requestedURL", requestURL);
            response.Add("query", request.query);
            response.Add("keyWords", request.keyWords);

            return(response);
        }
Exemplo n.º 5
0
 => Initialize(requestBody, responseBody, request, response);
Exemplo n.º 6
0
    static public IEnumerator GetCharOfImage(string base64Image, Action <string> callback)
    {
        // requestBodyを作成
        var requests = new requestBody
        {
            requests = new List <AnnotateImageRequest>()
        };

        var request = new AnnotateImageRequest
        {
            image = new Image
            {
                content = base64Image
            },

            imageContext = new ImageContext
            {
                languageHints = "ja-JP"
            },

            features = new List <Feature>()
        };

        var feature = new Feature
        {
            //type = FeatureType.TEXT_DETECTION.ToString(),
            type       = "TEXT_DETECTION",
            maxResults = 10
        };

        request.features.Add(feature);

        requests.requests.Add(request);

        // JSONに変換
        string jsonRequestBody = JsonUtility.ToJson(requests);

        // ヘッダを"application/json"にして投げる
        var webRequest = new UnityWebRequest(url, "POST");

        byte[] postData = Encoding.UTF8.GetBytes(jsonRequestBody);
        webRequest.uploadHandler   = new UploadHandlerRaw(postData);
        webRequest.downloadHandler = new DownloadHandlerBuffer();
        webRequest.SetRequestHeader("Content-Type", "application/json");

        yield return(webRequest.Send());

        if (webRequest.isError)
        {
            // エラー時の処理
            Debug.Log(webRequest.error);
        }
        else
        {
            // 成功時の処理
            Debug.Log(webRequest.downloadHandler.text);
            var responses = JsonUtility.FromJson <ResponseBody>(webRequest.downloadHandler.text);

            if (responses.responses[0].textAnnotations.Count == 0) //読み込み失敗などレスポンスが帰ってこなかった時
            {
                callback("認識失敗");
            }
            else
            {
                Debug.Log(responses.responses[0].textAnnotations[0].description);
                callback(responses.responses[0].textAnnotations[0].description);
            }
        }
    }
Exemplo n.º 7
0
    public IEnumerator request(string base64Image)
    {
        if (uploading)
        {
            yield break;
        }

        uploading = true;

        // requestBodyを作成
        var requests = new requestBody();

        requests.requests = new List <AnnotateImageRequest>();

        var request = new AnnotateImageRequest();

        request.image         = new Image();
        request.image.content = base64Image;

        request.features = new List <Feature>();
        var feature = new Feature();

        feature.type       = FeatureType.LABEL_DETECTION.ToString();
        feature.maxResults = 10;
        request.features.Add(feature);

        requests.requests.Add(request);

        // JSONに変換
        string jsonRequestBody = JsonUtility.ToJson(requests);

        // ヘッダを"application/json"にして投げる
        UnityWebRequest www = UnityWebRequest.Post(url, "POST");

        byte[] postData = Encoding.UTF8.GetBytes(jsonRequestBody);
        www.uploadHandler   = (UploadHandler) new UploadHandlerRaw(postData);
        www.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        www.SetRequestHeader("Content-Type", "application/json");

        yield return(www.SendWebRequest());

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
            response = www.error;
        }
        else
        {
            Debug.Log("Form upload complete!");
            if (www.responseCode == 200)
            {
                //Debug.Log(www.downloadHandler.text);
                response = www.downloadHandler.text;
            }
            else
            {
                Debug.Log("failed");
                response = "Request failed. HTTP Response code: " + www.responseCode;
            }
        }
        uploading = false;
        yield break;
    }
Exemplo n.º 8
0
        public Object scrape([FromBody] requestBody request)
        {
            Dictionary <string, Object> response = scraperService.check(request);

            return(response);
        }
Exemplo n.º 9
0
    private IEnumerator requestVisionAPI(string base64Image)
    {
        string apiKey = "your key";
        string url    = "https://vision.googleapis.com/v1/images:annotate?key=" + apiKey;

        // requestBodyを作成
        var requests = new requestBody();

        requests.requests = new List <AnnotateImageRequest>();

        var request = new AnnotateImageRequest();

        request.image         = new Image();
        request.image.content = base64Image;

        request.features = new List <Feature>();
        var feature = new Feature();

        feature.type       = FeatureType.TEXT_DETECTION.ToString();
        feature.maxResults = 10;
        request.features.Add(feature);

        requests.requests.Add(request);

        // JSON
        string jsonRequestBody = JsonUtility.ToJson(requests);

        // "application/json"
        var webRequest = new UnityWebRequest(url, "POST");

        byte[] postData = Encoding.UTF8.GetBytes(jsonRequestBody);
        webRequest.uploadHandler   = (UploadHandler) new UploadHandlerRaw(postData);
        webRequest.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        webRequest.SetRequestHeader("Content-Type", "application/json");

        yield return(webRequest.SendWebRequest());

        // when google cloud vision responds
        if (webRequest.isNetworkError)
        {
            Debug.Log("Error: " + webRequest.error);
        }
        else
        {
            var responses = JsonUtility.FromJson <responseOCR>(webRequest.downloadHandler.text);

            string data = System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data);

            Debug.Log("Response: " + data);

            String textInImage = string.Empty;
            if (responses.responses.Count() == 1)
            {
                if (responses.responses.First().fullTextAnnotation.text != null)
                {
                    textInImage = responses.responses.First().fullTextAnnotation.text;
                }
            }

            textOCR.text = textInImage;
        }
    }