Example #1
0
        public async Task <ActionResult> GetGridData(UpdateRequestData UserModel)
        {
            var azureconfiguration = ConfigInit.getConfiguration();

            var            cosmosDBResponse       = new CosmosAPIService();
            DocumentClient documentClient         = new DocumentClient(new Uri(azureconfiguration.AzureCosmosDBConnectionString), azureconfiguration.AzureCosmonDBPrimaryKey1);
            var            createDataBaseResponse = await documentClient.CreateDatabaseIfNotExistsAsync(new Microsoft.Azure.Documents.Database {
                Id = "CognitiveAPIDemoDataSource"
            });

            var createDataBaseCollectionResponse = await documentClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("CognitiveAPIDemoDataSource"), new DocumentCollection { Id = "PocCollection" }, new RequestOptions { OfferThroughput = 1000 });

            var documentResponse = await cosmosDBResponse.UpdateDocumentCollection(UserModel, documentClient);

            if (!documentResponse)
            {
                return(Json(new { cosmosID = UserModel.cosmosID, imageText = UserModel.imageText, status = "Fail" }));
            }

            return(Json(new { cosmosID = UserModel.cosmosID, imageText = UserModel.imageText, status = "Success" }));
        }
Example #2
0
        public async Task <ActionResult> DoFileUploads(HttpPostedFileBase[] files)
        {
            // Step 1: Intialize the Azure Configuration form azure settings config
            var azureconfiguration = ConfigInit.getConfiguration();

            // Step 2: Intialize the Azure DataLake Storage
            var                storageAccountConnectionString_File = CloudStorageAccount.Parse(azureconfiguration.AzureStorageConnectionString);
            CloudFileClient    cloudFileClient = storageAccountConnectionString_File.CreateCloudFileClient();
            CloudFileShare     cloudFileShare  = cloudFileClient.GetShareReference(azureconfiguration.AzureFileStorageReference);
            CloudFileDirectory rootDir         = cloudFileShare.GetRootDirectoryReference();
            CloudFile          fileSas         = null;

            // Step 3: Intailize the API Client
            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", azureconfiguration.AzureVisionAPISubscriptionKey);
            HttpResponseMessage response;

            // Step 4: Intailize the Cosmos DB
            var            cosmosDBResponse       = new CosmosAPIService();
            DocumentClient documentClient         = new DocumentClient(new Uri(azureconfiguration.AzureCosmosDBConnectionString), azureconfiguration.AzureCosmonDBPrimaryKey1);
            var            createDataBaseResponse = await documentClient.CreateDatabaseIfNotExistsAsync(new Microsoft.Azure.Documents.Database {
                Id = "CognitiveAPIDemoDataSource"
            });

            var createDataBaseCollectionResponse = await documentClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("CognitiveAPIDemoDataSource"), new DocumentCollection { Id = "PocCollection" }, new RequestOptions { OfferThroughput = 1000 });

            List <APIData> objrApiData = new List <APIData>();

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].FileName.EndsWith(".jpg") || files[i].FileName.EndsWith(".png"))
                {
                    bytesFromImage = getbyteFromInputStream(files[i].InputStream, files[i].ContentLength);

                    if (cloudFileShare.Exists())
                    {
                        fileSas = rootDir.GetFileReference(Path.GetFileName(files[i].FileName));
                        fileSas.UploadFromStream(new MemoryStream(bytesFromImage));
                    }


                    // Extracting Image :
                    using (ByteArrayContent content = new ByteArrayContent(bytesFromImage))
                    {
                        content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                        response = await client.PostAsync(azureconfiguration.AzureVisionAPIEndPoint + "?" + "mode=Handwritten", content);
                    }

                    if (response.IsSuccessStatusCode)
                    {
                        string operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault();
                        string contentString;
                        int    res = 0;
                        do
                        {
                            System.Threading.Thread.Sleep(1000);
                            response = await client.GetAsync(operationLocation);

                            contentString = await response.Content.ReadAsStringAsync();

                            ++res;
                        }while (res < 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1);

                        if (res == 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1)
                        {
                            //return null;
                        }

                        var           rootobject = JsonConvert.DeserializeObject <RootObject>(contentString);
                        StringBuilder builder    = new StringBuilder();
                        for (int s = 0; s < rootobject.recognitionResult.lines.Count; s++)
                        {
                            builder.Append(rootobject.recognitionResult.lines[s].text.ToString());
                        }
                        objrApiData.Add(new APIData()
                        {
                            OCRID     = Guid.NewGuid().ToString(),
                            imageData = bytesFromImage,
                            imageText = builder.ToString(),
                            ImageUrl  = Path.GetFileName(files[i].FileName),//fileSas.Uri.AbsoluteUri.ToString(),
                            pid       = i,
                            sid       = "text" + i,
                            Error     = "no Error",
                            Remarks   = "No Remarks Found"
                        });
                    }
                    else
                    {
                        string errorString = await response.Content.ReadAsStringAsync();

                        var errorObject = JsonConvert.DeserializeObject <VisionAPIErrorMessage>(errorString);

                        objrApiData.Add(new APIData()
                        {
                            OCRID     = Guid.NewGuid().ToString(),
                            imageData = bytesFromImage,
                            imageText = errorObject.error.message.ToString(),
                            ImageUrl  = Path.GetFileName(files[i].FileName),//fileSas.Uri.AbsoluteUri.ToString(),
                            pid       = i,
                            sid       = "text" + i,
                            Error     = errorObject.error.message.ToString(),
                            Remarks   = "Image scanning Failed"
                        });
                    }
                }
            }
            var dbProcessingResponse = await cosmosDBResponse.CreateDocumentCollection(objrApiData, documentClient);

            TempData["objrApiData"] = objrApiData;
            return(RedirectToAction("ShowResults"));
        }