private static ImagePredictionResultModel ProcessImagePredictions(string imageUrl, Guid projectId) { string predictionKey = "6c9f96aa9a574ab8b3aa18f5e0ec4c1c"; var shouldAlert = false; // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); // Make a prediction against the new project var result = endpoint.PredictImageUrl(projectId, new Microsoft.Cognitive.CustomVision.Models.ImageUrl(imageUrl)); // Need to throttle the requests as to not upset it into throwing the dreaded 429 too many requests in a given period of time Thread.Sleep(200); //check for any probabilities greater than 90% shouldAlert = result.Predictions.Any(x => x.Probability > .9); if (shouldAlert) { return(result); } else { return(null); } }
public async Task <ImageInsights> PredictImage(string imageLocalPath) { var fileName = Path.GetFileName(imageLocalPath); var customVisionProjectId = ConfigurationManager.AppSettings["CustomVisionProjectId"]; var customVisionPredictionKey = ConfigurationManager.AppSettings["CustomVisionPredictionKey"]; PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(customVisionPredictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); try { // Make a prediction against the project var testImage = new MemoryStream(System.IO.File.ReadAllBytes(imageLocalPath)); var result = await endpoint.PredictImageAsync(Guid.Parse(customVisionProjectId), testImage); ImageInsights insights = new ImageInsights { ImageId = fileName, Tags = result.Predictions.Where(s => s.Probability >= 0.60).Select(s => s.Tag).ToArray() }; return(insights); } catch (Exception ex) { return(null); } }
public async Task <IList <PredictionViewModel> > Predict(byte[] image) { var trainingCredentials = new TrainingApiCredentials(APIKeys.TrainingAPIKey); var trainingApi = new TrainingApi(trainingCredentials); var predictionEndpointCredentials = new PredictionEndpointCredentials(APIKeys.PredictionAPIKey); var predictionEndpoint = new PredictionEndpoint(predictionEndpointCredentials); var projects = await trainingApi.GetProjectsAsync(); var stream = new System.IO.MemoryStream(image); var results = await predictionEndpoint.PredictImageAsync(projects[0].Id, stream); var predictions = new List <PredictionViewModel>(); foreach (var result in results.Predictions) { predictions.Add(new PredictionViewModel() { Tag = result.Tag, Prediction = result.Probability }); } return(predictions); }
static void Main(string[] args) { var predictionKey = "insert your prediction key here"; PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var testImage = new MemoryStream(File.ReadAllBytes(@"insert the picture you want to compare here")); var projectId = new Guid("insert the project guid here"); var result = endpoint.PredictImage(projectId, testImage, null, null); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); } Console.ReadKey(); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); try { string requestBody = new StreamReader(req.Body).ReadToEnd(); var prediction = JsonConvert.DeserializeObject <Prediction>(requestBody); var api = new TrainingApi(new TrainingApiCredentials(prediction.TrainingId)); var account = api.GetAccountInfo(); var predictionKey = account.Keys.PredictionKeys.PrimaryKey; var creds = new PredictionEndpointCredentials(predictionKey); var endpoint = new PredictionEndpoint(creds); //This is where we run our prediction against the default iteration var result = endpoint.PredictImageUrl(new Guid(prediction.ProjectId), new ImageUrl(prediction.ImageUrl)); prediction.Results = new Dictionary <string, decimal>(); // Loop over each prediction and write out the results foreach (var outcome in result.Predictions) { if (outcome.Probability > .70) { prediction.Results.Add(outcome.Tag, (decimal)outcome.Probability); } } return((ActionResult) new OkObjectResult(prediction)); } catch (Exception e) { return(new BadRequestObjectResult(e.GetBaseException().Message)); } }
// GET: RecognitionModels public async Task <ActionResult> Index([Bind(Include = "Id,Key,ImageUpload")] RecognitionModel recognitionModel) { ViewBag.Message = "Result will show here."; Trace.WriteLine("Starting Prediction..."); if (recognitionModel.ImageUpload != null) { // Create image. Guid projectid = new Guid("57471653-6e79-455f-b874-ee00d1014c37"); // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(recognitionModel.Key); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); if (recognitionModel.ImageUpload != null && recognitionModel.ImageUpload.ContentLength > 0) { //var uploadDir = "~/uploads"; //var imagePath = Path.Combine(Server.MapPath(uploadDir), recognitionModel.ImageUpload.FileName); //var imageUrl = Path.Combine(uploadDir, recognitionModel.ImageUpload.FileName); //Trace.WriteLine("ImageUrl:" + imageUrl); //Trace.WriteLine("Image path:" + imagePath); //recognitionModel.ImageUpload.SaveAs(imagePath); Trace.WriteLine("createing memory stream"); //MemoryStream memStream = new MemoryStream(System.IO.File.ReadAllBytes(imagePath)); MemoryStream memStream = new MemoryStream(); recognitionModel.ImageUpload.InputStream.Position = 0; recognitionModel.ImageUpload.InputStream.CopyTo(memStream); memStream.Position = 0; // Make a prediction against the new project Trace.WriteLine("Making a prediction:"); try { var result = endpoint.PredictImage(projectid, memStream); // Loop over each prediction and write out the results var predicition = result.Predictions.FirstOrDefault(P => P.Probability >= 0.8); if (predicition == null) { ViewBag.Message = "Could not find a good match for the uploaded image. Please try again."; } else { ViewBag.Message = "Your image is of type " + predicition.Tag + " with the probability of " + predicition.Probability; } } catch (Exception e) { ViewBag.Message = "Could not make a predicition of image " + recognitionModel.ImageUpload.FileName + "! Error message: " + e.Message; } } } return(View(recognitionModel)); }
public CustomVisionCommunicator() { this.visionApiKey = Settings.Instance.VisionApiKey; this.predictionKey = Settings.Instance.PredictionKey; this.projectId = new Guid(Settings.Instance.VisionApiProjectId); //changes everytime when model is retrained this.iterationId = new Guid(Settings.Instance.VisionApiIterationId); PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); //Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key endpoint = new PredictionEndpoint(predictionEndpointCredentials); vsc = new VisionServiceClient(visionApiKey, "https://westeurope.api.cognitive.microsoft.com/vision/v1.0"); }
public IList <Prediction> PredictImage(Stream imageStream) { var account = TrainingApi.GetAccountInfo(); var predictionKey = account.Keys.PredictionKeys.PrimaryKey; var predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); var endpoint = new PredictionEndpoint(predictionEndpointCredentials); var result = endpoint.PredictImage(ProjectGuid, imageStream); return(result.Predictions.Select(p => new Prediction { Tag = Enum.Parse <Models.Training.Tag>(p.Tag), Probability = p.Probability }).ToList()); }
public static bool IsValidCard(string url) { try { string trainingKey = Constants.CustomVisionTrainingAPIKey; // Create the Api, passing in a credentials object that contains the training key TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey); TrainingApi trainingApi = new TrainingApi(trainingCredentials); var projects = trainingApi.GetProjects(); ProjectModel project = projects.FirstOrDefault(e => e.Name == "IDVision"); // Get the prediction key, which is used in place of the training key when making predictions var account = trainingApi.GetAccountInfo(); var predictionKey = account.Keys.PredictionKeys.PrimaryKey; // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); byte[] byteData = Utilities.Utilities.GetImagesAsByteArrayFromUri(url); MemoryStream stream = new MemoryStream(byteData); // Make a prediction against the new project var predictionResult = endpoint.PredictImage(project.Id, stream); // Loop over each prediction and write out the results foreach (var c in predictionResult.Predictions) { if ((c.Probability * 100) > 80) { return(true); } else { return(false); } } } catch (Exception ex) { throw ex; } return(false); }
static void Main(string[] args) { MemoryStream testImage = null; Guid projectId; string predictionKey; try { Console.Write("Enter a local image URL: "); var imagePath = Console.ReadLine(); testImage = new MemoryStream(File.ReadAllBytes(imagePath)); Console.Write("Enter Project Id Guid: "); var projectIdGuid = Console.ReadLine(); projectId = Guid.Parse(projectIdGuid); Console.Write("Enter Prediction Key: "); predictionKey = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(); // Utilizing the Microsoft.Cognitive.CustomVision NuGet Package to establish Credentials and the Endpoint PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); ImagePredictionResultModel result = endpoint.PredictImage(projectId, testImage); Console.WriteLine("Prediction results for the given image are..."); foreach (ImageTagPrediction prediction in result.Predictions) { Console.WriteLine($"{prediction.Tag}: {prediction.Probability:P1}"); } Console.WriteLine(); exitInNSeconds(5); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine(); exitInNSeconds(5); } }
static void Main(string[] args) { Guid projectId = Guid.Parse("[Insert project ID]"); string predictionKey = "[Insert prediction Key]"; string imagePath = @"d:\sample.jpg"; MemoryStream testImage = new MemoryStream(File.ReadAllBytes(imagePath)); PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); ImagePredictionResultModel result = endpoint.PredictImage(projectId, testImage); // Loop over each prediction and write out the results foreach (ImageTagPrediction prediction in result.Predictions) { Console.WriteLine($"\t{prediction.Tag}: {prediction.Probability:P1}"); } Console.ReadKey(); }
public static void MakePrediction() { var predictionKey = "031439b0f9ec4549995e53e32971c605"; PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var testImage = HomeController.HaraFinalImage; var projectId = new Guid("62ccee84-08f9-44e7-89f4-6b5b76894a8a"); var result = endpoint.PredictImage(projectId, testImage, null, null); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); PredictionResults pr = new PredictionResults(); pr.tag = c.Tag; pr.prediction = c.Probability; } }
public static void RunCustomVisionService(Image image) { var predictionEndpointCredentials = new PredictionEndpointCredentials( Config.Default.CustomVisionPredictionKey ); var endpoint = new PredictionEndpoint(predictionEndpointCredentials); var result = endpoint.PredictImage( new Guid(Config.Default.CustomVisionProjectId), new MemoryStream(image.Bytes), new Guid(Config.Default.CustomVisionIterationId)); var waldo = result .Predictions .FirstOrDefault(p => p.Tag == "waldo"); if (waldo != null && waldo.Probability > 0.25d) { image.WaldoDetected = true; } image.ProcessedCustomVision = true; }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "api/MakePrediction")] HttpRequestMessage req, TraceWriter log) { try { var stream = await req.Content.ReadAsStreamAsync(); var prediction = new Prediction { ProjectId = "36f167d1-82e0-45f4-8ddc-ffc6d3fe3a41", //36f167d1-82e0-45f4-8ddc-... TrainingKey = "c63201c1e627428fb5c5d603430b5f62", //c63201c1e627428fb5c5d6... TimeStamp = DateTime.UtcNow, UserId = Guid.NewGuid().ToString(), ImageUrl = await UploadImageToBlobStorage(stream) }; var api = new TrainingApi(new TrainingApiCredentials(prediction.TrainingKey)); var account = api.GetAccountInfo(); var predictionKey = account.Keys.PredictionKeys.PrimaryKey; var creds = new PredictionEndpointCredentials(predictionKey); var endpoint = new PredictionEndpoint(creds); //This is where we run our prediction against the default iteration var result = endpoint.PredictImageUrl(new Guid(prediction.ProjectId), new ImageUrl(prediction.ImageUrl)); prediction.Results = new Dictionary <string, decimal>(); // Loop over each prediction and write out the results foreach (var outcome in result.Predictions) { if (outcome.Probability >= 0.0010) { prediction.Results.Add(outcome.Tag, (decimal)outcome.Probability); } } await CosmosDataService.Instance.InsertItemAsync(prediction); return(req.CreateResponse(HttpStatusCode.OK, prediction)); } catch (Exception e) { var baseException = e.GetBaseException(); var operationException = baseException as HttpOperationException; var reason = baseException.Message; if (operationException != null) { var jobj = JObject.Parse(operationException.Response.Content); var code = jobj.GetValue("Code"); if (code != null && !string.IsNullOrWhiteSpace(code.ToString())) { reason = code.ToString(); } } return(req.CreateErrorResponse(HttpStatusCode.BadRequest, reason)); } async Task <string> UploadImageToBlobStorage(Stream stream) { //Create a new blob block Id var blobId = Guid.NewGuid().ToString() + ".jpg"; if (_blobContainer == null) { //You can set your endpoint here as a string in code or just set it to pull from your App Settings var containerName = "images"; var endpoint = $"https://mynewstorageaccountblob.blob.core.windows.net/{containerName}/?sv=2017-04-17&ss=b&srt=sco&sp=rwdlac&se=2019-01-06T04:57:40Z&st=2018-01-05T20:57:40Z&spr=https&sig=YE2ZWYTvRax4jRUmBpZSzaCFDd8ZwM3pxSDHYWVn0dY%3D"; _blobContainer = new CloudBlobContainer(new Uri(endpoint)); } //Create a new block to store this uploaded image data var blockBlob = _blobContainer.GetBlockBlobReference(blobId); blockBlob.Properties.ContentType = "image/jpg"; //You can even store metadata with the content blockBlob.Metadata.Add("createdFor", "This Awesome Hackathon"); //Upload and return the new URL associated w/ this blob content await blockBlob.UploadFromStreamAsync(stream); return(blockBlob.StorageUri.PrimaryUri.ToString()); } }
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = nameof(AnalyseCustomImage))] HttpRequestMessage req, TraceWriter log) { using (var analytic = new AnalyticService(new RequestTelemetry { Name = nameof(AnalyseCustomImage) })) { try { var allTags = new List <string>(); var j = JObject.Parse(req.Content.ReadAsStringAsync().Result); var imageUrl = (string)j["imageUrl"]; var treasureId = (string)j["treasureId"]; var gameId = (string)j["gameId"]; var game = CosmosDataService.Instance.GetItemAsync <Game>(gameId).Result; if (game == null) { return(req.CreateErrorResponse(HttpStatusCode.NotFound, "Game not found")); } var treasure = game.Treasures.SingleOrDefault(t => t.Id == treasureId); if (treasure == null) { return(req.CreateErrorResponse(HttpStatusCode.NotFound, "Treasure not found")); } var api = new TrainingApi(new TrainingApiCredentials(ConfigManager.Instance.CustomVisionTrainingKey)); var account = api.GetAccountInfo(); var predictionKey = account.Keys.PredictionKeys.PrimaryKey; var creds = new PredictionEndpointCredentials(predictionKey); var endpoint = new PredictionEndpoint(creds); //This is where we run our prediction against the default iteration var result = endpoint.PredictImageUrl(new Guid(game.CustomVisionProjectId), new ImageUrl(imageUrl)); bool toReturn = false; // Loop over each prediction and write out the results foreach (var outcome in result.Predictions) { if (treasure.Attributes.Any(a => a.Name.Equals(outcome.Tag, StringComparison.InvariantCultureIgnoreCase))) { if (outcome.Probability >= ConfigManager.Instance.CustomVisionMinimumPredictionProbability) { toReturn = true; break; } } } return(req.CreateResponse(HttpStatusCode.OK, toReturn)); } catch (Exception e) { analytic.TrackException(e); return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } } }
public HotDogOrNotPage() { InitializeComponent(); predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); endpoint = new PredictionEndpoint(predictionEndpointCredentials); }
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; // Variables declaration | 変数定義 bool food = false; // "food" tag | "food" タグの有無 string tag = ""; // food category tag | 食べ物カテゴリータグ string msg = ""; // response message from bot | 返答メッセージ // Prep for Custom Vision API | Custom Vision API を使う準備 var cvCred = new PredictionEndpointCredentials("YOUR_PREDICTION_KEY"); var cvEp = new PredictionEndpoint(cvCred); var cvGuid = new Guid("YOUR_PROJECT_ID"); if (activity.Attachments?.Count != 0) { // Get attachment (photo) and get as Stream | 送られてきた画像を Stream として取得 var photoUrl = activity.Attachments[0].ContentUrl; var client = new HttpClient(); var photoStream = await client.GetStreamAsync(photoUrl); try { // Predict Image using Custom Vision API | 画像を判定 var cvResult = await cvEp.PredictImageAsync(cvGuid, photoStream); // Get food and category tag | food タグ および カテゴリーを取得 foreach (var item in cvResult.Predictions) { if (item.Probability > 0.8) { if (item.Tag == "food") { food = true; } else { tag = item.Tag; break; } } } } catch { // Error Handling } } if (tag != "") { // Set message | タグに応じてメッセージをセット //msg = "This is " + tag + "! Looks good ;)"; //msg = "この写真は " + tag + " だね♪"; switch (tag) { case "curry": msg = "カレーおいしそう!甘いチャイでホッとしよう☕"; //msg = "Have sweet chai after spicy curry!"; break; case "gyoza": msg = "やっぱ餃子にはビールだね🍺"; //msg = "Beer should be best much to Gyoza!"; break; case "pizza": msg = "ピザには刺激的な炭酸飲料★はどうかな?"; //msg = "What about sparkling soda with pizza?"; break; case "meat": msg = "肉、にく、ニク♪ 赤ワインを合わせてどうぞ🍷"; //msg = "Red wine makes you eat more meat!"; break; case "ramen": msg = "やめられないよねー。ラーメンには緑茶でスッキリ☆"; //msg = "Have green tea after Ramen!"; break; case "sushi": msg = "今日はちょっとリッチにお寿司?合わせるなら日本酒かな🍶"; //msg = "Sushi! Have you ever tried Japanese Sake?"; break; } } else if (food == true) { //msg = "I'm not sure what it is ..."; msg = "この食べ物は分からないです...日本の夏は麦茶だね!"; } else { //msg = "Send me food photo you are eating!"; msg = "食べ物の写真を送ってね♪"; } await context.PostAsync(msg); context.Wait(MessageReceivedAsync); }