Created
August 10, 2021 16:41
-
-
Save craigmarvelley/6fc2bfbd5d0646ad34cfb5fa95cb6ba4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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