using System.Globalization; using System.Windows; namespace Azaion.Annotator.DTO; public class AnnotationInfo { public int ClassNumber { get; set; } public double X { get; set; } public double Y { get; set; } public double Width { get; set; } public double Height { get; set; } public AnnotationInfo() { } public AnnotationInfo(int classNumber, double x, double y, double width, double height) { ClassNumber = classNumber; X = x; Y = y; Width = width; Height = height; } public override string ToString() => $"{ClassNumber} {X:F5} {Y:F5} {Width:F5} {Height:F5}".Replace(',', '.'); public AnnotationInfo ToLabelCoordinates(Size canvasSize, Size videoSize) { var cw = canvasSize.Width; var ch = canvasSize.Height; var canvasAR = cw / ch; var videoAR = videoSize.Width / videoSize.Height; var annInfo = new AnnotationInfo { ClassNumber = this.ClassNumber }; double left, top; if (videoAR > canvasAR) //100% width { left = X / cw; annInfo.Width = Width / cw; var realHeight = cw / videoAR; //real video height in pixels on canvas var blackStripHeight = (ch - realHeight) / 2.0; //height of black strips at the top and bottom top = (Y - blackStripHeight) / realHeight; annInfo.Height = Height / realHeight; } else //100% height { top = Y / ch; annInfo.Height = Height / ch; var realWidth = ch * videoAR; //real video width in pixels on canvas var blackStripWidth = (cw - realWidth) / 2.0; //height of black strips at the top and bottom left = (X - blackStripWidth) / realWidth; annInfo.Width = Width / realWidth; } annInfo.X = left + annInfo.Width / 2.0; annInfo.Y = top + annInfo.Height / 2.0; return annInfo; } public AnnotationInfo ToCanvasCoordinates(Size canvasSize, Size videoSize) { var cw = canvasSize.Width; var ch = canvasSize.Height; var canvasAR = cw / ch; var videoAR = videoSize.Width / videoSize.Height; var annInfo = new AnnotationInfo { ClassNumber = this.ClassNumber }; double left = X - Width / 2; double top = Y - Height / 2; if (videoAR > canvasAR) //100% width { var realHeight = cw / videoAR; //real video height in pixels on canvas var blackStripHeight = (ch - realHeight) / 2.0; //height of black strips at the top and bottom annInfo.X = left * cw; annInfo.Y = top * realHeight + blackStripHeight; annInfo.Width = Width * cw; annInfo.Height = Height * realHeight; } else //100% height { var realWidth = ch * videoAR; //real video width in pixels on canvas var blackStripWidth = (cw - realWidth) / 2.0; //height of black strips at the top and bottom annInfo.X = left * realWidth + blackStripWidth; annInfo.Y = top * ch; annInfo.Width = Width * realWidth; annInfo.Height = Height * ch; } return annInfo; } public static AnnotationInfo? Parse(string? s) { if (s == null || string.IsNullOrEmpty(s)) return null; var strs = s.Replace(',','.').Split(' '); if (strs.Length != 5) return null; try { var res = new AnnotationInfo { ClassNumber = int.Parse(strs[0], CultureInfo.InvariantCulture), X = double.Parse(strs[1], CultureInfo.InvariantCulture), Y = double.Parse(strs[2], CultureInfo.InvariantCulture), Width = double.Parse(strs[3], CultureInfo.InvariantCulture), Height = double.Parse(strs[4], CultureInfo.InvariantCulture) }; return res; } catch (Exception) { return null; } } }