Пример #1
0
        public async Task <IActionResult> Post(List <IFormFile> img)
        {
            var requestStream = Request.HttpContext.Items;

            const string scue        = "https://southcentralus.api.cognitive.microsoft.com";
            string       trainingKey = "4f473807b7434dd5a1bdb45cb9104b38";

            CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient()
            {
                ApiKey   = trainingKey,
                Endpoint = scue
            };

            // Find the object detection domain
            var domains = trainingApi.GetDomainsAsync();
            //var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection");
            var project = trainingApi.GetProject(Guid.Parse("b911d77a-ef25-47fd-86ed-87db4500ef7b"));


            const string southcentralus = "https://southcentralus.api.cognitive.microsoft.com";

            string predictionKey = "a58f3ca5856c491db0b73b87cb1118cf";

            CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
            {
                ApiKey   = predictionKey,
                Endpoint = southcentralus
            };

            var c      = new List <PredictionModel>();
            var result = endpoint.PredictImage(Guid.Parse("cbfa66a3-9815-47d6-a389-7438e468ac15"), img[0].OpenReadStream());

            ImageUrl imgUrl = new ImageUrl();

            imgUrl.Url = "https://http2.mlstatic.com/guitarra-tagima-pr-200-special-pr200-sunburst-D_NQ_NP_894387-MLB26271081482_112017-F.jpg";



            var resultImageUrl = endpoint.PredictImageUrl(Guid.Parse("cbfa66a3-9815-47d6-a389-7438e468ac15"), imgUrl);

            foreach (var item in result.Predictions.OrderBy(x => x.Probability))
            {
                var pm = new PredictionModel(Math.Round(item.Probability * 100), item.TagId, item.TagName, item.BoundingBox);

                if (pm.Probability > 70)
                {
                    pm.BoundingBox.Top    = Convert.ToInt32(pm.BoundingBox.Top * 380);
                    pm.BoundingBox.Height = Convert.ToInt32(pm.BoundingBox.Height * 380);
                    pm.BoundingBox.Left   = Convert.ToInt32(pm.BoundingBox.Left * 700);
                    pm.BoundingBox.Width  = Convert.ToInt32(pm.BoundingBox.Width * 700);
                    c.Add(pm);
                }
            }

            return(Ok(c));
        }
Пример #2
0
        public IEnumerable <TagPrediction> GetImageTagPredictions(string imageUrl)
        {
            var client = new CustomVisionPredictionClient()
            {
                ApiKey   = customVisionSubscriptionKey,
                Endpoint = "https://southcentralus.api.cognitive.microsoft.com"
            };

            var result = client.PredictImageUrl(customVisionProjectId, new ImageUrl(imageUrl));

            return(result.Predictions.Select(p => new TagPrediction(p.TagName, (int)(p.Probability * 100))).ToList());
        }
Пример #3
0
        public static void predictingImages(String imgUrl)
        {
            CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
            {
                ApiKey   = predictionKey,
                Endpoint = SouthCentralUsEndpoint
            };

            // Make a prediction against the new project
            //Console.WriteLine("Predicting:");
            //Test Image to be predicted after training of classifier
            projectID = new Guid("83867a3b-64ae-4c1d-994c-1fc75b7f0e5a");
            Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl testImage = new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl(imgUrl);
            var result = endpoint.PredictImageUrl(projectID, testImage);

            // Loop over each prediction and write out the results
            foreach (var c in result.Predictions)
            {
                Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
            }
        }
Пример #4
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            //try
            //{
            //    await connection.OpenAsync();
            //}
            //catch (Exception)
            //{

            //    throw;
            //}

            Prediction pd = new Prediction();

            string url = "https://informebairro.com.br/wp-content/uploads/2018/01/aula-de-instrumentos-musicais-violao-guitarra.jpg";

            for (int i = 0; i < 2; i++)
            {
                await SendQueue.SendMessagesAsync(url);
                await sendQueue1();
                await sendQueue2();
            }

            log.Info("C# HTTP trigger function processed a request.");

            // parse query parameter
            string name = req.GetQueryNameValuePairs()
                          .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                          .Value;

            if (name == null)
            {
                // Get request body
                dynamic data = await req.Content.ReadAsAsync <object>();

                name = data?.name;
            }

            const string southcentralus = "https://southcentralus.api.cognitive.microsoft.com";

            string predictionKey = "a58f3ca5856c491db0b73b87cb1118cf";

            CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
            {
                ApiKey   = predictionKey,
                Endpoint = southcentralus
            };

            ImageUrl imgUrl = new ImageUrl();

            imgUrl.Url = "https://http2.mlstatic.com/guitarra-tagima-pr-200-special-pr200-sunburst-D_NQ_NP_894387-MLB26271081482_112017-F.jpg";

            var c = new List <PredictionModel>();

            var resultImageUrl = endpoint.PredictImageUrl(Guid.Parse("cbfa66a3-9815-47d6-a389-7438e468ac15"), imgUrl);


            foreach (var item in resultImageUrl.Predictions.OrderBy(x => x.Probability))
            {
                var pm = new PredictionModel(Math.Round(item.Probability * 100), item.TagId, item.TagName, item.BoundingBox);

                if (pm.Probability > 59)
                {
                    pm.BoundingBox.Top    = Convert.ToInt32(pm.BoundingBox.Top * 380);
                    pm.BoundingBox.Height = Convert.ToInt32(pm.BoundingBox.Height * 380);
                    pm.BoundingBox.Left   = Convert.ToInt32(pm.BoundingBox.Left * 700);
                    pm.BoundingBox.Width  = Convert.ToInt32(pm.BoundingBox.Width * 700);
                    c.Add(pm);
                }
            }

            foreach (var item in c)
            {
                pd.Id  = Guid.NewGuid();
                pd.Url = url;
                pd.CanAdvertisement = true;
                pd.Probability      = item.Probability;

                string sql = "INSERT INTO Predictions(Id,Url,CanAdvertisement,Probability) VALUES(@param1,@param2,@param3,@param3)";
                using (SqlCommand cmd = new SqlCommand(sql, connection))
                {
                    cmd.Parameters.Add("@param1", SqlDbType.UniqueIdentifier).Value = pd.Id;
                    cmd.Parameters.Add("@param2", SqlDbType.VarChar, 100).Value     = pd.Url;
                    cmd.Parameters.Add("@param3", SqlDbType.Bit).Value   = pd.CanAdvertisement;
                    cmd.Parameters.Add("@param4", SqlDbType.Float).Value = pd.Probability;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
            }


            var jsonToReturn = JsonConvert.SerializeObject(resultImageUrl);

            return(req.CreateResponse(HttpStatusCode.OK, jsonToReturn, "application/json"));
        }