public ClassificationResult GetResult(string inputImageLocation, string pictureName) { ImageAnnotatorClient client = ImageAnnotatorClient.Create(); Image image = Image.FromFile(inputImageLocation); IReadOnlyList <EntityAnnotation> results; var startTime = DateTime.Now; try { results = client.DetectLabels(image, null, Constants.maxLabelsReturned); } catch (Exception e) { throw new Exception("Error during detect label call Google.", e); } var endTime = DateTime.Now; var labels = new List <string>(); var scores = new List <float>(); foreach (var label in results) { if (!string.IsNullOrEmpty(label.Description) && label.Score != 0) { labels.Add(label.Description); scores.Add(label.Score * 100); } else { throw new Exception("Exception during Google processing of image " + inputImageLocation); } } var classificationResult = new ClassificationResult() { APIName = "Google" }; classificationResult.ProcessingTimeMilliseconds = endTime.Subtract(startTime).TotalMilliseconds; classificationResult.InputLabel = pictureName; classificationResult.ReturnedLabel1 = labels[0]; classificationResult.ReturnedConfidence1 = scores[0]; classificationResult.ReturnedLabel2 = labels[1]; classificationResult.ReturnedConfidence2 = scores[1]; classificationResult.ReturnedLabel3 = labels[2]; classificationResult.ReturnedConfidence3 = scores[2]; classificationResult.FilePath = inputImageLocation; return(classificationResult); }
public ClassificationResult GetResult(string inputImageLocation, string pictureName) { Amazon.Rekognition.Model.Image image = new Amazon.Rekognition.Model.Image(); // Load image try { using (FileStream fs = new FileStream(inputImageLocation, FileMode.Open, FileAccess.Read)) { byte[] data = null; data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); image.Bytes = new MemoryStream(data); } } catch (Exception e) { throw new Exception("Error during loading image file AWS.", e); } // Create client AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); // Create detectLabelsRequest DetectLabelsRequest detectlabelsRequest = new DetectLabelsRequest() { Image = image, MaxLabels = Constants.maxLabelsReturned, }; ClassificationResult classificationResult = new ClassificationResult() { APIName = "AWS" }; try { var startTime = DateTime.Now; var detectLabelsResponse = rekognitionClient.DetectLabelsAsync(detectlabelsRequest); detectLabelsResponse.Wait(); var endTime = DateTime.Now; var labels = new List <string>(); var scores = new List <float>(); foreach (var label in detectLabelsResponse.Result.Labels) { if (!string.IsNullOrEmpty(label.Name) && label.Confidence != 0) { labels.Add(label.Name); scores.Add(label.Confidence); } else { throw new Exception("Exception during AWS processing of image " + inputImageLocation); } } classificationResult.ProcessingTimeMilliseconds = endTime.Subtract(startTime).TotalMilliseconds; classificationResult.InputLabel = pictureName; classificationResult.ReturnedLabel1 = labels[0]; classificationResult.ReturnedConfidence1 = scores[0]; classificationResult.ReturnedLabel2 = labels[1]; classificationResult.ReturnedConfidence2 = scores[1]; classificationResult.ReturnedLabel3 = labels[2]; classificationResult.ReturnedConfidence3 = scores[2]; classificationResult.FilePath = inputImageLocation; } catch (Exception e) { throw e; } return(classificationResult); }
public ClassificationResult GetResult(string inputImageLocation, string pictureName) { #region SETUP string apiKey = "***"; string apiSecret = "***"; string basicAuthValue = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(String.Format("{0}:{1}", apiKey, apiSecret))); #endregion SETUP #region UPLOAD var uploadClient = new RestClient("https://api.imagga.com/v2/uploads"); uploadClient.Timeout = -1; var uploadRequest = new RestRequest(Method.POST); uploadRequest.AddHeader("Authorization", String.Format("Basic {0}", basicAuthValue)); uploadRequest.AddFile("image", inputImageLocation); var startTime = DateTime.Now; IRestResponse uploadResponse = uploadClient.Execute(uploadRequest); ImaggaUploadResponse immagaUploadResponse = JsonConvert.DeserializeObject <ImaggaUploadResponse>(uploadResponse.Content); var uploadid = string.Empty; if (immagaUploadResponse.status.type == "success") { uploadid = immagaUploadResponse.result.upload_id; } else { throw new Exception("error during immaggaUpload"); } #endregion UPLOAD #region LABEL var labelClient = new RestClient("https://api.imagga.com/v2/tags"); labelClient.Timeout = -1; var labelRequest = new RestRequest(Method.GET); labelRequest.AddParameter("image_upload_id", uploadid); labelRequest.AddParameter("limit", Constants.maxLabelsReturned); labelRequest.AddHeader("Authorization", String.Format("Basic {0}", basicAuthValue)); IRestResponse labelResponse = labelClient.Execute(labelRequest); var endTime = DateTime.Now; ImaggaLabelResponse imaggaLabelResponse = JsonConvert.DeserializeObject <ImaggaLabelResponse>(labelResponse.Content); var labels = new List <string>(); var scores = new List <float>(); if (imaggaLabelResponse.status.type == "success") { foreach (var label in imaggaLabelResponse.result.tags) { if (!string.IsNullOrEmpty(label.tag.en) && label.confidence != 0) { labels.Add(label.tag.en); scores.Add(label.confidence); } else { throw new Exception("Exception during imagga processing of image " + inputImageLocation); } } } else { throw new Exception("Exception during imagga processing of image, no success returned: " + inputImageLocation); } #endregion LABEL var classificationResult = new ClassificationResult() { APIName = "imagga" }; classificationResult.ProcessingTimeMilliseconds = endTime.Subtract(startTime).TotalMilliseconds; classificationResult.InputLabel = pictureName; classificationResult.ReturnedLabel1 = labels[0]; classificationResult.ReturnedConfidence1 = scores[0]; classificationResult.ReturnedLabel2 = labels[1]; classificationResult.ReturnedConfidence2 = scores[1]; classificationResult.ReturnedLabel3 = labels[2]; classificationResult.ReturnedConfidence3 = scores[2]; classificationResult.FilePath = inputImageLocation; return(classificationResult); }
static void Main(string[] args) { Console.WriteLine("Get images from folder " + Constants.startPath); /* * Get all items from each subdirectory and send to computer vision API's */ Console.WriteLine("Start image classification."); List <ClassificationResult> classResultsAll = new List <ClassificationResult>(); List <string> filePathsList = Directory.GetFiles(Constants.startPath).ToList(); Console.WriteLine(filePathsList.Count + " images found."); foreach (string file in filePathsList) { var filename = Path.GetFileName(file); long sizeBytes = new FileInfo(file).Length; if (true) { try { IClassification googleLabeler = new GoogleClassification(); ClassificationResult classResultGoogle = googleLabeler.GetResult(file, filename); classResultGoogle.FileSizeBytes = sizeBytes; classResultsAll.Add(classResultGoogle); } catch (Exception e) { Console.WriteLine("Error during Google classification of image " + file); throw e; } } if (true) { try { IClassification awsLabeler = new AWSClassification(); ClassificationResult classResultAWS = awsLabeler.GetResult(file, filename); classResultAWS.FileSizeBytes = sizeBytes; classResultsAll.Add(classResultAWS); } catch (Exception e) { Console.WriteLine("Error during AWS classification of image " + file); throw e; } } try { IClassification imaggalabeler = new ImaggaClassification(); ClassificationResult classResultimaga = imaggalabeler.GetResult(file, filename); classResultimaga.FileSizeBytes = sizeBytes; classResultsAll.Add(classResultimaga); } catch (Exception e) { Console.WriteLine("Error during imagga classification of image " + file); throw e; } } Console.WriteLine("Successfully classified images."); Console.WriteLine("Start write to CSV."); try { // Write to CSV using (var writer = new StreamWriter(Constants.outputFolder + @"\result" + Guid.NewGuid().ToString() + ".csv")) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.WriteRecords(classResultsAll); } } catch (Exception e) { Console.WriteLine("Error during write to CSV."); throw e; } Console.WriteLine("End write to CSV."); Console.WriteLine("Program executed successfully."); Console.ReadLine(); }