Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension Array where Element == OCRCandidate {
func sortCandidates() -> [OCRCandidate] {
self.sorted { (currentCandidate, nextCandidate) -> Bool in
let currentCandidateBottomY = currentCandidate.boundingPoints.avgBottomY
let nextCandidateBottomY = nextCandidate.boundingPoints.avgBottomY
if currentCandidateBottomY < nextCandidateBottomY {
// Rounding item for discrepancies with Y positions.
let difference = (currentCandidateBottomY - nextCandidateBottomY).roundTo2dp
// Allowing 0.01 due to image distortion or any other anomalies with the image.
if abs(difference) <= 0.01 {
return currentCandidate.boundingPoints.bottomLeft.x < nextCandidate.boundingPoints.bottomLeft.x
} else {
return true
}
}
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment