public void TagImageTest() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "TagImageTest"); string imageUrl = GetTestImageUrl("house.jpg"); using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance())) { TagResult result = client.TagImageAsync(imageUrl).Result; Assert.Matches("^\\d{4}-\\d{2}-\\d{2}(-preview)?$", result.ModelVersion); var expects = new string[] { "grass", "outdoor", "building", "plant", "property", "home", "house", "real estate", "sky", "siding", "porch", "yard", "cottage", "garden buildings", "door", "lawn", "window", "farmhouse", "tree", "backyard", "driveway", "shed", "roof", "land lot" }; var intersect = expects.Intersect(result.Tags.Select(tag => tag.Name).ToArray()).ToArray(); Assert.True(intersect.Length == expects.Length); // Confirm tags are in descending confidence order var orignalConfidences = result.Tags.Select(tag => tag.Confidence).ToArray(); var sortedConfidences = orignalConfidences.OrderByDescending(c => c).ToArray(); Assert.Equal(sortedConfidences, orignalConfidences); } } }
/// <summary> /// Sends a URL to Cognitive Services and generates tags for it. /// </summary> /// <param name="imageUrl">The URL of the image for which to generate tags.</param> /// <returns>Awaitable tagging result.</returns> private async Task <TagResult> GenerateTagsForUrlAsync(string imageUrl) { // ----------------------------------------------------------------------- // KEY SAMPLE CODE STARTS HERE // ----------------------------------------------------------------------- // // Create Cognitive Services Vision API Service client. // using (var client = new ComputerVisionClient(Credentials) { Endpoint = Endpoint }) { Log("ComputerVisionClient is created"); // // Generate tags for the given URL. // Log("Calling ComputerVisionClient.TagImageAsync()..."); string language = (_language.SelectedItem as RecognizeLanguage).ShortCode; TagResult analysisResult = await client.TagImageAsync(imageUrl, language); return(analysisResult); } // ----------------------------------------------------------------------- // KEY SAMPLE CODE ENDS HERE // ----------------------------------------------------------------------- }
public TagResult SaveTag(TagResult tagResult) { if (tagResult == null || tagResult.TagForm == null) { throw new ArgumentNullException("No TagForm send"); } if (string.IsNullOrEmpty(tagResult.TagForm.Name)) { throw new ArgumentException("Empty Name", nameof(tagResult.TagForm.Name)); } var tagDto = TagParse(tagResult); if (string.IsNullOrEmpty(tagResult.TagForm.Id) || tagResult.TagForm.Id == "0") { tagDto.Id = 0; tagDto = _tagRepository.Insert(tagDto); } else { tagDto.Id = int.Parse(tagResult.TagForm.Id); tagDto = _tagRepository.Update(tagDto); } return(TagParse(tagDto)); }
public IActionResult GetById(int id, DateTimeOffset?timestamp = null) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var status = _entityIndex.Status(ResourceType.Tag, id) ?? ContentStatus.Published; if (!UserPermissions.IsAllowedToGet(User.Identity, status, _entityIndex.Owner(ResourceType.Tag, id))) { return(Forbid()); } var tag = _db.Database.GetCollection <Tag>(ResourceType.Tag.Name) .AsQueryable() .FirstOrDefault(x => x.Id == id); if (tag == null) { return(NotFound()); } if (timestamp != null && tag.Timestamp <= timestamp.Value) { return(StatusCode(304)); } var tagResult = new TagResult(tag) { Timestamp = _referencesIndex.LastModificationCascading(ResourceType.Tag, id) }; return(Ok(tagResult)); }
public async Task <OcrResultDTO> Post() { StringBuilder sb = new StringBuilder(); OcrResultDTO ocrResultDTO = new OcrResultDTO(); try { if (Request.Form.Files.Count > 0) { var file = Request.Form.Files[Request.Form.Files.Count - 1]; if (file.Length > 0) { var memoryStream = new MemoryStream(); file.CopyTo(memoryStream); byte[] imageFileBytes = memoryStream.ToArray(); memoryStream.Flush(); string JSONResult = await ReadTextFromStreamAzure(imageFileBytes); string JSONResult2 = await ReadTextFromStreamAWS(imageFileBytes); ImageDescription imgDescAzure = JsonConvert.DeserializeObject <ImageDescription>(JSONResult); var AWSList = JsonConvert.DeserializeObject <List <string> >(JSONResult2); ImageAnalysis imageAnalysis = JsonConvert.DeserializeObject <ImageAnalysis>(JSONResult); TagResult TagResult = JsonConvert.DeserializeObject <TagResult>(JSONResult); //OcrResult ocrResult = JsonConvert.DeserializeObject<OcrResult>(JSONResult); var AzureList = new List <string>(); foreach (var item in imageAnalysis.Description.Tags) { AzureList.Add(item); } var selectedRTRCAzure = GetWinner(AzureList); var selectedRTRCAWS = GetWinner(AWSList); sb.Append("*********************Azure*********************** "); sb.Append("\n"); sb.Append(selectedRTRCAzure); sb.Append("\n"); sb.Append("**********************AWS************************ "); sb.Append("\n"); sb.Append(selectedRTRCAWS); sb.Append("\n"); ocrResultDTO.DetectedText = sb.ToString(); } } return(ocrResultDTO); } catch { ocrResultDTO.DetectedText = "Error occurred. Try again"; ocrResultDTO.Language = "unk"; return(ocrResultDTO); } }
public void TagImageInStreamTest() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "TagImageInStreamTest"); using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance())) using (FileStream stream = new FileStream(GetTestImagePath("house.jpg"), FileMode.Open)) { const string Chinese = "zh"; TagResult result = client.TagImageInStreamAsync(stream, Chinese).Result; Assert.Matches("^\\d{4}-\\d{2}-\\d{2}(-preview)?$", result.ModelVersion); var expects = new string[] { "草", "户外", "建筑", "植物", "财产", "家", "屋子", "不动产", "天空", "护墙板", "门廊", "院子", "小别墅", "花园建筑", "门", "草坪", "窗户/车窗", "农舍", "树", "后院", "车道", "小屋", "屋顶", "地段" }; var intersect = expects.Intersect(result.Tags.Select(tag => tag.Name).ToArray()).ToArray(); Assert.True(intersect.Length == expects.Length); } } }
/// <summary> /// Uploads the image to Cognitive Services and generates tags. /// </summary> /// <param name="imageFilePath">The image file path.</param> /// <returns>Awaitable tagging result.</returns> private async Task <TagResult> UploadAndGetTagsForImageAsync(string imageFilePath) { // ----------------------------------------------------------------------- // KEY SAMPLE CODE STARTS HERE // ----------------------------------------------------------------------- // // Create Cognitive Services Vision API Service client. // using (var client = new ComputerVisionClient(Credentials) { Endpoint = Endpoint }) using (Stream imageFileStream = File.OpenRead(imageFilePath)) { Log("ComputerVisionClient is created"); // // Upload and image and generate tags. // Log("Calling ComputerVisionClient.TagImageInStreamAsync()..."); string language = (_language.SelectedItem as RecognizeLanguage).ShortCode; TagResult analysisResult = await client.TagImageInStreamAsync(imageFileStream, language); return(analysisResult); } // ----------------------------------------------------------------------- // KEY SAMPLE CODE ENDS HERE // ----------------------------------------------------------------------- }
public void TestTagAdd() { var tag = GetMethodTag(); ImageUploadParams uploadParams = new ImageUploadParams() { File = new FileDescription(m_testImagePath), Tags = m_apiTag }; ImageUploadResult uploadResult = m_cloudinary.Upload(uploadParams); TagParams tagParams = new TagParams() { Command = TagCommand.Add, Tag = tag }; tagParams.PublicIds.Add(uploadResult.PublicId); TagResult tagResult = m_cloudinary.Tag(tagParams); Assert.AreEqual(1, tagResult.PublicIds.Length); Assert.AreEqual(uploadResult.PublicId, tagResult.PublicIds[0]); }
//Creates one instances of connection and avaids exhaust of ssockets public static void Main(string[] args) { try { Console.WriteLine("Started Tag Api application"); HttpService.InitializeService(); TagResult objResult = new TagResult(); bool bSuccess = HttpService.GetTagsAll(objResult).GetAwaiter().GetResult(); Console.WriteLine($"Populated All Tags : {bSuccess}"); if (bSuccess) { //Insert into DB HttpService.tagService.TruncateTagTable(); bool bSuccessResult = HttpService.tagService.InsertTagResult(objResult); HttpService.tagService.MergeTagTable(); Console.WriteLine("Inserted data into Tags table successfully"); } } catch (Exception ex) { Console.WriteLine($"ERROR : {ex.Message}"); } Console.WriteLine("Finished Tag Api application"); }
public async Task <TagResult> TagAsync(TagParams parameters) { using (var response = await Api.CallAsync(HttpMethod.Post, Api.ApiUrlImgUpV.Action("tags").BuildUrl(), parameters.ToParamsDictionary(), null, null)) { return(await TagResult.Parse(response)); } }
public async Task <OcrResultDTO> Post([FromBody] myImg image) { StringBuilder sb = new StringBuilder(); OcrResultDTO ocrResultDTO = new OcrResultDTO(); // var baseURLImage = "https://www.fmpilot2.com/Attachment/IFM/AAR-PV08/WEB-1721300/1[20200616_182215543].jpg"; try { if (!string.IsNullOrEmpty(image.filename)) { string JSONResult = await ReadTextFromStreamAzureUrl(image.filename); //string JSONResult2 = await ReadTextFromStreamAWS(ImageUrl); ImageDescription imgDescAzure = JsonConvert.DeserializeObject <ImageDescription>(JSONResult); // var AWSList = JsonConvert.DeserializeObject<List<string>>(JSONResult2); ImageAnalysis imageAnalysis = JsonConvert.DeserializeObject <ImageAnalysis>(JSONResult); TagResult TagResult = JsonConvert.DeserializeObject <TagResult>(JSONResult); //OcrResult ocrResult = JsonConvert.DeserializeObject<OcrResult>(JSONResult); var AzureList = new List <string>(); if (imageAnalysis.Description != null) { foreach (var item in imageAnalysis.Description.Tags) { AzureList.Add(item); } var selectedRTRCAzure = GetWinner(AzureList); // var selectedRTRCAWS = GetWinner(AWSList); sb.Append("*********************Azure*********************** "); sb.Append("\n"); sb.Append(selectedRTRCAzure); sb.Append("\n"); sb.Append("**********************AWS************************ "); sb.Append("\n"); // sb.Append(selectedRTRCAWS); sb.Append("\n"); ocrResultDTO.DetectedText = sb.ToString(); } else { ocrResultDTO.DetectedText = "Cannot process this image"; } } return(ocrResultDTO); } catch { ocrResultDTO.DetectedText = "Error occurred. Try again"; ocrResultDTO.Language = "unk"; return(ocrResultDTO); } }
private static void DisplayTags(TagResult tags) { foreach (var tag in tags.Tags) { Console.WriteLine("{0}\twith confidence {1}", tag.Name, tag.Confidence); } Console.WriteLine("\n"); }
/// <summary> /// Show Tagging Result. /// </summary> /// <param name="result">Tag result to log.</param> protected void LogTagResult(TagResult result) { ImageAnalysis analysisResult = new ImageAnalysis { Metadata = result.Metadata, Tags = result.Tags }; LogAnalysisResult(analysisResult); }
/// <summary> /// Manage tag assignments /// </summary> /// <param name="parameters">Parameters of tag management</param> /// <returns>Results of tags management</returns> public TagResult Tag(TagParams parameters) { string uri = m_api.ApiUrlImgUpV.Action("tags").BuildUrl(); using (HttpWebResponse response = m_api.Call(HttpMethod.POST, uri, parameters.ToParamsDictionary(), null)) { TagResult result = TagResult.Parse(response); return(result); } }
public async Task PushRenameTagJobStatusUpdate(string uniqueKey, TagResult tagResult, JobStage jobStage) { await _hubContext.Clients.All.PushRenameTagJobStatusUpdate( new RenameTagJobStatus { UniqueKey = uniqueKey, Item = tagResult, JobStage = jobStage } ); }
private TagDto TagParse(TagResult tagResult) { TagDto tagDto = new TagDto { Id = int.Parse(tagResult.TagForm.Id), Name = tagResult.TagForm.Name, Symbol = tagResult.TagForm.Symbol }; return(tagDto); }
public IActionResult Post([FromBody] TagResult tag) { try { object tagResult = _tagService.SaveTag(tag); return(Ok(tagResult)); } catch (Exception exc) { _logger.LogError(exc, exc.Message); return(BadRequest(exc.Message)); } }
/// <summary> /// Helper function /// </summary> /// <param name="tagResult"></param> /// <returns></returns> private bool AnalyzeTags(TagResult tagResult) { var extractedTags = new List <string>(); foreach (var item in tagResult.Tags) { extractedTags.Add(item.Name); } bool isNeatlyWrapped = _neatlyWrapped.Intersect(extractedTags).Count() == _neatlyWrapped.Count(); return(isNeatlyWrapped); }
private TagResult TagParse(TagDto tagDto) { TagResult tagResult = new TagResult { TagForm = new TagForm() }; tagResult.TagForm.Id = tagDto.Id.ToString(); tagResult.TagForm.Name = tagDto.Name; tagResult.TagForm.Symbol = tagDto.Symbol; return(tagResult); }
// Analyze a remote image private static async Task TagImageFromUrlAsync(ComputerVisionClient computerVision, string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl); return; } TagResult tags = await computerVision.TagImageAsync(imageUrl); Console.WriteLine(imageUrl); DisplayTags(tags); }
public ActionResult AddTagToArticle(int articleid, int tagid) { TagResult entity = _tagresultRepository.AddTagToArticle(articleid, tagid); // _timelineService.RegisterActivity(Realm.BG, ActivityType.ProductionCreated, User.Id); // Feedback.AddMessageSuccess(DepartmentResources.DepartmentAddSuccessMessage); //return RedirectToAction(nameof(Index), nameof(DepartmentController).RemoveControllerSuffix()); //return View("Manager", model); return(Json(new { ок = true //name = entity.Name })); }
public void ClearGivenTag() { Cloudinary cloudinary = TestUtilities.GetCloudinary(); TagParams tagParams = new TagParams() { Command = TagCommand.Remove, Tag = "another_tag" }; tagParams.PublicIds.Add("UploadWithTags"); tagParams.PublicIds.Add("de9wjix4hhnqpxixq6cw"); TagResult tagResult = cloudinary.Tag(tagParams); TestUtilities.LogAndWrite(tagResult, "ClearGivenTag.txt"); }
public void ModifyAssignedTags() { Cloudinary cloudinary = TestUtilities.GetCloudinary(); TagParams tagParams = new TagParams() { Command = TagCommand.Add, Tag = "another_tag" }; tagParams.PublicIds.Add("UploadWithTags"); tagParams.PublicIds.Add("de9wjix4hhnqpxixq6cw"); TagResult tagResult = cloudinary.Tag(tagParams); TestUtilities.LogAndWrite(tagResult, "ModifyAssignedTags.txt"); }
/// <summary> /// Create entity. /// </summary> public TagResult AddTagToArticle(int articleid, int tagid) { using (var connection = _dbConnectionFactory.CreateConnection()) { connection.Open(); using (var transaction = connection.BeginTransaction()) { var parameters = new DynamicParameters(); parameters.Add("@ArticleId", articleid, DbType.Int32); parameters.Add("@TagId", tagid, DbType.Int32); // Configure outputs parameters.Add("@EntityId", dbType: DbType.Int32, direction: ParameterDirection.Output); parameters.Add("@ErrorMessage", dbType: DbType.String, size: 1000, direction: ParameterDirection.Output); // Execute query int result = connection.Execute( sql: TagResultInsert_Proc, commandType: CommandType.StoredProcedure, transaction: transaction, param: parameters ); // Check for errors string errorMessage = parameters.Get <string>("@ErrorMessage"); if (!string.IsNullOrEmpty(errorMessage)) { throw new RepositoryException(errorMessage); } TagResult entity = new TagResult { ArticleId = articleid, TagId = tagid }; // Set the id entity.Id = parameters.Get <int>("@EntityId"); transaction.Commit(); return(entity); } } }
// Analyze a local image private static async Task TagImageFromStreamAsync(ComputerVisionClient computerVision, string imagePath) { if (!File.Exists(imagePath)) { Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath); return; } using (Stream imageStream = File.OpenRead(imagePath)) { TagResult tags = await computerVision.TagImageInStreamAsync(imageStream); Console.WriteLine(imagePath); DisplayTags(tags); } }
public async Task <List <string> > AnalyseVideo(int fps) { Console.WriteLine($"Analysing video {Bytes.Length} bytes"); WriteVideoFile(); ExtractFrames(fps); List <string> tags = new List <string>(); //Console.WriteLine("Connecting to Azure"); ApiKeyServiceClientCredentials creds = new ApiKeyServiceClientCredentials(Environment.GetEnvironmentVariable("computerVisionApiKey")); IComputerVisionAPI azure = new ComputerVisionAPI(creds, new HttpThrottleHandler(new HttpClientHandler())); azure.AzureRegion = AzureRegions.Westeurope; string[] frameFiles = Directory.GetFiles(BaseDirectory, $"*{ImageFileExt}") .Select(Path.GetFileName).ToArray(); // So that the latest ones are more likely to get scanned before 429 errors happen frameFiles.Reverse(); Console.WriteLine($"Processing {frameFiles.Length} frames"); foreach (string frameFile in frameFiles) { try { Stream stream = new FileStream(BaseDirectory + "/" + frameFile, FileMode.Open); TagResult tagResult = await azure.TagImageInStreamAsync(stream); tags.AddRange(tagResult.Tags.Select(tag => tag.Name)); } catch (Exception ex) { //Console.WriteLine($"Error: {ex.Message} {ex.InnerException?.Message}"); } } tags = tags.Distinct().ToList(); Console.WriteLine($"Tags retreived: {string.Join(",", tags)}"); return(tags); }
public void TagImageInStreamTest() { using (MockContext context = MockContext.Start(this.GetType().FullName)) { HttpMockServer.Initialize(this.GetType().FullName, "TagImageInStreamTest"); const string Chinese = "zh"; using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance())) using (FileStream stream = new FileStream(GetTestImagePath("house.jpg"), FileMode.Open)) { TagResult result = client.TagImageInStreamAsync(stream, Chinese).Result; Assert.Equal( new string[] { "草", "户外", "天空", "屋子", "建筑", "绿色", "草坪", "住宅", "绿色的", "家", "房子", "屋顶", "平房", "车库", "历史" }, result.Tags.Select(tag => tag.Name).ToArray()); } } }
public void TagImageInStreamTest() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "TagImageInStreamTest"); const string Chinese = "zh"; using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance())) using (FileStream stream = new FileStream(GetTestImagePath("house.jpg"), FileMode.Open)) { TagResult result = client.TagImageInStreamAsync(stream, Chinese).Result; var expects = new string[] { "草", "户外", "天空", "屋子", "建筑", "绿色", "草坪", "住宅", "绿色的", "家", "房子" }; var intersect = expects.Intersect(result.Tags.Select(tag => tag.Name).ToArray()).ToArray(); Assert.True(intersect.Length == expects.Length); } } }
public void TagImageModelVersionTest() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "TagImageModelVersionTest"); using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance())) using (FileStream stream = new FileStream(GetTestImagePath("house.jpg"), FileMode.Open)) { const string Chinese = "zh"; const string targetModelVersion = "2021-04-01"; TagResult result = client.TagImageInStreamAsync( stream, Chinese, modelVersion: targetModelVersion).Result; Assert.Equal(targetModelVersion, result.ModelVersion); } } }
private void Create() { CurrentTags.Clear(); tagDictionary.Clear(); foreach (var device in myDevices.Values) { foreach (var tag in device.Collection.GetAllTags()) { if (tagDictionary.ContainsKey(tag.Name)) { continue; } TagResult aTag = new TagResult(tag); tagDictionary.Add(tag.Name, aTag); tag.TagStatusChanged += Tag_TagStatusChanged; CurrentTags.Add(aTag); } } DeviceCount = myDevices.Count; Status = LiveDataViewModelStatuses.Ready; }