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
private fun analyze(frame: MLFrame) { | |
// Create a task to process the result returned by the image segmentation analyzer. | |
val task: Task<MLImageSegmentation> = analyzer?.asyncAnalyseFrame(frame)!! | |
// Asynchronously process the result returned by the image segmentation analyzer. | |
task.addOnSuccessListener { | |
// Detection success. | |
Log.i(TAG, "Success - Result is $it") | |
configureAfterImage(it.foreground) | |
}.addOnFailureListener { |
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
private fun initializeAnalyzer() { | |
analyzer = MLAnalyzerFactory.getInstance().imageSegmentationAnalyzer | |
} |
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
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
initializeAnalyzer() | |
binding?.selectImageButton?.setOnClickListener { askImageFromUser() } | |
binding?.analyzeButton?.setOnClickListener { | |
analyze(getMLFrame(selectedImageBitmap ?: return@setOnClickListener)) | |
} | |
} |
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
private fun releaseDetectionResources() { | |
if (analyzer != null) { | |
try { | |
analyzer!!.stop() | |
} catch (e: IOException) { | |
Log.e(TAG, "Exception is $e") | |
} | |
} | |
} |
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
private fun initializeAnalyzer() { | |
val setting = MLImageSegmentationSetting.Factory() // Set whether to support fine segmentation. The value true indicates fine segmentation, and the value false indicates fast segmentation. | |
.setExact(false) | |
.setAnalyzerType(MLImageSegmentationSetting.BODY_SEG) | |
.setScene(MLImageSegmentationScene.FOREGROUND_ONLY) | |
.create() | |
analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting) | |
analyzer?.setTransactor(ImageSegmentAnalyzerTransactor(binding?.graphic ?: return)) | |
} |
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
private fun initializeLensEngine() { | |
lensEngine = LensEngine.Creator(activity, analyzer) | |
.setLensType(LensEngine.FRONT_LENS) | |
.applyDisplayDimension(WIDTH, HEIGHT) | |
.applyFps(FPS) | |
.enableAutomaticFocus(true) | |
.create() | |
} |
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
override fun onResume() { | |
super.onResume() | |
openCameraStream(lensEngine ?: return) | |
} | |
override fun onPause() { | |
super.onPause() | |
closeCameraStream() | |
} |
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
// 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 |
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
enum class TextLanguage(val languageCode: String) { | |
ENGLISH("en"), | |
CHINESE( "cn"), | |
TURKISH("tr") | |
} |
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
// TextRecognitionViewModel.kt | |
fun initializeStreamAnalyzer(context: Context) { | |
textRecognizer.initializeStreamAnalyzer(context) | |
} | |
// TextRecognizer.kt | |
fun initializeStreamAnalyzer(context: Context) { | |
initializeDeviceStreamAnalyzer(context) | |
} |