This file contains 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 const val REQUEST_PERMISSIONS = 1 | |
private const val REQUEST_TAKE_PICTURE = 2 | |
class MainActivity : AppCompatActivity() { | |
private var photoFilePath = "" | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) |
This file contains 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
object UriHelper { | |
fun getUriFromFilePath(context: Context, filePath: String): Uri { | |
val file = File(filePath) | |
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
FileProvider.getUriForFile( | |
context, | |
context.applicationContext.packageName + ".uri", | |
file) | |
} else { |
This file contains 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
class MainActivity : AppCompatActivity() { | |
private lateinit var classifier: Classifier | |
//other properties | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
checkPermissions() |
This file contains 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
interface Classifier { | |
fun recognizeImage(bitmap: Bitmap): Result | |
} |
This file contains 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
data class Result(val result: String, val confidence: Float) |
This file contains 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
const val GRAPH_FILE_PATH = "file:///android_asset/graph.pb" | |
const val LABELS_FILE_PATH = "file:///android_asset/labels.txt" | |
const val GRAPH_INPUT_NAME = "input" | |
const val GRAPH_OUTPUT_NAME = "final_result" | |
const val IMAGE_SIZE = 224 |
This file contains 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
class MainActivity : AppCompatActivity { | |
//... | |
private val handler = Handler() | |
private var photoFilePath = "" | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
val file = File(photoFilePath) | |
if (requestCode == REQUEST_TAKE_PICTURE && file.exists()) { | |
classifyPhoto(file) |
This file contains 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
object ImageClassifierFactory { | |
fun create( | |
assetManager: AssetManager, | |
graphFilePath: String, | |
labelsFilePath: String, | |
imageSize: Int, | |
inputName: String, | |
outputName: String | |
): Classifier { |
This file contains 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 const val ENABLE_LOG_STATS = false | |
class ImageClassifier ( | |
private val inputName: String, | |
private val outputName: String, | |
private val imageSize: Long, | |
private val labels: List<String>, | |
private val imageBitmapPixels: IntArray, | |
private val imageNormalizedPixels: FloatArray, | |
private val results: FloatArray, |
This file contains 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
class MainActivity : AppCompatActivity { | |
//... | |
private fun classifyAndShowResult(croppedBitmap: Bitmap) { | |
runInBackground( | |
Runnable { | |
val result = classifier.recognizeImage(croppedBitmap) | |
showResult(result) | |
}) |
OlderNewer