Skip to content

Instantly share code, notes, and snippets.

@yektasarioglu
Last active January 5, 2021 08:53
Show Gist options
  • Save yektasarioglu/4094c938b24584b0274039a01b24ea11 to your computer and use it in GitHub Desktop.
Save yektasarioglu/4094c938b24584b0274039a01b24ea11 to your computer and use it in GitHub Desktop.
The initialization of static text analyzer
// TextRecognitionViewModel.kt
fun initializeTextAnalyzer(isNetworkConnected: Boolean) {
textRecognizer.initializeTextAnalyzer(isNetworkConnected, TextLanguage.TURKISH)
}
// TextRecognizer.kt
fun initializeTextAnalyzer(isNetworkConnected: Boolean = false, textLanguage: TextLanguage) {
if (isNetworkConnected)
initializeCloudTextAnalyzer(listOf(textLanguage.languageCode))
else
initializeDeviceTextAnalyzer(textLanguage.languageCode)
}
private fun initializeCloudTextAnalyzer(supportedLanguageList: List<String>) {
Timber.d("On-Cloud analyzer initialized !!")
setting = MLRemoteTextSetting.Factory() // Set the on-cloud text detection mode.
// MLRemoteTextSetting.OCR_COMPACT_SCENE: dense text recognition
// MLRemoteTextSetting.OCR_LOOSE_SCENE: sparse text recognition
.setTextDensityScene(MLRemoteTextSetting.OCR_COMPACT_SCENE) // Specify the languages that can be recognized, which should comply with ISO 639-1. .setLanguageList(supportedLanguageList) // TODO: Is sending single language makes it more precise?
// MLRemoteTextSetting.NGON: Return the coordinates of the four corner points of the quadrilateral.
// MLRemoteTextSetting.ARC: Return the corner points of a polygon border in an arc. The coordinates of up to 72 corner points can be returned.
.setBorderType(MLRemoteTextSetting.ARC)
.create()
analyzer = MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer(setting as? MLRemoteTextSetting)
}
private fun initializeDeviceTextAnalyzer(language: String) {
Timber.d("On-Device analyzer initialized !!")
setting = MLLocalTextSetting.Factory()
.setOCRMode(MLLocalTextSetting.OCR_DETECT_MODE)
.setLanguage(language)
.create()
analyzer = MLAnalyzerFactory.getInstance().getLocalTextAnalyzer(setting as? MLLocalTextSetting
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment