Last active
August 1, 2018 11:13
-
-
Save Pooh3Mobi/3fc04bee5b65936073b5cb488b783357 to your computer and use it in GitHub Desktop.
ML Kit Sample : thanks by source https://github.com/yanzm/MLKitSample
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
package input.your.pkg | |
import android.graphics.Bitmap | |
import android.view.View | |
import com.google.firebase.ml.vision.FirebaseVision | |
import com.google.firebase.ml.vision.common.FirebaseVisionImage | |
import kotlinx.android.synthetic.main.activity_main.* | |
public fun MainActivity.detectTextOnCloud(bitmap: Bitmap) { | |
detectButton.isEnabled = false | |
progressBar.visibility = View.VISIBLE | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
// val options = FirebaseVisionCloudDetectorOptions.Builder() | |
// .setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL) | |
// .setMaxResults(15) | |
// .build() | |
FirebaseVision.getInstance() | |
// .getVisionCloudTextDetector(options) | |
.visionCloudTextDetector | |
.detectInImage(image) | |
.addOnSuccessListener { cloudText -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
for (page in cloudText.pages) { | |
for (block in page.blocks) { | |
for (paragraph in block.paragraphs) { | |
for (word in paragraph.words) { | |
val text = word.symbols.joinToString(separator = "") { it.text } | |
word.boundingBox?.let { | |
overlay.add(BoxData(text, it)) | |
} | |
} | |
} | |
} | |
} | |
} | |
.addOnFailureListener { e -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
e.printStackTrace() | |
} | |
} | |
public fun MainActivity.labelOnCloud(bitmap: Bitmap) { | |
detectButton.isEnabled = false | |
progressBar.visibility = View.VISIBLE | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
// val options = FirebaseVisionCloudDetectorOptions.Builder() | |
// .setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL) | |
// .setMaxResults(15) | |
// .build() | |
FirebaseVision.getInstance() | |
// .getVisionCloudLabelDetector(options) | |
.visionCloudLabelDetector | |
.detectInImage(image) | |
.addOnSuccessListener { labels -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
overlay.add(TextsData(labels.map { "${it.label}, ${it.confidence}" })) | |
} | |
.addOnFailureListener { e -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
e.printStackTrace() | |
} | |
} | |
public fun MainActivity.detectLandOnCloud(bitmap: Bitmap) { | |
detectButton.isEnabled = false | |
progressBar.visibility = View.VISIBLE | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
// val options = FirebaseVisionCloudDetectorOptions.Builder() | |
// .setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL) | |
// .setMaxResults(15) | |
// .build() | |
FirebaseVision.getInstance() | |
// .getVisionCloudLandmarkDetector(options) | |
.visionCloudLandmarkDetector | |
.detectInImage(image) | |
.addOnSuccessListener { labels -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
overlay.add(TextsData(labels.map { "${it.landmark}, ${it.confidence}" })) | |
} | |
.addOnFailureListener { e -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
e.printStackTrace() | |
} | |
} |
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
package input.your.pkg | |
import android.graphics.Bitmap | |
import android.view.View | |
import com.google.firebase.ml.vision.FirebaseVision | |
import com.google.firebase.ml.vision.common.FirebaseVisionImage | |
import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetectorOptions | |
import kotlinx.android.synthetic.main.activity_main.* | |
public fun MainActivity.detectText(bitmap: Bitmap) { | |
detectButton.isEnabled = false | |
progressBar.visibility = View.VISIBLE | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
FirebaseVision.getInstance() | |
.visionTextDetector | |
.detectInImage(image) | |
.addOnSuccessListener { texts -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
for (block in texts.blocks) { | |
for (line in block.lines) { | |
for (element in line.elements) { | |
element.boundingBox?.let { | |
overlay.add(BoxData(element.text, it)) | |
} | |
} | |
} | |
} | |
} | |
.addOnFailureListener { e -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
e.printStackTrace() | |
} | |
} | |
public fun MainActivity.detectFaces(bitmap: Bitmap) { | |
detectButton.isEnabled = false | |
progressBar.visibility = View.VISIBLE | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
val options = FirebaseVisionFaceDetectorOptions.Builder() | |
.setModeType(FirebaseVisionFaceDetectorOptions.ACCURATE_MODE) | |
.setLandmarkType(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS) | |
.setClassificationType(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS) | |
.setMinFaceSize(0.15f) | |
.setTrackingEnabled(true) | |
.build() | |
FirebaseVision.getInstance() | |
.getVisionFaceDetector(options) | |
.detectInImage(image) | |
.addOnSuccessListener { faces -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
for (face in faces) { | |
face.boundingBox?.let { | |
overlay.add(BoxData("", it)) | |
} | |
} | |
} | |
.addOnFailureListener { e -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
e.printStackTrace() | |
} | |
} | |
public fun MainActivity.readBarCodes(bitmap: Bitmap) { | |
detectButton.isEnabled = false | |
progressBar.visibility = View.VISIBLE | |
// バーコードの種類を変更できる | |
// val options = FirebaseVisionBarcodeDetectorOptions.Builder() | |
// .setBarcodeFormats( | |
// FirebaseVisionBarcode.FORMAT_EAN_8, | |
// FirebaseVisionBarcode.FORMAT_EAN_13) | |
// .build() | |
// | |
// The following formats are supported: | |
// | |
// Code 128 (FORMAT_CODE_128) | |
// Code 39 (FORMAT_CODE_39) | |
// Code 93 (FORMAT_CODE_93) | |
// Codabar (FORMAT_CODABAR) | |
// EAN-13 (FORMAT_EAN_13) | |
// EAN-8 (FORMAT_EAN_8) | |
// ITF (FORMAT_ITF) | |
// UPC-A (FORMAT_UPC_A) | |
// UPC-E (FORMAT_UPC_E) | |
// QR Code (FORMAT_QR_CODE) | |
// PDF417 (FORMAT_PDF417) | |
// Aztec (FORMAT_AZTEC) | |
// Data Matrix (FORMAT_DATA_MATRIX) | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
FirebaseVision.getInstance() | |
// .getVisionBarcodeDetector(options) | |
.visionBarcodeDetector | |
.detectInImage(image) | |
.addOnSuccessListener { barcodes -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
for (barcode in barcodes) { | |
barcode.boundingBox?.let { | |
overlay.add(BoxData(barcode.rawValue ?: "", it)) | |
} | |
} | |
} | |
.addOnFailureListener { e -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
e.printStackTrace() | |
} | |
} | |
public fun MainActivity.label(bitmap: Bitmap) { | |
detectButton.isEnabled = false | |
progressBar.visibility = View.VISIBLE | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
// 閾値を指定 | |
// val options = FirebaseVisionLabelDetectorOptions.Builder() | |
// .setConfidenceThreshold(0.8f) | |
// .build() | |
FirebaseVision.getInstance() | |
// .getVisionLabelDetector(options) | |
.visionLabelDetector | |
.detectInImage(image) | |
.addOnSuccessListener { labels -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
overlay.add(TextsData(labels.map { "${it.label}, ${it.confidence}" })) | |
} | |
.addOnFailureListener { e -> | |
detectButton.isEnabled = true | |
progressBar.visibility = View.GONE | |
e.printStackTrace() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment