I
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using VideoOS.Platform;
using VideoOS.Platform.Search;
using VideoOS.Platform.Search.Results;
namespace Human.SearchAgent
{
public class HumanSearchDefinition : SearchDefinition
{
public HumanSearchDefinition(SearchScope searchScope) : base(searchScope)
{ }
protected override void Search(Guid sessionId, DateTime from, DateTime to, IEnumerable<Item> items, CancellationToken cancellationToken)
{
var results = new List<SearchResultData>();
var xmlResults = GetResultFromXML();
foreach (var r in xmlResults)
{
var searchResult = new HumanSearchResultData(GenerateSeededGuide(r.Id),r)
{
Title = Convert.ToString("Width:"+r.width+"\nHeight:"+r.height+"\nX:"+r.x_position+"\nY:"+r.y_position),//"Human", // TODO: Default title is camera name, but you can modify it if needed.
Item = items.First(i => i.FQID.ObjectId == r.deviceID),
BeginTime = r.objectRecognitionTime,
EndTime = r.objectRecognitionTime.AddSeconds(1),
TriggerTime = r.objectRecognitionTime//from.AddTicks((to - from).Ticks / 2)
};
results.Add(searchResult);
}
if(results.Any())
FireSearchResultsReadyEvent(sessionId, results);
}
private Guid GenerateSeededGuide(int seed)
{
var r = new Random(seed);
var guide = new byte[16];
r.NextBytes(guide);
return new Guid(guide);
}
private static List<HumanMetaData> GetResultFromXML()
{
var sourceFolder = "C://<Path to XML>";
var files = Directory.GetFiles(sourceFolder, "*.xml", SearchOption.AllDirectories);
var results = new List<HumanMetaData>();
int count = 0;
foreach (var file in files)
{
var doc = XDocument.Load(file);
var h = doc.Root.Element("event").Descendants("entity").ElementAt(0).Attribute("height").Value;
var time = doc.Root.Element("event").Attribute("frameTimestamp").Value;
var x = doc.Root.Element("event").Descendants("entity").ElementAt(0).Attribute("position-x").Value;
var y = doc.Root.Element("event").Descendants("entity").ElementAt(0).Attribute("position-y").Value;
var w = doc.Root.Element("event").Descendants("entity").ElementAt(0).Attribute("width").Value;
var t = doc.Root.Element("event").Descendants("entity").ElementAt(0).Attribute("type").Value;
count = count + 1;
results.Add(new HumanMetaData
{ Id = count,
deviceID = new Guid("D873A47D-2D9A-4EEF-8D55-F3117D4D8FBA"),
objectRecognitionTime = Convert.ToDateTime(time),
height = Convert.ToDouble(h),
x_position = Convert.ToDouble(x),
y_position = Convert.ToDouble(y),
width = Convert.ToDouble(w),
type = t
});
}
return results;
}
}
internal class HumanSearchResultData : SearchResultData
{
public HumanMetaData HumanMetaData { get; }
public HumanSearchResultData(Guid id, HumanMetaData humanMetaData) : base(id)
{
HumanMetaData = humanMetaData;
}
protected override Task<ICollection<BoundingShape>> GetBoundingShapesAsync(CancellationToken cancellationToken)
{
return Task.Run(delegate
{
ICollection<BoundingShape> shapes = new List<BoundingShape>();
shapes.Add(new RectangleBoundingShape
{
X = HumanMetaData.x_position,
Y = HumanMetaData.y_position,
Width = HumanMetaData.width,
Height = HumanMetaData.height
});
return shapes;
}, cancellationToken);
}
/*protected override Task<ICollection<ResultProperty>> GetPropertiesAsync(CancellationToken cancellationToken)
{
return base.GetPropertiesAsync(cancellationToken);
}
protected override Task<ICollection<BoundingShape>> GetBoundingShapesAsync(CancellationToken cancellationToken)
{
return Task.Run(delegate
{
ICollection<BoundingShape> shapes = new List<BoundingShape>();
shapes.Add(new RectangleBoundingShape
{
X = HumanMetaData.x_position,
Y = HumanMetaData.y_position,
Width = HumanMetaData.width,
Height = HumanMetaData.height
});
return shapes;
}, cancellationToken);
}
*/
protected override Task<ICollection<ResultProperty>> GetPropertiesAsync(CancellationToken cancellationToken)
{
return Task.Run(delegate
{
ICollection<ResultProperty> properties = new List<ResultProperty>();
properties.Add(new ResultProperty("Human Type", "Human"));
return properties;
}, cancellationToken);
}
}
internal class HumanMetaData
{
public int Id { get; set; }
public Guid deviceID { get; set; }
public DateTime objectRecognitionTime { get; set; }
public double height { get; set; }
public double x_position { get; set; }
public double y_position { get; set; }
public double width { get; set; }
public string type { get; set; }
//public string colors = "Yellow";
}
}
Above is my code for HumanSearchDefinition.cs for detecting human events from xml data and put bounding box around them. This code is able to get the events but the bounding boxes are not visible. Can suggest what is wrong in the code ?


