public async Task Initialize(HyperTag tag) { this._hyperTag = tag; this.HyperTagLabel.Value = tag.GetElement <HyperTagLabel>()?.Label ?? ""; var ids = tag.GetElements <IHyperTagHyperIds>().FirstOrDefault(e => e.HyperId.TrackId.Value.Type == HyperTrackTypes.Video); this.HyperTagId = ids; if (this.Context?.ShowFragmentAndSlice == true) { this.HyperTagFragmentSliceLabel = $"@ {ids.HyperId.FragmentId}:{ids.HyperId.SliceId}"; } var startAtElement = tag.GetElements <HyperTagTime>().FirstOrDefault(t => t.TimeType == HyperTagTime.TimeTypes.StreamTime); this.StartAt = (int)(startAtElement.StreamTime_TimeSpan?.TotalSeconds ?? 0); var execution = tag.GetElement <TagonomyExecutionResultHyperTagElement>(); if (execution != null && execution.Result != null) { var steps = execution.Result.FinishedPaths?.SelectMany(p => p.Steps).ToArray(); if (steps != null && steps.Any()) { var tagonomyLabels = new List <string>(); foreach (TagonomyExecutionStep step in steps) { // Do not print the initial Tagonomy start step, as this is simply the name of the Tagonomy, which is not informative here. if (string.IsNullOrEmpty(step.ParentPathNodeElementId) == false) { tagonomyLabels.Add(step.OptionalTargetNodeName); } var checkLeafs = step.Actions.Where(it => it.NodeElementTypeName == typeof(CheckStateLeafNodeElement).FullName && ((CheckStateLeafNodeElement.ElementUniformResult)it.Result).IsChecked); foreach (var item in checkLeafs) { tagonomyLabels.Add(item.NodeName); } } TagonomyLabels = tagonomyLabels; } } Image = await LoadImage(tag); }
private async Task <byte[]> LoadImage(HyperTag tag) { if (Context?.HyperStore == null) { return(null); } var ids = this.HyperTagId; var geometry = tag.GetElement <HyperTagGeometry>(); var args2 = new RetrieveFragmentFramesArgs() { AssetId = ids.HyperId.AssetId.Value, FragmentId = ids.HyperId.HasFullFragmentData ? ids.HyperId.FragmentId.Value : new HyperFragmentId(0), SliceIds = new HyperSliceId[] { ids.HyperId.HasFullSliceData?ids.HyperId.SliceId.Value : new HyperSliceId(0) }, GeometryItem = this.Context?.ExtractMode == true ? geometry?.GeometryItem : null, FabricServiceId = this.Context?.FabricServiceId, }; var sliceResult = await Context.HyperStore.ExecuteAsync(args2); if (sliceResult == null || sliceResult.Length == 0 || sliceResult[0].Image?.Data == null) { return(null); } byte[] imageData = sliceResult[0].Image.Data; if (this.Context?.ExtractMode != true) { imageData = TagRenderHelper.RenderTag(tag, imageData); } return(imageData); }
public SKPoint[] ProcessHyperTag(HyperTag tag, int width, int height) { var geometry = tag.GetElement <HyperTagGeometry>(); var homographyEl = tag.GetElement <HomographyTagElement>(); // Ensure this is an AOI, not something else, like measuremenet etc. if (geometry == null || homographyEl == null) { return(null); } var polygon = (UniPolygon2f)geometry.GeometryItem.Shape; var convertedPoints = polygon.Points.Select(it => geometry.GeometryItem.ConvertFromXSpace(it)).Select(it => new Point((int)(it.X * width), (int)(it.Y * height))).ToArray(); // Calcuate the min and max positions of the extracted regions globally for all polygons (if many). _minX = convertedPoints.Min(x => x.X); _maxX = convertedPoints.Max(x => x.X); _minY = convertedPoints.Min(x => x.Y); _maxY = convertedPoints.Max(x => x.Y); if (_minX.HasValue && _minX.Value % 2 == 1) { _minX = _minX.Value + 1; } if (_maxX.HasValue && _maxX.Value % 2 == 1) { _maxX = _maxX.Value + 1; } if (_minY.HasValue && _minY.Value % 2 == 1) { _minY = _minY.Value + 1; } if (_maxY.HasValue && _maxY.Value % 2 == 1) { _maxY = _maxY.Value + 1; } return(convertedPoints.Select(x => new SKPoint(x.X, x.Y)).ToArray()); }
public static byte[] RenderTag(HyperTag tag, byte[] imageData) { if (imageData == null || imageData.Length == 0) { return(imageData); } using (var bitmapStream = new MemoryStream(imageData)) { using (Bitmap bitmap = new Bitmap(bitmapStream)) { using (var g = Graphics.FromImage(bitmap)) { foreach (var component in tag.GetElements <HyperTagGeometry>()) { var rect = component.GeometryItem.BoundingBox; if (component.GeometryItem.SpaceMode == GeometryItem.SpaceModes.XSpace) { rect = component.GeometryItem.ConvertFromAbsoluteXSpaceToRealWorldSpace(rect, bitmap.Width, bitmap.Height); } lock (g_penSyncRoot) // Pen is not thread safe { var geometry = tag.GetElement <HyperTagGeometry>(); if (geometry.GeometryItem.Shape is UniPolygon2f polygonX) { UniPolygon2f polygon = polygonX; if (geometry.GeometryItem.SpaceMode == GeometryItem.SpaceModes.XSpace) { polygon = geometry.GeometryItem.ConvertFromAbsoluteXSpaceToRealWorldSpace(polygonX, bitmap.Width, bitmap.Height); g.DrawPolygon(g_pen, polygon.Points.Select(it => new PointF(it.X, it.Y))?.ToArray()); } g.DrawPolygon(g_pen, polygon.Points.Select(it => new PointF(it.X, it.Y))?.ToArray()); } else { g.DrawRectangle(g_pen, new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height)); } } } } using (var writeStream = new MemoryStream()) { bitmap.Save(writeStream, ImageFormat.Jpeg); return(writeStream.ToArray()); } } } }
private TagMaskExtraction ProcessMaskedTag(HyperTag tag) { var geometry = tag.GetElement <HyperTagGeometry>(); var geometryMask = tag.GetElement <HyperTagGeometryMask>(); if (geometryMask == null || geometry == null) { return(null); } //var classification = tag.GetElements<HyperTagLabel>().Where(x => x.Type == HyperTagLabel.Types.Classification).Select(x => x.Label).FirstOrDefault(); //if (classification == null) //{ // classification = tag.GetElement<TagonomyExecutionResultHyperTagElement>()?.GetCombinedLabel(); //} var rect = geometry.GeometryItem.BoundingBox; rect = geometry.GeometryItem.ConvertFromAbsoluteXSpaceToRealWorldSpace(rect, _width, _height); var skBitmap = SKBitmap.Decode(geometryMask.RawData); if (skBitmap.Width != geometryMask.Width || skBitmap.Height != geometryMask.Height) { var newSkBitmap = new SKBitmap((int)rect.Width, (int)rect.Height); using (skBitmap) { SKBitmap.Resize(newSkBitmap, skBitmap, SKBitmapResizeMethod.Lanczos3); } skBitmap = newSkBitmap; } return(new TagMaskExtraction { Image = skBitmap, Rect = rect }); }
private void ProcessGeometryTag(HyperTag tag, List <HeatmapRenderHelper.HeatPoint> heatpoints) { var geometryItems = tag.GetElements <HyperTagGeometry>().Select(x => x.GeometryItem).ToList(); geometryItems.AddRange(tag.GetElements <HyperTagTrackingData>().SelectMany(x => x.TrackingSequence.Select(ts => ts.GeometryItem))); var classification = tag.GetElements <HyperTagLabel>().Where(x => x.Type == HyperTagLabel.Types.Classification).Select(x => x.Label).FirstOrDefault(); if (classification == null) { classification = tag.GetElement <TagonomyExecutionResultHyperTagElement>()?.GetCombinedLabel(); } //var setting = Settings.Classifications.FirstOrDefault(x => x.Classification == classification); //if (setting == null) //{ // setting = new ClassificationSettings // { // Classification = classification, // IsEnabled = true, // Color = Settings.Classifications.First().Color // }; // Settings.Classifications.Add(setting); //} //setting.DebugCount++; foreach (var geo in geometryItems) { var rect = geo.BoundingBox; if (geo.SpaceMode != GeometryItem.SpaceModes.Normal) { rect = geo.ConvertFromXSpace(rect); } heatpoints.Add(new HeatmapRenderHelper.HeatPoint((int)(rect.Center.X * _width), (int)(rect.Bottom * _height), 255, classification)); } }