private static async Task <EvaluateImageResult> EvaluateImage(string key, string type, string urlImage, Stream rawImage, bool cacheImage) { // init EvaluateImageResult result = null; var moderator = new ModeratorClient(key); // go switch (type) { case "url": { result = await moderator.EvaluateImageAsync(urlImage, DataRepresentationType.Url, cacheImage); break; } case "raw": { result = await moderator.EvaluateImageAsync(rawImage, cacheImage); break; } } return(result); }
public void EvaluateImageStream() { EvaluateImageResult result = null; using (Stream stream = new FileStream(this.testImageContent, FileMode.Open, FileAccess.Read)) { var moderate = this.client.EvaluateImageAsync(stream, this.isCache); result = moderate.Result; } Assert.IsNotNull(result); Assert.IsTrue(result.AdultClassificationScore > 0, "Expected higher than 0 Adult Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(result)); Assert.IsTrue(result.RacyClassificationScore > 0, "Expected higher than 0 Racy Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(result)); Assert.IsTrue(!result.IsImageAdultClassified, "Image should not have been classified as Adult, Response: {0}", JsonConvert.SerializeObject(result)); Assert.IsTrue(!result.IsImageRacyClassified, "Image should not have been classified as Racy, Response: {0}", JsonConvert.SerializeObject(result)); }
public void EvaluateImageCache() { EvaluateImageResult result = null; result = this.EvaluateImage(); Assert.IsNotNull(result.CacheID); if (!string.IsNullOrWhiteSpace(result.CacheID)) { var moderate = this.client.EvaluateImageAsync(result.CacheID, DataRepresentationType.Cacheid, this.isCache); result = moderate.Result; } Assert.IsNotNull(result); Assert.IsTrue(result.AdultClassificationScore > 0, "Expected higher than 0 Adult Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(result)); Assert.IsTrue(result.RacyClassificationScore > 0, "Expected higher than 0 Racy Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(result)); Assert.IsTrue(!result.IsImageAdultClassified, "Image should not have been classified as Adult, Response: {0}", JsonConvert.SerializeObject(result)); Assert.IsTrue(!result.IsImageRacyClassified, "Image should not have been classified as Racy, Response: {0}", JsonConvert.SerializeObject(result)); }
public async Task EvaluateImageTestDataBasedAsync() { string type = this.TestContext.DataRow["DataType"].ToString(); bool isEnabled = bool.Parse(this.TestContext.DataRow["Enabled"].ToString()); var expectSizeException = bool.Parse(this.TestContext.DataRow["SizeException"].ToString()); if (!isEnabled) { return; } EvaluateImageResult result = null; try { this.TestContext.WriteLine("ImageEvaluate - Running Data Row"); switch (type) { case "url": { result = await this.client.EvaluateImageAsync( this.TestContext.DataRow["DataVal"].ToString(), DataRepresentationType.Url, bool.Parse(this.TestContext.DataRow["Cache"].ToString())); break; } case "raw": { var webClient = new WebClient(); byte[] imageBytes = webClient.DownloadData(this.TestContext.DataRow["DataVal"].ToString()); result = await this.client.EvaluateImageAsync( new MemoryStream(imageBytes), bool.Parse(this.TestContext.DataRow["Cache"].ToString())); break; } } if (expectSizeException) { Assert.Fail("Image Size Expection was expected!!"); } Assert.IsNotNull(result); if (bool.Parse(this.TestContext.DataRow["Cache"].ToString())) { Assert.IsTrue(!string.IsNullOrWhiteSpace(result.CacheID), "Expected CacheId, Response: {0}", JsonConvert.SerializeObject(result)); } else { Assert.IsTrue(string.IsNullOrWhiteSpace(result.CacheID), "CacheId NOT EXPECTED, Response: {0}", JsonConvert.SerializeObject(result)); } Assert.IsTrue(result.AdultClassificationScore > 0, "Expected higher than 0 Adult Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(result)); Assert.IsTrue(result.RacyClassificationScore > 0, "Expected higher than 0 Racy Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(result)); Assert.AreEqual(result.IsImageAdultClassified, bool.Parse(this.TestContext.DataRow["IsAdult"].ToString()), "Image should not have been classified as Adult, Response: {0}", JsonConvert.SerializeObject(result)); Assert.AreEqual(result.IsImageRacyClassified, bool.Parse(this.TestContext.DataRow["IsRacy"].ToString()), "Image should not have been classified as Racy, Response: {0}", JsonConvert.SerializeObject(result)); } catch (Microsoft.CognitiveServices.ContentModerator.ClientException ex) { if ((ex.Error.Message != "Image Size Error") || !expectSizeException) { throw; } } }