หลังจากฝึกโมเดลของคุณเองโดยใช้ AutoML Vision Edge แล้ว คุณจะใช้โมเดลดังกล่าวในแอปเพื่อติดป้ายกำกับรูปภาพได้
ก่อนเริ่มต้น
- เพิ่ม Firebase ลงในโปรเจ็กต์ Android หากยังไม่ได้ดำเนินการ
- เพิ่มทรัพยากร Dependency สำหรับคลัง ML Kit สำหรับ Android ลงในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติจะเป็น
app/build.gradle
)apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies { // ... implementation 'com.google.firebase:firebase-ml-vision:24.0.3' implementation 'com.google.firebase:firebase-ml-vision-automl:18.0.5' }
1. โหลดโมเดล
ML Kit จะเรียกใช้โมเดลที่ AutoML สร้างขึ้นบนอุปกรณ์ อย่างไรก็ตาม คุณสามารถกําหนดค่า ML Kit ให้โหลดโมเดลจากระยะไกลจาก Firebase, จากพื้นที่เก็บข้อมูลในเครื่อง หรือทั้ง 2 อย่าง
โฮสติ้งโมเดลใน Firebase จะช่วยให้คุณอัปเดตโมเดลได้โดยไม่ต้องเผยแพร่แอปเวอร์ชันใหม่ และสามารถใช้ Remote Config และ A/B Testing เพื่อแสดงโมเดลต่างๆ ให้กับผู้ใช้กลุ่มต่างๆ ได้อย่างไดนามิก
หากเลือกที่จะระบุเฉพาะโมเดลโดยโฮสต์กับ Firebase และไม่ได้รวมไว้ในแอป คุณจะลดขนาดการดาวน์โหลดเริ่มต้นของแอปได้ แต่โปรดทราบว่าหากไม่ได้รวมโมเดลไว้ในแอป ฟังก์ชันการทำงานที่เกี่ยวข้องกับโมเดลจะไม่พร้อมใช้งานจนกว่าแอปจะดาวน์โหลดโมเดลเป็นครั้งแรก
การรวมโมเดลไว้ในแอปช่วยให้มั่นใจได้ว่าฟีเจอร์ ML ของแอปจะยังคงทํางานได้เมื่อโมเดลที่โฮสต์ใน Firebase ไม่พร้อมใช้งาน
กำหนดค่าแหล่งข้อมูลรูปแบบที่โฮสต์โดย Firebase
หากต้องการใช้โมเดลที่โฮสต์จากระยะไกล ให้สร้างออบเจ็กต์ FirebaseAutoMLRemoteModel
โดยระบุชื่อที่คุณกำหนดให้กับโมเดลเมื่อเผยแพร่
Java
// Specify the name you assigned in the Firebase console.
FirebaseAutoMLRemoteModel remoteModel =
new FirebaseAutoMLRemoteModel.Builder("your_remote_model").build();
Kotlin
// Specify the name you assigned in the Firebase console.
val remoteModel = FirebaseAutoMLRemoteModel.Builder("your_remote_model").build()
จากนั้นเริ่มงานการดาวน์โหลดโมเดลโดยระบุเงื่อนไขที่คุณต้องการอนุญาตให้ดาวน์โหลด หากโมเดลไม่ได้อยู่ในอุปกรณ์ หรือหากมีโมเดลเวอร์ชันใหม่กว่า แท็บจะดาวน์โหลดโมเดลจาก Firebase แบบไม่สอดคล้องกัน ดังนี้
Java
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder()
.requireWifi()
.build();
FirebaseModelManager.getInstance().download(remoteModel, conditions)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// Success.
}
});
Kotlin
val conditions = FirebaseModelDownloadConditions.Builder()
.requireWifi()
.build()
FirebaseModelManager.getInstance().download(remoteModel, conditions)
.addOnCompleteListener {
// Success.
}
แอปจํานวนมากจะเริ่มการดาวน์โหลดในโค้ดเริ่มต้น แต่คุณก็เริ่มได้ทุกเมื่อก่อนที่จะต้องใช้โมเดล
กำหนดค่าแหล่งข้อมูลโมเดลในเครื่อง
วิธีรวมโมเดลกับแอป
- แตกไฟล์โมเดลและข้อมูลเมตาของโมเดลจากไฟล์ ZIP ที่คุณดาวน์โหลดจากคอนโซล Firebase เราขอแนะนำให้คุณใช้ไฟล์ตามที่ดาวน์โหลดมาโดยไม่มีการแก้ไข (รวมถึงชื่อไฟล์)
-
รวมโมเดลและไฟล์ข้อมูลเมตาของโมเดลไว้ในแพ็กเกจแอป
- หากไม่มีโฟลเดอร์ชิ้นงานในโปรเจ็กต์ ให้สร้างโฟลเดอร์โดยคลิกขวาที่โฟลเดอร์
app/
แล้วคลิกใหม่ > โฟลเดอร์ > โฟลเดอร์ชิ้นงาน - สร้างโฟลเดอร์ย่อยภายในโฟลเดอร์ชิ้นงานเพื่อเก็บไฟล์โมเดล
- คัดลอกไฟล์
model.tflite
,dict.txt
และmanifest.json
ไปยังโฟลเดอร์ย่อย (ไฟล์ทั้ง 3 ไฟล์ต้องอยู่ในโฟลเดอร์เดียวกัน)
- หากไม่มีโฟลเดอร์ชิ้นงานในโปรเจ็กต์ ให้สร้างโฟลเดอร์โดยคลิกขวาที่โฟลเดอร์
- เพิ่มข้อมูลต่อไปนี้ลงในไฟล์
build.gradle
ของแอปเพื่อให้แน่ใจว่า Gradle จะไม่บีบอัดไฟล์โมเดลเมื่อสร้างแอป ไฟล์โมเดลจะรวมอยู่ในแพ็กเกจแอปและพร้อมใช้งานสำหรับ ML Kit เป็นชิ้นงานดิบandroid { // ... aaptOptions { noCompress "tflite" } }
- สร้างออบเจ็กต์
FirebaseAutoMLLocalModel
โดยระบุเส้นทางไปยังไฟล์ Manifest ของรูปแบบ ดังนี้Java
FirebaseAutoMLLocalModel localModel = new FirebaseAutoMLLocalModel.Builder() .setAssetFilePath("manifest.json") .build();
Kotlin
val localModel = FirebaseAutoMLLocalModel.Builder() .setAssetFilePath("manifest.json") .build()
สร้างเครื่องมือติดป้ายกำกับรูปภาพจากโมเดล
หลังจากกําหนดค่าแหล่งที่มาของโมเดลแล้ว ให้สร้างFirebaseVisionImageLabeler
ออบเจ็กต์จากแหล่งที่มาแหล่งใดแหล่งหนึ่ง
หากมีเพียงโมเดลที่รวมอยู่ในเครื่อง ให้สร้างเครื่องติดป้ายกำกับจากออบเจ็กต์ FirebaseAutoMLLocalModel
แล้วกําหนดค่าเกณฑ์คะแนนความเชื่อมั่นที่ต้องการ (ดูประเมินโมเดล)
Java
FirebaseVisionImageLabeler labeler;
try {
FirebaseVisionOnDeviceAutoMLImageLabelerOptions options =
new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
.setConfidenceThreshold(0.0f) // Evaluate your model in the Firebase console
// to determine an appropriate value.
.build();
labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options);
} catch (FirebaseMLException e) {
// ...
}
Kotlin
val options = FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
.setConfidenceThreshold(0) // Evaluate your model in the Firebase console
// to determine an appropriate value.
.build()
val labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options)
หากมีโมเดลที่โฮสต์จากระยะไกล คุณจะต้องตรวจสอบว่าได้ดาวน์โหลดโมเดลแล้วก่อนที่จะเรียกใช้ คุณตรวจสอบสถานะของงานดาวน์โหลดรูปแบบได้โดยใช้isModelDownloaded()
วิธีการของตัวจัดการรูปแบบ
แม้ว่าคุณจะต้องยืนยันข้อมูลนี้ก่อนเรียกใช้โปรแกรมติดป้ายกำกับ แต่หากมีทั้งโมเดลที่โฮสต์จากระยะไกลและโมเดลที่รวมอยู่ในเครื่อง ก็อาจต้องทำการตรวจสอบนี้เมื่อสร้างอินสแตนซ์โปรแกรมติดป้ายกำกับรูปภาพ โดยสร้างโปรแกรมติดป้ายกำกับจากโมเดลระยะไกลหากมีการดาวน์โหลดไว้ และจากโมเดลในเครื่องหากไม่ได้ดาวน์โหลดไว้
Java
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener(new OnSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean isDownloaded) {
FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder optionsBuilder;
if (isDownloaded) {
optionsBuilder = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(remoteModel);
} else {
optionsBuilder = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel);
}
FirebaseVisionOnDeviceAutoMLImageLabelerOptions options = optionsBuilder
.setConfidenceThreshold(0.0f) // Evaluate your model in the Firebase console
// to determine an appropriate threshold.
.build();
FirebaseVisionImageLabeler labeler;
try {
labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options);
} catch (FirebaseMLException e) {
// Error.
}
}
});
Kotlin
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener { isDownloaded ->
val optionsBuilder =
if (isDownloaded) {
FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(remoteModel)
} else {
FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
}
// Evaluate your model in the Firebase console to determine an appropriate threshold.
val options = optionsBuilder.setConfidenceThreshold(0.0f).build()
val labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options)
}
หากคุณมีเพียงโมเดลที่โฮสต์จากระยะไกล คุณควรปิดใช้ฟังก์ชันการทำงานที่เกี่ยวข้องกับโมเดล เช่น ปิดใช้หรือซ่อน UI บางส่วน จนกว่าคุณจะยืนยันว่าดาวน์โหลดโมเดลแล้ว โดยแนบโปรแกรมรับฟังกับเมธอด download()
ของเครื่องมือจัดการโมเดล ดังนี้
Java
FirebaseModelManager.getInstance().download(remoteModel, conditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void v) {
// Download complete. Depending on your app, you could enable
// the ML feature, or switch from the local model to the remote
// model, etc.
}
});
Kotlin
FirebaseModelManager.getInstance().download(remoteModel, conditions)
.addOnCompleteListener {
// Download complete. Depending on your app, you could enable the ML
// feature, or switch from the local model to the remote model, etc.
}
2. เตรียมรูปภาพอินพุต
จากนั้นสําหรับรูปภาพแต่ละรูปที่ต้องการติดป้ายกํากับ ให้สร้างออบเจ็กต์ FirebaseVisionImage
โดยใช้ตัวเลือกใดตัวเลือกหนึ่งที่อธิบายในส่วนนี้ แล้วส่งไปยังอินสแตนซ์ของ FirebaseVisionImageLabeler
(อธิบายไว้ในส่วนถัดไป)
คุณสามารถสร้างออบเจ็กต์ FirebaseVisionImage
จากออบเจ็กต์ media.Image
, ไฟล์ในอุปกรณ์, อาร์เรย์ไบต์ หรือออบเจ็กต์ Bitmap
ได้โดยทำดังนี้
-
หากต้องการสร้างออบเจ็กต์
FirebaseVisionImage
จากออบเจ็กต์media.Image
เช่น เมื่อจับภาพจากกล้องของอุปกรณ์ ให้ส่งออบเจ็กต์media.Image
และการหมุนของรูปภาพไปยังFirebaseVisionImage.fromMediaImage()
หากคุณใช้ไลบรารี CameraX คลาส
OnImageCapturedListener
และImageAnalysis.Analyzer
จะคํานวณค่าการหมุนให้คุณ คุณจึงต้องแปลงการหมุนเป็นค่าคงที่ROTATION_
ของ ML Kit ก่อนเรียกใช้FirebaseVisionImage.fromMediaImage()
Java
private class YourAnalyzer implements ImageAnalysis.Analyzer { private int degreesToFirebaseRotation(int degrees) { switch (degrees) { case 0: return FirebaseVisionImageMetadata.ROTATION_0; case 90: return FirebaseVisionImageMetadata.ROTATION_90; case 180: return FirebaseVisionImageMetadata.ROTATION_180; case 270: return FirebaseVisionImageMetadata.ROTATION_270; default: throw new IllegalArgumentException( "Rotation must be 0, 90, 180, or 270."); } } @Override public void analyze(ImageProxy imageProxy, int degrees) { if (imageProxy == null || imageProxy.getImage() == null) { return; } Image mediaImage = imageProxy.getImage(); int rotation = degreesToFirebaseRotation(degrees); FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation); // Pass image to an ML Kit Vision API // ... } }
Kotlin
private class YourImageAnalyzer : ImageAnalysis.Analyzer { private fun degreesToFirebaseRotation(degrees: Int): Int = when(degrees) { 0 -> FirebaseVisionImageMetadata.ROTATION_0 90 -> FirebaseVisionImageMetadata.ROTATION_90 180 -> FirebaseVisionImageMetadata.ROTATION_180 270 -> FirebaseVisionImageMetadata.ROTATION_270 else -> throw Exception("Rotation must be 0, 90, 180, or 270.") } override fun analyze(imageProxy: ImageProxy?, degrees: Int) { val mediaImage = imageProxy?.image val imageRotation = degreesToFirebaseRotation(degrees) if (mediaImage != null) { val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) // Pass image to an ML Kit Vision API // ... } } }
หากไม่ได้ใช้คลังกล้องที่ระบุการหมุนของรูปภาพ คุณจะคำนวณการหมุนได้จากการหมุนของอุปกรณ์และการวางแนวของเซ็นเซอร์กล้องในอุปกรณ์ โดยทำดังนี้
Java
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private int getRotationCompensation(String cameraId, Activity activity, Context context) throws CameraAccessException { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int rotationCompensation = ORIENTATIONS.get(deviceRotation); // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE); int sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION); rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360; // Return the corresponding FirebaseVisionImageMetadata rotation value. int result; switch (rotationCompensation) { case 0: result = FirebaseVisionImageMetadata.ROTATION_0; break; case 90: result = FirebaseVisionImageMetadata.ROTATION_90; break; case 180: result = FirebaseVisionImageMetadata.ROTATION_180; break; case 270: result = FirebaseVisionImageMetadata.ROTATION_270; break; default: result = FirebaseVisionImageMetadata.ROTATION_0; Log.e(TAG, "Bad rotation value: " + rotationCompensation); } return result; }
Kotlin
private val ORIENTATIONS = SparseIntArray() init { ORIENTATIONS.append(Surface.ROTATION_0, 90) ORIENTATIONS.append(Surface.ROTATION_90, 0) ORIENTATIONS.append(Surface.ROTATION_180, 270) ORIENTATIONS.append(Surface.ROTATION_270, 180) } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Throws(CameraAccessException::class) private fun getRotationCompensation(cameraId: String, activity: Activity, context: Context): Int { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. val deviceRotation = activity.windowManager.defaultDisplay.rotation var rotationCompensation = ORIENTATIONS.get(deviceRotation) // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. val cameraManager = context.getSystemService(CAMERA_SERVICE) as CameraManager val sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION)!! rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360 // Return the corresponding FirebaseVisionImageMetadata rotation value. val result: Int when (rotationCompensation) { 0 -> result = FirebaseVisionImageMetadata.ROTATION_0 90 -> result = FirebaseVisionImageMetadata.ROTATION_90 180 -> result = FirebaseVisionImageMetadata.ROTATION_180 270 -> result = FirebaseVisionImageMetadata.ROTATION_270 else -> { result = FirebaseVisionImageMetadata.ROTATION_0 Log.e(TAG, "Bad rotation value: $rotationCompensation") } } return result }
จากนั้นส่งออบเจ็กต์
media.Image
และค่าการหมุนไปยังFirebaseVisionImage.fromMediaImage()
ดังนี้Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
Kotlin
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
- หากต้องการสร้างออบเจ็กต์
FirebaseVisionImage
จาก URI ของไฟล์ ให้ส่งบริบทแอปและ URI ของไฟล์ไปยังFirebaseVisionImage.fromFilePath()
ซึ่งจะมีประโยชน์เมื่อคุณใช้ IntentACTION_GET_CONTENT
เพื่อแจ้งให้ผู้ใช้เลือกรูปภาพจากแอปแกลเลอรีJava
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
Kotlin
val image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
- หากต้องการสร้างออบเจ็กต์
FirebaseVisionImage
จากByteBuffer
หรืออาร์เรย์ไบต์ ให้คำนวณการหมุนภาพก่อนตามที่อธิบายไว้ข้างต้นสำหรับอินพุตmedia.Image
จากนั้นสร้างออบเจ็กต์
FirebaseVisionImageMetadata
ซึ่งมีข้อมูลความสูง กว้าง รูปแบบการเข้ารหัสสี และการหมุนของรูปภาพJava
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build();
Kotlin
val metadata = FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build()
ใช้บัฟเฟอร์หรืออาร์เรย์ รวมถึงออบเจ็กต์ข้อมูลเมตาเพื่อสร้างออบเจ็กต์
FirebaseVisionImage
ดังนี้Java
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
Kotlin
val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
- วิธีสร้างออบเจ็กต์
FirebaseVisionImage
จากออบเจ็กต์Bitmap
Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Kotlin
val image = FirebaseVisionImage.fromBitmap(bitmap)
Bitmap
ต้องตั้งตรงโดยไม่จำเป็นต้องหมุนเพิ่มเติม
3. เรียกใช้โปรแกรมติดป้ายกำกับรูปภาพ
หากต้องการติดป้ายกำกับวัตถุในรูปภาพ ให้ส่งผ่านออบเจ็กต์ FirebaseVisionImage
ไปยังเมธอด processImage()
ของ FirebaseVisionImageLabeler
Java
labeler.processImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() {
@Override
public void onSuccess(List<FirebaseVisionImageLabel> labels) {
// Task completed successfully
// ...
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
// ...
}
});
Kotlin
labeler.processImage(image)
.addOnSuccessListener { labels ->
// Task completed successfully
// ...
}
.addOnFailureListener { e ->
// Task failed with an exception
// ...
}
หากติดป้ายกำกับรูปภาพสำเร็จ ระบบจะส่งอาร์เรย์ของออบเจ็กต์ FirebaseVisionImageLabel
ไปยัง Listener ที่ดำเนินการสำเร็จ คุณดูข้อมูลเกี่ยวกับองค์ประกอบที่ระบบจดจำได้ในรูปภาพจากวัตถุแต่ละรายการ
เช่น
Java
for (FirebaseVisionImageLabel label: labels) {
String text = label.getText();
float confidence = label.getConfidence();
}
Kotlin
for (label in labels) {
val text = label.text
val confidence = label.confidence
}
เคล็ดลับในการปรับปรุงประสิทธิภาพแบบเรียลไทม์
- จำกัดการเรียกใช้เครื่องตรวจจับ หากเฟรมวิดีโอใหม่พร้อมใช้งานขณะที่ตัวตรวจจับทำงานอยู่ ให้วางเฟรม
- หากคุณใช้เอาต์พุตของตัวตรวจจับเพื่อวางกราฟิกซ้อนทับบนรูปภาพอินพุต ให้รับผลลัพธ์จาก ML Kit ก่อน จากนั้นจึงแสดงผลรูปภาพและวางซ้อนในขั้นตอนเดียว ซึ่งจะทำให้คุณแสดงผลไปยังพื้นผิวการแสดงผลเพียงครั้งเดียวสำหรับเฟรมอินพุตแต่ละเฟรม
-
หากคุณใช้ Camera2 API ให้จับภาพในรูปแบบ
ImageFormat.YUV_420_888
หากคุณใช้ Camera API เวอร์ชันเก่า ให้ถ่ายภาพในรูปแบบ
ImageFormat.NV21