private static async Task <Windows.Web.Http.IHttpContent> ConvertRequestContentAsync(HttpContent content)
    {
        if (content == null)
        {
            return(null);
        }
        Stream contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

        var result = new Windows.Web.Http.HttpStreamContent(contentStream.AsInputStream());

        CopyHeaders(content.Headers, result.Headers);
        return(result);
    }
Beispiel #2
0
        public static async Task <string> headUpload(string stunum, string fileUri, string uri = "http://hongyan.cqupt.edu.cn/cyxbsMobile/index.php/home/Photo/upload", bool isPath = false)
        {
            Windows.Web.Http.HttpClient _httpClient = new Windows.Web.Http.HttpClient();
            CancellationTokenSource     _cts        = new CancellationTokenSource();

            Windows.Web.Http.HttpStringContent stunumStringContent = new Windows.Web.Http.HttpStringContent(stunum);
            string head = "";
            //IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            IStorageFile saveFile;

            if (isPath)
            {
                saveFile = await StorageFile.GetFileFromPathAsync(fileUri);
            }
            else
            {
                saveFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(fileUri));
            }

            try
            {
                // 构造需要上传的文件数据
                IRandomAccessStreamWithContentType stream1 = await saveFile.OpenReadAsync();

                Windows.Web.Http.HttpStreamContent            streamContent = new Windows.Web.Http.HttpStreamContent(stream1);
                Windows.Web.Http.HttpMultipartFormDataContent fileContent   = new Windows.Web.Http.HttpMultipartFormDataContent();

                fileContent.Add(streamContent, "fold", "head.png");
                fileContent.Add(stunumStringContent, "stunum");

                Windows.Web.Http.HttpResponseMessage response =
                    await
                    _httpClient.PostAsync(new Uri(uri), fileContent)
                    .AsTask(_cts.Token);

                head = Utils.ConvertUnicodeStringToChinese(await response.Content.ReadAsStringAsync().AsTask(_cts.Token));
                Debug.WriteLine(head);
                return(head);
            }
            catch (Exception)
            {
                Debug.WriteLine("上传头像失败,编辑页面");
                return("");
            }
        }
        /// <summary>
        /// Converts the convent from System.Net.Http.HttpContent to Windows.Web.Http.IHttpContent.
        /// </summary>
        /// <param name="content">The System.Net.Http.HttpContent to convert.</param>
        /// <returns>Converted Windows.Web.Http.IHttpContent.</returns>
        private static async Task <Windows.Web.Http.IHttpContent> ConvertContent(HttpContent content)
        {
            // If no content to convert
            if (content == null)
            {
                return(null);
            }

            // Convert the content
            var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);

            var converted = new Windows.Web.Http.HttpStreamContent(stream.AsInputStream());

            // Copy content headers
            WindowsHttpMessageHandler.CopyHeaders(content.Headers, converted.Headers);

            // Return the converted content
            return(converted);
        }
        public static async Task <string> ReadRequest(StreamSocket socket)
        {
            var httpStreamContent = new Windows.Web.Http.HttpStreamContent(socket.InputStream);

            var stringContent = await httpStreamContent.ReadAsInputStreamAsync();

            var request = new StringBuilder();

            using (var input = stringContent)
            {
                var data     = new byte[BufferSize];
                var buffer   = data.AsBuffer();
                var dataRead = BufferSize;
                while (dataRead == BufferSize)
                {
                    await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);

                    request.Append(Encoding.UTF8.GetString(data, 0, data.Length));
                    dataRead = buffer.Length;
                }
            }
            return(request.ToString());
        }
        public static async Task<string> headUpload(string stunum, string fileUri, string uri = "http://hongyan.cqupt.edu.cn/cyxbsMobile/index.php/home/Photo/upload", bool isPath = false)
        {
            Windows.Web.Http.HttpClient _httpClient = new Windows.Web.Http.HttpClient();
            CancellationTokenSource _cts = new CancellationTokenSource();
            Windows.Web.Http.HttpStringContent stunumStringContent = new Windows.Web.Http.HttpStringContent(stunum);
            string head = "";
            //IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            IStorageFile saveFile;
            if (isPath)
                saveFile = await StorageFile.GetFileFromPathAsync(fileUri);
            else
                saveFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(fileUri));

            try
            {
                // 构造需要上传的文件数据
                IRandomAccessStreamWithContentType stream1 = await saveFile.OpenReadAsync();
                Windows.Web.Http.HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(stream1);
                Windows.Web.Http.HttpMultipartFormDataContent fileContent = new Windows.Web.Http.HttpMultipartFormDataContent();

                fileContent.Add(streamContent, "fold", "head.png");
                fileContent.Add(stunumStringContent, "stunum");

                Windows.Web.Http.HttpResponseMessage response =
                    await
                        _httpClient.PostAsync(new Uri(uri), fileContent)
                            .AsTask(_cts.Token);
                head = Utils.ConvertUnicodeStringToChinese(await response.Content.ReadAsStringAsync().AsTask(_cts.Token));
                Debug.WriteLine(head);
                return head;
            }
            catch (Exception)
            {
                Debug.WriteLine("上传头像失败,编辑页面");
                return "";
            }
        }
        private static async Task <RTIHttpContent> CreateRequestContentAsync(HttpRequestMessage request, HttpRequestHeaderCollection rtHeaderCollection)
        {
            HttpContent content = request.Content;

            RTIHttpContent      rtContent;
            ArraySegment <byte> buffer;

            // If we are buffered already, it is more efficient to send the data directly using the buffer with the
            // WinRT HttpBufferContent class than using HttpStreamContent. This also avoids issues caused by
            // a design limitation in the System.Runtime.WindowsRuntime System.IO.NetFxToWinRtStreamAdapter.
            if (content.TryGetBuffer(out buffer))
            {
                rtContent = new RTHttpBufferContent(buffer.Array.AsBuffer(), (uint)buffer.Offset, (uint)buffer.Count);
            }
            else
            {
                Stream contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

                if (contentStream is RTIInputStream)
                {
                    rtContent = new RTHttpStreamContent((RTIInputStream)contentStream);
                }
                else if (contentStream is MemoryStream)
                {
                    var memStream = contentStream as MemoryStream;
                    if (memStream.TryGetBuffer(out buffer))
                    {
                        rtContent = new RTHttpBufferContent(buffer.Array.AsBuffer(), (uint)buffer.Offset, (uint)buffer.Count);
                    }
                    else
                    {
                        byte[] byteArray = memStream.ToArray();
                        rtContent = new RTHttpBufferContent(byteArray.AsBuffer(), 0, (uint)byteArray.Length);
                    }
                }
                else
                {
                    rtContent = new RTHttpStreamContent(contentStream.AsInputStream());
                }
            }

            // RTHttpBufferContent constructor automatically adds a Content-Length header. RTHttpStreamContent does not.
            // Clear any 'Content-Length' header added by the RTHttp*Content objects. We need to clear that now
            // and decide later whether we need 'Content-Length' or 'Transfer-Encoding: chunked' headers based on the
            // .NET HttpRequestMessage and Content header collections.
            rtContent.Headers.ContentLength = null;

            // Deal with conflict between 'Content-Length' vs. 'Transfer-Encoding: chunked' semantics.
            // Desktop System.Net allows both headers to be specified but ends up stripping out
            // 'Content-Length' and using chunked semantics.  The WinRT APIs throw an exception so
            // we need to manually strip out the conflicting header to maintain app compatibility.
            if (request.Headers.TransferEncodingChunked.HasValue && request.Headers.TransferEncodingChunked.Value)
            {
                content.Headers.ContentLength = null;
            }
            else
            {
                // Trigger delayed header generation via TryComputeLength. This code is needed due to an outstanding
                // bug in HttpContentHeaders.ContentLength. See GitHub Issue #5523.
                content.Headers.ContentLength = content.Headers.ContentLength;
            }

            foreach (KeyValuePair <string, IEnumerable <string> > headerPair in content.Headers)
            {
                foreach (string value in headerPair.Value)
                {
                    if (!rtContent.Headers.TryAppendWithoutValidation(headerPair.Key, value))
                    {
                        // rtContent headers are restricted to a white-list of allowed headers, while System.Net.HttpClient's content headers
                        // will allow custom headers.  If something is not successfully added to the content headers, try adding them to the standard headers.
                        bool success = rtHeaderCollection.TryAppendWithoutValidation(headerPair.Key, value);
                        Debug.Assert(success);
                    }
                }
            }
            return(rtContent);
        }
Beispiel #7
0
        /// <summary>
        /// SendVisionPicture method
        /// </summary>
        /// <param name="subscriptionKey">service key
        /// </param>
        /// <param name="hostname">hostname associated with the service url
        /// </param>
        /// <param name="visualFeatures">visual features for the request: tags, ...
        /// </param>
        /// <param name="details">detail information for the request:
        /// </param>
        /// <param name="lang">language for the response chinese or english so far
        /// </param>
        /// <param name="pictureFile">StorageFile associated with the picture file which
        /// will be sent to the Vision Services.
        /// </param>
        /// <return>The result of the Vision REST API.
        /// </return>
        public IAsyncOperation <VisionResponse> SendVisionPicture(string subscriptionKey, string hostname, string visualFeatures, string details, string lang, Windows.Storage.StorageFile pictureFile)
        {
            return(Task.Run <VisionResponse>(async() =>
            {
                VisionResponse r = null;

                try
                {
                    string visionUrl = string.Format(VisionUrl, hostname, visualFeatures, details, lang);
                    Windows.Web.Http.HttpClient hc = new Windows.Web.Http.HttpClient();

                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("Ocp-Apim-Subscription-Key", subscriptionKey);
                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("ContentType", "application/octet-stream");

                    Windows.Web.Http.HttpResponseMessage hrm = null;

                    Windows.Storage.StorageFile file = pictureFile;
                    if (file != null)
                    {
                        using (var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                        {
                            if (fileStream != null)
                            {
                                Windows.Web.Http.HttpStreamContent content = new Windows.Web.Http.HttpStreamContent(fileStream.AsStream().AsInputStream());
                                System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource();
                                IProgress <Windows.Web.Http.HttpProgress> progress = new Progress <Windows.Web.Http.HttpProgress>(ProgressHandler);
                                content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/octet-stream");

                                hrm = await hc.PostAsync(new Uri(visionUrl), content).AsTask(cts.Token, progress);
                            }
                        }
                    }
                    if (hrm != null)
                    {
                        switch (hrm.StatusCode)
                        {
                        case Windows.Web.Http.HttpStatusCode.Ok:
                            var b = await hrm.Content.ReadAsBufferAsync();
                            string result = System.Text.UTF8Encoding.UTF8.GetString(b.ToArray());
                            if (!string.IsNullOrEmpty(result))
                            {
                                r = new VisionResponse(result, null);
                            }
                            break;

                        default:
                            int code = (int)hrm.StatusCode;
                            string HttpError = "Http Response Error: " + code.ToString() + " reason: " + hrm.ReasonPhrase.ToString();
                            System.Diagnostics.Debug.WriteLine(HttpError);
                            r = new VisionResponse(string.Empty, HttpError);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception while sending the audio file:" + ex.Message);
                }

                return r;
            }).AsAsyncOperation <VisionResponse>());
        }
        private async Task ClassifyAllFiles()
        {
            Debug.WriteLine("-- GETTING FILES");
            StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

            StorageFolder dataFolder = await appInstalledFolder.GetFolderAsync(GlobalData.dataFolder);

            IReadOnlyList <StorageFile> fileList = await dataFolder.GetFilesAsync();

            var result = new ObservableCollection <BitmapImage>();

            Speech.Speak("Processing of images for Invasive Ductal Carcinoma initiating");
            Debug.WriteLine(" ");
            int received   = 0;
            int counter    = 0;
            int identified = 0;
            int incorrect  = 0;
            int unsure     = 0;
            int tns        = 0;
            int fps        = 0;
            int fns        = 0;

            foreach (StorageFile file in fileList)
            {
                Debug.WriteLine(file.Name);
                IBuffer buffer = await FileIO.ReadBufferAsync(file);

                byte[] bytes    = buffer.ToArray();
                Stream streamer = new MemoryStream(bytes);
                Windows.Web.Http.HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(streamer.AsInputStream());

                var myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                myFilter.AllowUI = false;
                var client = new Windows.Web.Http.HttpClient(myFilter);
                Windows.Web.Http.HttpResponseMessage results = await client.PostAsync(new Uri(GlobalData.protocol + GlobalData.ip + ":" + GlobalData.port + GlobalData.endpointIDC), streamContent);

                string stringReadResult = await results.Content.ReadAsStringAsync();

                Debug.WriteLine(stringReadResult);

                JToken token = JObject.Parse(stringReadResult);
                received = (int)token.SelectToken("Results");
                double confidence = (double)token.SelectToken("Confidence");
                string Response   = (string)token.SelectToken("ResponseMessage");

                if (received != 0)
                {
                    if (file.Name.Contains("class1"))
                    {
                        if (confidence >= GlobalData.threshold)
                        {
                            Debug.WriteLine("CORRECT: IDC correctly detected in image " + (counter + 1) + " " + file.Name + " with " + confidence + " confidence.");
                            Debug.WriteLine(" ");
                            identified = identified + 1;
                        }
                        else
                        {
                            Debug.WriteLine("UNSURE: IDC detected in image " + (counter + 1) + " " + file.Name + " with " + confidence + " confidence.");
                            unsure = unsure + 1;
                        }
                    }
                    else
                    {
                        if (confidence >= GlobalData.threshold)
                        {
                            Debug.WriteLine("FALSE POSITIVE: IDC incorrectly detected in image " + (counter + 1) + " " + file.Name + " with " + confidence + " confidence.");
                            fps       = fps + 1;
                            incorrect = incorrect + 1;
                        }
                        else
                        {
                            Debug.WriteLine("UNSURE: IDC detected in image " + (counter + 1) + " " + file.Name + " with " + confidence + " confidence.");
                            unsure = unsure + 1;
                        }
                    }
                    Speech.Speak("Processed image " + (counter + 1));
                }
                else
                {
                    if (file.Name.Contains("class0"))
                    {
                        Debug.WriteLine("CORRECT: IDC correctly not detected in image " + (counter + 1) + " " + file.Name + " with " + confidence + " confidence.");
                        tns = tns + 1;
                    }
                    else
                    {
                        if (confidence >= GlobalData.threshold)
                        {
                            Debug.WriteLine("FALSE NEGATIVE: IDC incorrectly not detected in image " + (counter + 1) + " " + file.Name + " with " + confidence + " confidence.");
                            fns       = fns + 1;
                            incorrect = incorrect + 1;
                        }
                        else
                        {
                            Debug.WriteLine("UNSURE: IDC detected in image " + (counter + 1) + " " + file.Name + " with " + confidence + " confidence.");
                            unsure = unsure + 1;
                        }
                    }
                    Speech.Speak("Processed image " + (counter + 1));
                    Debug.WriteLine(" ");
                }
                counter++;
                if (counter == (GlobalData.expectedCount * 2))
                {
                    double actualIdentified = (double)identified;
                    double accuracy         = (((double)tns + actualIdentified) / (actualIdentified + (double)fps)) / ((double)counter - unsure);
                    double precision        = actualIdentified / (actualIdentified + (double)fps);
                    double recall           = actualIdentified / (actualIdentified + (double)fns);
                    double fscore           = 2 * ((double)precision * (double)recall / ((double)precision + (double)recall));

                    Speech.Speak(identified + " true positives, " + fps + " false positives, " + fns + " false negatives, " + unsure + " unsure, " + tns + " true negatives, " + incorrect + " incorrect examples classified, " + Math.Round(accuracy, 2) + " accuracy, " + Math.Round(precision, 2) + " precision, " + Math.Round(recall, 2) + " recall, " + Math.Round(fscore, 2) + " fscore");

                    Debug.WriteLine(" ");
                    Debug.WriteLine("- " + identified + " true positives, " + fps + " false positives, " + fns + " false negatives, " + tns + " true negatives");
                    Debug.WriteLine("- " + unsure + " unsure");
                    Debug.WriteLine("- " + incorrect + " incorrect examples classified");
                    Debug.WriteLine("- " + Math.Round(accuracy, 2) + " accuracy");
                    Debug.WriteLine("- " + Math.Round(precision, 2) + " precision");
                    Debug.WriteLine("- " + Math.Round(recall, 2) + " recall");
                    Debug.WriteLine("- " + Math.Round(fscore, 2) + " fscore");
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
        private static async Task<RTIHttpContent> CreateRequestContentAsync(HttpRequestMessage request, RTHttpRequestHeaderCollection rtHeaderCollection)
        {
            HttpContent content = request.Content;

            RTIHttpContent rtContent;
            ArraySegment<byte> buffer;

            // If we are buffered already, it is more efficient to send the data directly using the buffer with the
            // WinRT HttpBufferContent class than using HttpStreamContent. This also avoids issues caused by
            // a design limitation in the System.Runtime.WindowsRuntime System.IO.NetFxToWinRtStreamAdapter.
            Stream contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

            if (contentStream is RTIInputStream)
            {
                rtContent = new RTHttpStreamContent((RTIInputStream)contentStream);
            }
            else if (contentStream is MemoryStream)
            {
                var memStream = contentStream as MemoryStream;
                if (memStream.TryGetBuffer(out buffer))
                {
                    rtContent = new RTHttpBufferContent(buffer.Array.AsBuffer(), (uint)buffer.Offset, (uint)buffer.Count);
                }
                else
                {
                    byte[] byteArray = memStream.ToArray();
                    rtContent = new RTHttpBufferContent(byteArray.AsBuffer(), 0, (uint)byteArray.Length);
                }
            }
            else
            {
                rtContent = new RTHttpStreamContent(contentStream.AsInputStream());
            }

            // RTHttpBufferContent constructor automatically adds a Content-Length header. RTHttpStreamContent does not.
            // Clear any 'Content-Length' header added by the RTHttp*Content objects. We need to clear that now
            // and decide later whether we need 'Content-Length' or 'Transfer-Encoding: chunked' headers based on the
            // .NET HttpRequestMessage and Content header collections.
            rtContent.Headers.ContentLength = null;

            // Deal with conflict between 'Content-Length' vs. 'Transfer-Encoding: chunked' semantics.
            // Desktop System.Net allows both headers to be specified but ends up stripping out
            // 'Content-Length' and using chunked semantics.  The WinRT APIs throw an exception so
            // we need to manually strip out the conflicting header to maintain app compatibility.
            if (request.Headers.TransferEncodingChunked.HasValue && request.Headers.TransferEncodingChunked.Value)
            {
                content.Headers.ContentLength = null;
            }
            else
            {
                // Trigger delayed header generation via TryComputeLength. This code is needed due to an outstanding
                content.Headers.ContentLength = content.Headers.ContentLength;
            }

            foreach (KeyValuePair<string, IEnumerable<string>> headerPair in content.Headers)
            {
                foreach (string value in headerPair.Value)
                {
                    if (!rtContent.Headers.TryAppendWithoutValidation(headerPair.Key, value))
                    {
                        // rtContent headers are restricted to a white-list of allowed headers, while System.Net.HttpClient's content headers 
                        // will allow custom headers.  If something is not successfully added to the content headers, try adding them to the standard headers.
                        bool success = rtHeaderCollection.TryAppendWithoutValidation(headerPair.Key, value);
                        Debug.Assert(success);
                    }
                }
            }
            return rtContent;
        }
        /// <summary>
        /// SendStorageFile method
        /// </summary>
        /// <param name="wavFile">StorageFile associated with the audio file which
        /// will be sent to the SpeechToText Services.
        /// </param>
        /// <param name="locale">language associated with the current buffer/recording.
        /// for instance en-US, fr-FR, pt-BR, ...
        /// </param>
        /// <return>The result of the SpeechToText REST API.
        /// </return>
        public async System.Threading.Tasks.Task <SpeechToTextResponse> SendStorageFile(Windows.Storage.StorageFile wavFile, string locale)
        {
            SpeechToTextResponse r = null;
            int loop = 1;

            while (loop-- > 0)
            {
                try
                {
                    string os        = "Windows" + SpeechToText.SystemInformation.SystemVersion;
                    string deviceid  = "b2c95ede-97eb-4c88-81e4-80f32d6aee54";
                    string speechUrl = SpeechUrl + "?scenarios=ulm&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&version=3.0&device.os=" + os + "&locale=" + locale + "&format=json&requestid=" + Guid.NewGuid().ToString() + "&instanceid=" + deviceid + "&result.profanitymarkup=1&maxnbest=3";
                    Windows.Web.Http.HttpClient hc = new Windows.Web.Http.HttpClient();

                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("Authorization", Token);
                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("ContentType", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
                    Windows.Web.Http.HttpResponseMessage hrm = null;

                    Windows.Storage.StorageFile file = wavFile;
                    if (file != null)
                    {
                        using (var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                        {
                            if (STTStream != null)
                            {
                                STTStream.AudioLevel -= STTStream_AudioLevel;
                                STTStream.Dispose();
                                STTStream = null;
                            }
                            STTStream = SpeechToTextMainStream.Create();
                            if (STTStream != null)
                            {
                                byte[] byteArray = new byte[fileStream.Size];
                                fileStream.ReadAsync(byteArray.AsBuffer(), (uint)fileStream.Size, Windows.Storage.Streams.InputStreamOptions.Partial).AsTask().Wait();
                                STTStream.WriteAsync(byteArray.AsBuffer()).AsTask().Wait();

                                Windows.Web.Http.HttpStreamContent content = new Windows.Web.Http.HttpStreamContent(STTStream.AsStream().AsInputStream());
                                content.Headers.ContentLength = STTStream.GetLength();
                                System.Diagnostics.Debug.WriteLine("REST API Post Content Length: " + content.Headers.ContentLength.ToString() + " bytes");
                                System.Threading.CancellationTokenSource  cts      = new System.Threading.CancellationTokenSource();
                                IProgress <Windows.Web.Http.HttpProgress> progress = new Progress <Windows.Web.Http.HttpProgress>(ProgressHandler);
                                hrm = await hc.PostAsync(new Uri(speechUrl), content).AsTask(cts.Token, progress);
                            }
                        }
                    }
                    if (hrm != null)
                    {
                        switch (hrm.StatusCode)
                        {
                        case Windows.Web.Http.HttpStatusCode.Ok:
                            var b = await hrm.Content.ReadAsBufferAsync();

                            string result = System.Text.UTF8Encoding.UTF8.GetString(b.ToArray());
                            if (!string.IsNullOrEmpty(result))
                            {
                                r = new SpeechToTextResponse(result);
                            }
                            break;

                        case Windows.Web.Http.HttpStatusCode.Forbidden:
                            string token = await RenewToken();

                            if (string.IsNullOrEmpty(token))
                            {
                                loop++;
                            }
                            break;

                        default:
                            int    code      = (int)hrm.StatusCode;
                            string HttpError = "Http Response Error: " + code.ToString() + " reason: " + hrm.ReasonPhrase.ToString();
                            System.Diagnostics.Debug.WriteLine(HttpError);
                            r = new SpeechToTextResponse(string.Empty, HttpError);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception while sending the audio file:" + ex.Message);
                }
            }
            return(r);
        }
        /// <summary>
        /// SendAudioStream method
        /// This method sends a SpeechToTextAudioStream towards Cognitive Services REST API.
        /// Usually, the method GetAudioStream returns the SpeechToTextAudioStream (if available) then the method SendAudioStream
        /// sends a SpeechToTextAudioStream towards Cognitive Services REST API.
        /// </summary>
        /// <param name="locale">language associated with the current buffer/recording.
        /// for instance en-US, fr-FR, pt-BR, ...
        /// </param>
        /// <param name="stream">AudioStream which will be forwarded to REST API.
        /// </param>
        /// <return>The result of the SpeechToText REST API.
        /// </return>
        public async System.Threading.Tasks.Task <SpeechToTextResponse> SendAudioStream(string locale, SpeechToTextAudioStream stream)
        {
            SpeechToTextResponse r = null;
            int loop = 1;

            while (loop-- > 0)
            {
                try
                {
                    string os        = "Windows" + SpeechToText.SystemInformation.SystemVersion;
                    string deviceid  = "b2c95ede-97eb-4c88-81e4-80f32d6aee54";
                    string speechUrl = SpeechUrl + "?scenarios=ulm&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&version=3.0&device.os=" + os + "&locale=" + locale + "&format=json&requestid=" + Guid.NewGuid().ToString() + "&instanceid=" + deviceid + "&result.profanitymarkup=1&maxnbest=3";
                    Windows.Web.Http.HttpClient hc = new Windows.Web.Http.HttpClient();
                    System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource();
                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("Authorization", Token);
                    Windows.Web.Http.HttpResponseMessage hrm     = null;
                    Windows.Web.Http.HttpStreamContent   content = null;
                    content = new Windows.Web.Http.HttpStreamContent(stream.GetInputStreamAt(0));
                    content.Headers.ContentLength = (ulong)stream.Size;
                    if ((content != null) && (content.Headers.ContentLength > 0))
                    {
                        System.Diagnostics.Debug.WriteLine("REST API Post Content Length: " + content.Headers.ContentLength.ToString());
                        content.Headers.TryAppendWithoutValidation("ContentType", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
                        IProgress <Windows.Web.Http.HttpProgress> progress = new Progress <Windows.Web.Http.HttpProgress>(ProgressHandler);
                        hrm = await hc.PostAsync(new Uri(speechUrl), content).AsTask(cts.Token, progress);
                    }
                    if (hrm != null)
                    {
                        switch (hrm.StatusCode)
                        {
                        case Windows.Web.Http.HttpStatusCode.Ok:
                            var b = await hrm.Content.ReadAsBufferAsync();

                            string result = System.Text.UTF8Encoding.UTF8.GetString(b.ToArray());
                            if (!string.IsNullOrEmpty(result))
                            {
                                r = new SpeechToTextResponse(result);
                            }
                            break;

                        case Windows.Web.Http.HttpStatusCode.Forbidden:
                            string token = await RenewToken();

                            if (string.IsNullOrEmpty(token))
                            {
                                loop++;
                            }
                            break;

                        default:
                            int    code      = (int)hrm.StatusCode;
                            string HttpError = "Http Response Error: " + code.ToString() + " reason: " + hrm.ReasonPhrase.ToString();
                            System.Diagnostics.Debug.WriteLine(HttpError);
                            r = new SpeechToTextResponse(string.Empty, HttpError);
                            break;
                        }
                    }
                }
                catch (System.Threading.Tasks.TaskCanceledException)
                {
                    System.Diagnostics.Debug.WriteLine("http POST canceled");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("http POST exception: " + ex.Message);
                }
                finally
                {
                    System.Diagnostics.Debug.WriteLine("http POST done");
                }
            }
            return(r);
        }
Beispiel #12
0
        /// <summary>
        /// Takes a photo to a StorageFile and adds rotation metadata to it
        /// </summary>
        /// <returns></returns>
        private async Task TakePhotoAsync()
        {
            // While taking a photo, keep the video button enabled only if the camera supports simultaneously taking pictures and recording video
            VideoButton.IsEnabled = _mediaCapture.MediaCaptureSettings.ConcurrentRecordAndPhotoSupported;

            // Make the button invisible if it's disabled, so it's obvious it cannot be interacted with
            VideoButton.Opacity = VideoButton.IsEnabled ? 1 : 0;

            var stream = new InMemoryRandomAccessStream();

            Debug.WriteLine("Taking photo...");
            Speech.Speak("Thank you, authenticating you now.");
            await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);

            try
            {
                string datetime = DateTime.Now.ToString("yy-MM-dd") + "-" + DateTime.Now.ToString("hh-mm-ss") + ".jpg";
                var    file     = await _captureFolder.CreateFileAsync(datetime, CreationCollisionOption.GenerateUniqueName);

                Debug.WriteLine("Photo taken! Saving to " + file.Path);

                var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation());

                await ReencodeAndSavePhotoAsync(stream, file, photoOrientation);

                Debug.WriteLine("Photo saved!");

                IStorageFile storageFile = await StorageFile.GetFileFromPathAsync(file.Path);

                IBuffer buffer = await FileIO.ReadBufferAsync(storageFile);

                byte[] bytes    = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(buffer);
                Stream streamer = new MemoryStream(bytes);
                Windows.Web.Http.HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(streamer.AsInputStream());

                var myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                myFilter.AllowUI = false;
                var client = new Windows.Web.Http.HttpClient(myFilter);
                Windows.Web.Http.HttpResponseMessage result = await client.PostAsync(new Uri(GlobalData.protocol + GlobalData.ip + ":" + GlobalData.port + GlobalData.endpoint), streamContent);

                string stringReadResult = await result.Content.ReadAsStringAsync();

                Debug.WriteLine(stringReadResult);

                JToken token      = JObject.Parse(stringReadResult);
                int    identified = (int)token.SelectToken("Results");
                string Response   = (string)token.SelectToken("ResponseMessage");

                if (identified != 0)
                {
                    Debug.WriteLine("Identified " + identified);
                    this.Frame.Navigate(typeof(AppHome));
                }
                else
                {
                    Speech.Speak("Sorry we cannot authorise your request");
                }
            }
            catch (Exception ex)
            {
                // File I/O errors are reported as exceptions
                Debug.WriteLine("Exception when taking a photo: " + ex.ToString());
            }

            // Done taking a photo, so re-enable the button
            VideoButton.IsEnabled = true;
            VideoButton.Opacity   = 1;
        }
Beispiel #13
0
        public virtual async System.Threading.Tasks.Task <IServiceResponse> PostAsyncWindowsWeb(Uri uri, byte[] audioBytes, apiArgs)
        {
            IServiceResponse response = new IServiceResponse(service);

            try
            {
                // Using HttpClient to grab chunked encoding (partial) responses.
                using (Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient())
                {
                    Log.WriteLine("before requestContent");
                    Log.WriteLine("after requestContent");
                    // rate must be specified but doesn't seem to need to be accurate.

                    Windows.Web.Http.IHttpContent requestContent = null;
                    if (Options.options.APIs.preferChunkedEncodedRequests)
                    {
                        // using chunked transfer requests
                        Log.WriteLine("Using chunked encoding");
                        Windows.Storage.Streams.InMemoryRandomAccessStream contentStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
                        // TODO: obsolete to use DataWriter? use await Windows.Storage.FileIO.Write..(file);
                        Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(contentStream);
                        dw.WriteBytes(audioBytes);
                        await dw.StoreAsync();

                        // GetInputStreamAt(0) forces chunked transfer (sort of undocumented behavior).
                        requestContent = new Windows.Web.Http.HttpStreamContent(contentStream.GetInputStreamAt(0));
                    }
                    else
                    {
                        requestContent = new Windows.Web.Http.HttpBufferContent(audioBytes.AsBuffer());
                    }
                    requestContent.Headers.Add("Content-Type", "audio/l16; rate=" + sampleRate.ToString()); // must add header AFTER contents are initialized

                    Log.WriteLine("Before Post: Elapsed milliseconds:" + stopWatch.ElapsedMilliseconds);
                    response.RequestElapsedMilliseconds = stopWatch.ElapsedMilliseconds;
                    using (Windows.Web.Http.HttpResponseMessage hrm = await httpClient.PostAsync(uri, requestContent))
                    {
                        response.RequestElapsedMilliseconds = stopWatch.ElapsedMilliseconds - response.RequestElapsedMilliseconds;
                        response.StatusCode = (int)hrm.StatusCode;
                        Log.WriteLine("After Post: StatusCode:" + response.StatusCode + " Total milliseconds:" + stopWatch.ElapsedMilliseconds + " Request milliseconds:" + response.RequestElapsedMilliseconds);
                        if (hrm.StatusCode == Windows.Web.Http.HttpStatusCode.Ok)
                        {
                            string responseContents = await hrm.Content.ReadAsStringAsync();

                            string[] responseJsons = responseContents.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            foreach (string rj in responseJsons)
                            {
                                response.ResponseJson = rj;
                                Newtonsoft.Json.Linq.JToken ResponseBodyToken = Newtonsoft.Json.Linq.JObject.Parse(response.ResponseJson);
                                response.ResponseJsonFormatted = Newtonsoft.Json.JsonConvert.SerializeObject(ResponseBodyToken, new Newtonsoft.Json.JsonSerializerSettings()
                                {
                                    Formatting = Newtonsoft.Json.Formatting.Indented
                                });
                                if (Options.options.debugLevel >= 4)
                                {
                                    Log.WriteLine(response.ResponseJsonFormatted);
                                }
                                Newtonsoft.Json.Linq.JToken tokResult = ProcessResponse(ResponseBodyToken);
                                if (tokResult == null || string.IsNullOrEmpty(tokResult.ToString()))
                                {
                                    response.ResponseResult = Options.options.Services.APIs.SpeechToText.missingResponse;
                                    if (Options.options.debugLevel >= 3)
                                    {
                                        Log.WriteLine("ResponseResult:" + response.ResponseResult);
                                    }
                                }
                                else
                                {
                                    response.ResponseResult = tokResult.ToString();
                                    if (Options.options.debugLevel >= 3)
                                    {
                                        Log.WriteLine("ResponseResult:" + tokResult.Path + ": " + response.ResponseResult);
                                    }
                                }
                            }
                        }
                        else
                        {
                            response.ResponseResult = hrm.ReasonPhrase;
                            Log.WriteLine("PostAsync Failed: StatusCode:" + hrm.ReasonPhrase + "(" + response.StatusCode.ToString() + ")");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLine("Exception:" + ex.Message);
                if (ex.InnerException != null)
                {
                    Log.WriteLine("InnerException:" + ex.InnerException);
                }
            }
            return(response);
        }