Sử dụng Bộ công cụ học máy để dễ dàng thêm các tính năng phân đoạn theo chủ đề vào ứng dụng của bạn.
Tính năng | Chi tiết |
---|---|
Tên SDK | play-services-mlkit-subject-segmentation |
Triển khai | Chưa theo gói: mô hình được tải xuống một cách linh động bằng Dịch vụ Google Play. |
Tác động của kích thước ứng dụng | Tăng kích thước khoảng 200 KB. |
Thời gian khởi chạy | Người dùng có thể phải đợi mô hình tải xuống trước khi sử dụng lần đầu. |
Dùng thử
- Hãy thử ứng dụng mẫu để xem ví dụ về cách sử dụng API này.
Trước khi bắt đầu
- Trong tệp
build.gradle
ở cấp dự án, hãy nhớ đưa kho lưu trữ Maven của Google vào cả hai phầnbuildscript
vàallprojects
. - Thêm phần phụ thuộc của thư viện phân đoạn chủ đề Bộ công cụ học máy vào tệp gradle cấp ứng dụng của mô-đun, thường là
app/build.gradle
:
dependencies {
implementation 'com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1'
}
Như đã đề cập ở trên, mô hình này do Dịch vụ Google Play cung cấp.
Bạn có thể định cấu hình ứng dụng để tự động tải mô hình xuống thiết bị
sau khi cài đặt ứng dụng qua Cửa hàng Play. Để làm như vậy, hãy thêm nội dung khai báo sau vào tệp AndroidManifest.xml
của ứng dụng:
<application ...>
...
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="subject_segment" >
<!-- To use multiple models: android:value="subject_segment,model2,model3" -->
</application>
Bạn cũng có thể kiểm tra rõ ràng tình trạng cung cấp của mô hình và yêu cầu tải xuống thông qua Dịch vụ Google Play bằng ModuleInstallClient API.
Nếu bạn không cho phép tải mô hình xuống tại thời điểm cài đặt hoặc yêu cầu tải xuống một cách rõ ràng, thì mô hình sẽ được tải xuống trong lần đầu tiên bạn chạy trình phân đoạn. Các yêu cầu bạn đưa ra trước khi tải xuống hoàn tất sẽ không có kết quả.
1. Chuẩn bị hình ảnh đầu vào
Để thực hiện phân đoạn trên hình ảnh, hãy tạo một đối tượng InputImage
từ Bitmap
, media.Image
, ByteBuffer
, mảng byte hoặc một tệp trên thiết bị.
Bạn có thể tạo đối tượng InputImage
từ nhiều nguồn, mỗi nguồn được giải thích ở bên dưới.
Sử dụng media.Image
Để tạo đối tượng InputImage
từ đối tượng media.Image
, chẳng hạn như khi bạn chụp ảnh từ máy ảnh của thiết bị, hãy truyền đối tượng media.Image
và chế độ xoay của hình ảnh đến InputImage.fromMediaImage()
.
Nếu bạn sử dụng thư viện
CameraX, thì các lớp OnImageCapturedListener
và ImageAnalysis.Analyzer
sẽ tính giá trị xoay cho bạn.
Kotlin
private class YourImageAnalyzer : ImageAnalysis.Analyzer { override fun analyze(imageProxy: ImageProxy) { val mediaImage = imageProxy.image if (mediaImage != null) { val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees) // Pass image to an ML Kit Vision API // ... } } }
Java
private class YourAnalyzer implements ImageAnalysis.Analyzer { @Override public void analyze(ImageProxy imageProxy) { Image mediaImage = imageProxy.getImage(); if (mediaImage != null) { InputImage image = InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees()); // Pass image to an ML Kit Vision API // ... } } }
Nếu không sử dụng thư viện máy ảnh cho biết độ xoay của hình ảnh, bạn có thể tính độ xoay của hình ảnh dựa trên độ xoay của thiết bị và hướng của cảm biến máy ảnh trong thiết bị:
Kotlin
private val ORIENTATIONS = SparseIntArray() init { ORIENTATIONS.append(Surface.ROTATION_0, 0) ORIENTATIONS.append(Surface.ROTATION_90, 90) ORIENTATIONS.append(Surface.ROTATION_180, 180) ORIENTATIONS.append(Surface.ROTATION_270, 270) } /** * 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, isFrontFacing: Boolean): 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) // Get the device's sensor orientation. val cameraManager = activity.getSystemService(CAMERA_SERVICE) as CameraManager val sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION)!! if (isFrontFacing) { rotationCompensation = (sensorOrientation + rotationCompensation) % 360 } else { // back-facing rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360 } return rotationCompensation }
Java
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 0); ORIENTATIONS.append(Surface.ROTATION_90, 90); ORIENTATIONS.append(Surface.ROTATION_180, 180); ORIENTATIONS.append(Surface.ROTATION_270, 270); } /** * 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, boolean isFrontFacing) 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); // Get the device's sensor orientation. CameraManager cameraManager = (CameraManager) activity.getSystemService(CAMERA_SERVICE); int sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION); if (isFrontFacing) { rotationCompensation = (sensorOrientation + rotationCompensation) % 360; } else { // back-facing rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360; } return rotationCompensation; }
Sau đó, hãy truyền đối tượng media.Image
và giá trị độ xoay đến InputImage.fromMediaImage()
:
Kotlin
val image = InputImage.fromMediaImage(mediaImage, rotation)
Java
InputImage image = InputImage.fromMediaImage(mediaImage, rotation);
Sử dụng URI tệp
Để tạo đối tượng InputImage
từ URI tệp, hãy chuyển ngữ cảnh ứng dụng và URI tệp đến InputImage.fromFilePath()
. Điều này rất hữu ích khi bạn sử dụng ý định ACTION_GET_CONTENT
để nhắc người dùng chọn một hình ảnh trong ứng dụng thư viện của họ.
Kotlin
val image: InputImage try { image = InputImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
Java
InputImage image; try { image = InputImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
Sử dụng ByteBuffer
hoặc ByteArray
Để tạo một đối tượng InputImage
từ ByteBuffer
hoặc ByteArray
, trước tiên, hãy tính độ xoay hình ảnh như mô tả trước đó cho phương thức nhập media.Image
.
Sau đó, hãy tạo đối tượng InputImage
bằng vùng đệm hoặc mảng, cùng với chiều cao, chiều rộng, định dạng mã hoá màu và độ xoay của hình ảnh:
Kotlin
val image = InputImage.fromByteBuffer( byteBuffer, /* image width */ 480, /* image height */ 360, rotationDegrees, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 ) // Or: val image = InputImage.fromByteArray( byteArray, /* image width */ 480, /* image height */ 360, rotationDegrees, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 )
Java
InputImage image = InputImage.fromByteBuffer(byteBuffer, /* image width */ 480, /* image height */ 360, rotationDegrees, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 ); // Or: InputImage image = InputImage.fromByteArray( byteArray, /* image width */480, /* image height */360, rotation, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 );
Sử dụng Bitmap
Để tạo đối tượng InputImage
qua đối tượng Bitmap
, hãy khai báo sau:
Kotlin
val image = InputImage.fromBitmap(bitmap, 0)
Java
InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);
Hình ảnh được biểu thị bằng một đối tượng Bitmap
cùng với độ xoay.
2. Tạo một bản sao của SubjectSegmenter
Xác định các tuỳ chọn trình phân đoạn
Để phân đoạn hình ảnh của bạn, trước tiên hãy tạo một bản sao của SubjectSegmenterOptions
như sau:
Kotlin
val options = SubjectSegmenterOptions.Builder() // enable options .build()
Java
SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() // enable options .build();
Dưới đây là chi tiết về từng lựa chọn:
Mặt nạ tin cậy khi ở nền trước
Mặt nạ tin cậy ở nền trước cho phép bạn phân biệt chủ thể ở nền trước với nền sau.
Gọi enableForegroundConfidenceMask()
trong các tuỳ chọn cho phép bạn truy xuất mặt nạ nền trước bằng cách gọi getForegroundMask()
trên đối tượng SubjectSegmentationResult
được trả về sau khi xử lý hình ảnh.
Kotlin
val options = SubjectSegmenterOptions.Builder() .enableForegroundConfidenceMask() .build()
Java
SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() .enableForegroundConfidenceMask() .build();
Bitmap nền trước
Tương tự, bạn cũng có thể tạo bitmap của chủ thể ở nền trước.
Gọi enableForegroundBitmap()
trong các tuỳ chọn cho phép bạn truy xuất bitmap trên nền trước sau này bằng cách gọi getForegroundBitmap()
trên đối tượng SubjectSegmentationResult
được trả về sau khi xử lý hình ảnh.
Kotlin
val options = SubjectSegmenterOptions.Builder() .enableForegroundBitmap() .build()
Java
SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() .enableForegroundBitmap() .build();
Mặt nạ tin cậy nhiều chủ thể
Giống như đối với các tuỳ chọn ở nền trước, bạn có thể sử dụng SubjectResultOptions
để bật mặt nạ tin cậy cho từng đối tượng trên nền trước như sau:
Kotlin
val subjectResultOptions = SubjectSegmenterOptions.SubjectResultOptions.Builder() .enableConfidenceMask() .build() val options = SubjectSegmenterOptions.Builder() .enableMultipleSubjects(subjectResultOptions) .build()
Java
SubjectResultOptions subjectResultOptions = new SubjectSegmenterOptions.SubjectResultOptions.Builder() .enableConfidenceMask() .build() SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() .enableMultipleSubjects(subjectResultOptions) .build()
Bitmap nhiều chủ đề
Tương tự như vậy, bạn có thể bật bitmap cho từng đối tượng:
Kotlin
val subjectResultOptions = SubjectSegmenterOptions.SubjectResultOptions.Builder() .enableSubjectBitmap() .build() val options = SubjectSegmenterOptions.Builder() .enableMultipleSubjects(subjectResultOptions) .build()
Java
SubjectResultOptions subjectResultOptions = new SubjectSegmenterOptions.SubjectResultOptions.Builder() .enableSubjectBitmap() .build() SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() .enableMultipleSubjects(subjectResultOptions) .build()
Tạo trình phân đoạn đối tượng
Sau khi bạn chỉ định các tuỳ chọn SubjectSegmenterOptions
, hãy tạo một thực thể SubjectSegmenter
gọi getClient()
và truyền các tuỳ chọn đó dưới dạng một tham số:
Kotlin
val segmenter = SubjectSegmentation.getClient(options)
Java
SubjectSegmenter segmenter = SubjectSegmentation.getClient(options);
3. Xử lý hình ảnh
Truyền đối tượng InputImage
đã chuẩn bị vào phương thức process
của SubjectSegmenter
:
Kotlin
segmenter.process(inputImage) .addOnSuccessListener { result -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
Java
segmenter.process(inputImage) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(SubjectSegmentationResult result) { // Task completed successfully // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });
4. Nhận kết quả phân đoạn theo chủ thể
Truy xuất mặt nạ và bitmap trên nền trước
Sau khi xử lý xong, bạn có thể truy xuất mặt nạ trên nền trước cho hình ảnh gọi getForegroundConfidenceMask()
như sau:
Kotlin
val colors = IntArray(image.width * image.height) val foregroundMask = result.foregroundConfidenceMask for (i in 0 until image.width * image.height) { if (foregroundMask[i] > 0.5f) { colors[i] = Color.argb(128, 255, 0, 255) } } val bitmapMask = Bitmap.createBitmap( colors, image.width, image.height, Bitmap.Config.ARGB_8888 )
Java
int[] colors = new int[image.getWidth() * image.getHeight()]; FloatBuffer foregroundMask = result.getForegroundConfidenceMask(); for (int i = 0; i < image.getWidth() * image.getHeight(); i++) { if (foregroundMask.get() > 0.5f) { colors[i] = Color.argb(128, 255, 0, 255); } } Bitmap bitmapMask = Bitmap.createBitmap( colors, image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888 );
Bạn cũng có thể truy xuất bitmap nền trước của hình ảnh gọi getForegroundBitmap()
:
Kotlin
val foregroundBitmap = result.foregroundBitmap
Java
Bitmap foregroundBitmap = result.getForegroundBitmap();
Truy xuất mặt nạ và bitmap cho từng đối tượng
Tương tự, bạn có thể truy xuất mặt nạ cho các đối tượng được phân đoạn bằng cách gọi getConfidenceMask()
trên từng đối tượng như sau:
Kotlin
val subjects = result.subjects val colors = IntArray(image.width * image.height) for (subject in subjects) { val mask = subject.confidenceMask for (i in 0 until subject.width * subject.height) { val confidence = mask[i] if (confidence > 0.5f) { colors[image.width * (subject.startY - 1) + subject.startX] = Color.argb(128, 255, 0, 255) } } } val bitmapMask = Bitmap.createBitmap( colors, image.width, image.height, Bitmap.Config.ARGB_8888 )
Java
Listsubjects = result.getSubjects(); int[] colors = new int[image.getWidth() * image.getHeight()]; for (Subject subject : subjects) { FloatBuffer mask = subject.getConfidenceMask(); for (int i = 0; i < subject.getWidth() * subject.getHeight(); i++) { float confidence = mask.get(); if (confidence > 0.5f) { colors[width * (subject.getStartY() - 1) + subject.getStartX()] = Color.argb(128, 255, 0, 255); } } } Bitmap bitmapMask = Bitmap.createBitmap( colors, image.width, image.height, Bitmap.Config.ARGB_8888 );
Bạn cũng có thể truy cập bitmap của từng đối tượng được phân đoạn như sau:
Kotlin
val bitmaps = mutableListOf() for (subject in subjects) { bitmaps.add(subject.bitmap) }
Java
Listbitmaps = new ArrayList<>(); for (Subject subject : subjects) { bitmaps.add(subject.getBitmap()); }
Mẹo cải thiện hiệu suất
Đối với mỗi phiên ứng dụng, lần dự đoán đầu tiên thường chậm hơn so với các suy luận tiếp theo do khởi chạy mô hình. Nếu độ trễ thấp là rất quan trọng, hãy cân nhắc gọi trước suy luận "giả".
Chất lượng của kết quả tìm kiếm phụ thuộc vào chất lượng của hình ảnh đầu vào:
- Để Bộ công cụ học máy nhận được kết quả phân đoạn chính xác, hình ảnh phải có kích thước tối thiểu là 512x512 pixel.
- Tiêu điểm ảnh kém cũng có thể ảnh hưởng đến độ chính xác. Nếu bạn không nhận được kết quả chấp nhận được, hãy yêu cầu người dùng chụp lại hình ảnh.