Skip to content

Instantly share code, notes, and snippets.

@craigmarvelley
Created August 10, 2021 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigmarvelley/6fc2bfbd5d0646ad34cfb5fa95cb6ba4 to your computer and use it in GitHub Desktop.
Save craigmarvelley/6fc2bfbd5d0646ad34cfb5fa95cb6ba4 to your computer and use it in GitHub Desktop.
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