Skip to content

Instantly share code, notes, and snippets.

@GenchoBG
Last active April 14, 2019 14:41
Show Gist options
  • Save GenchoBG/4967478920d78f8c81660890dd8a3837 to your computer and use it in GitHub Desktop.
Save GenchoBG/4967478920d78f8c81660890dd8a3837 to your computer and use it in GitHub Desktop.
Once the have the data loaded we pass it into this function which identifies if there is an error.
public static bool IsEverythingFine(List<SubPicture> subPictures)
{
const int tolerance = 100;
var labels = subPictures.Where(sp => sp.Label.Contains("label")).ToList();
var maxItemY = subPictures.Except(labels).Min(i => i.TopLeft.Y);
foreach (var label in labels.ToArray())
{
if (label.TopLeft.Y < maxItemY)
{
labels.Remove(label);
}
}
var boundries = new List<int>();
var boundriesDict = new Dictionary<int, List<SubPicture>>();
foreach (var label in labels)
{
var labelY = label.TopLeft.Y;
if (!boundries.Any())
{
boundries.Add(labelY);
boundriesDict[labelY] = new List<SubPicture>() { label };
}
var found = false;
foreach (var boundry in boundries)
{
//if slightly upper
if (labelY <= boundry && labelY + tolerance >= boundry)
{
//we are in the clear
boundriesDict[boundry].Add(label);
found = true;
break;
}
//if slightly lower
if (labelY >= boundry && labelY - tolerance <= boundry)
{
//we are in the clear
boundriesDict[boundry].Add(label);
found = true;
break;
}
}
if (!found)
{
//not anywhere near
boundries.Add(labelY);
boundriesDict[labelY] = new List<SubPicture>() { label };
}
}
boundries = boundries.OrderBy(b => b).ToList();
var rowsWithItems = new Dictionary<int, List<SubPicture>>();
foreach (var boundry in boundries)
{
rowsWithItems.Add(boundry, new List<SubPicture>());
}
foreach (var boundtyKvp in boundriesDict)
{
foreach (var boundry in boundtyKvp.Value)
{
rowsWithItems[boundtyKvp.Key].Add(boundry);
}
}
foreach (var item in subPictures.Except(labels))
{
if (boundries.Count > 1)
{
for (int i = 0; i < boundries.Count - 1; i++)
{
if (item.TopLeft.Y >= boundries[i] && item.TopLeft.Y <= boundries[i + 1])
{
rowsWithItems[boundries[i]].Add(item);
break;
}
}
}
else
{
if (item.TopLeft.Y <= boundries[0])
{
rowsWithItems[boundries[0]].Add(item);
}
}
}
foreach (var row in rowsWithItems)
{
var checkedLabels = new HashSet<string>();
var items = row.Value.OrderBy(i => i.TopLeft.X).ToList();
var currentGroupLabel = items[0].Label.Replace("label_", "");
checkedLabels.Add(currentGroupLabel);
for (int i = 1; i < items.Count; i++)
{
var current = items[i];
var currentLabel = current.Label;
if (currentLabel.Contains("label_"))
{
currentLabel = currentLabel.Replace("label_", "");
}
if (currentLabel != currentGroupLabel)
{
if (checkedLabels.Contains(currentLabel))
{
return false;
}
currentGroupLabel = currentLabel;
}
}
}
var itemsNames = subPictures.Except(labels).Select(i => i.Label).Distinct().ToHashSet();
foreach (var label in labels)
{
var labelName = label.Label.Replace("label_", "");
if (!itemsNames.Contains(labelName))
{
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment