Uygulamanıza özne segmentasyonu özelliklerini kolayca eklemek için ML Kit'i kullanın.
| Özellik | Ayrıntılar |
|---|---|
| SDK adı | play-services-mlkit-subject-segmentation |
| Uygulama | Paketlenmemiş: Model, Google Play Hizmetleri kullanılarak dinamik olarak indirilir. |
| Uygulama boyutunun etkisi | ~200 KB boyut artışı. |
| Başlatma süresi | Kullanıcıların ilk kullanımdan önce modelin indirilmesini beklemesi gerekebilir. |
Deneyin
- Bu API'nin örnek kullanımını görmek için örnek uygulamayı deneyin.
Başlamadan önce
- Proje düzeyindeki
build.gradledosyanızda, Google'ın Maven deposunu hembuildscripthem deallprojectsbölümlerinize eklediğinizden emin olun. - ML Kit Subject Segmentation kitaplığının bağımlılığını, modülünüzün uygulama düzeyindeki Gradle dosyasına (genellikle
app/build.gradle) ekleyin:
dependencies {
implementation 'com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1'
}
Yukarıda belirtildiği gibi, model Google Play Hizmetleri tarafından sağlanır.
Uygulamanızı, Play Store'dan yüklendikten sonra modeli cihaza otomatik olarak indirecek şekilde yapılandırabilirsiniz. Bunun için uygulamanızın AndroidManifest.xml dosyasına aşağıdaki beyanı ekleyin:
<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>
Ayrıca, ModuleInstallClient API ile Google Play Hizmetleri üzerinden modelin kullanılabilirliğini açıkça kontrol edebilir ve indirme isteğinde bulunabilirsiniz.
Yükleme sırasında model indirmelerini etkinleştirmez veya açık indirme isteğinde bulunmazsanız model, segmenter'ı ilk kez çalıştırdığınızda indirilir. İndirme işlemi tamamlanmadan önce yaptığınız istekler sonuç vermez.
1. Giriş görüntüsünü hazırlama
Bir resimde segmentasyon gerçekleştirmek için Bitmap, media.Image, ByteBuffer, bayt dizisi veya cihazdaki bir dosyadan InputImage nesnesi oluşturun.
Farklı kaynaklardan InputImage nesnesi oluşturabilirsiniz. Bu kaynakların her biri aşağıda açıklanmıştır.
media.Image kullanma
Bir media.Image nesnesinden InputImage nesnesi oluşturmak için (ör. bir cihazın kamerasından görüntü yakaladığınızda) media.Image nesnesini ve görüntünün dönüşünü InputImage.fromMediaImage() değerine iletin.
CameraX kitaplığını kullanıyorsanız OnImageCapturedListener ve ImageAnalysis.Analyzer sınıfları, rotasyon değerini sizin için hesaplar.
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 // ... } } }
Görüntünün dönüş derecesini veren bir kamera kitaplığı kullanmıyorsanız bu değeri cihazın dönüş derecesinden ve cihazdaki kamera sensörünün yönünden hesaplayabilirsiniz:
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; }
Ardından media.Image nesnesini ve dönüş derecesi değerini InputImage.fromMediaImage()'e iletin:
Kotlin
val image = InputImage.fromMediaImage(mediaImage, rotation)
Java
InputImage image = InputImage.fromMediaImage(mediaImage, rotation);
Dosya URI'si kullanma
Bir dosya URI'sinden InputImage nesnesi oluşturmak için uygulama bağlamını ve dosya URI'sini InputImage.fromFilePath() yöntemine iletin. Bu, kullanıcının galeri uygulamasından bir resim seçmesini istemek için ACTION_GET_CONTENT intent'ini kullandığınızda yararlı olur.
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(); }
ByteBuffer veya ByteArray kullanma
ByteBuffer veya ByteArray nesnesinden InputImage nesnesi oluşturmak için önce media.Image girişi için daha önce açıklandığı şekilde resim döndürme derecesini hesaplayın.
Ardından, arabellek veya diziyle birlikte resmin yüksekliği, genişliği, renk kodlama biçimi ve dönüş derecesini içeren InputImage nesnesini oluşturun:
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 );
Bitmap kullanma
Bir Bitmap nesnesinden InputImage nesnesi oluşturmak için aşağıdaki bildirimi yapın:
Kotlin
val image = InputImage.fromBitmap(bitmap, 0)
Java
InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);
Resim, dönüş dereceleriyle birlikte bir Bitmap nesnesiyle temsil edilir.
2. SubjectSegmenter örneği oluşturma
Segmenter seçeneklerini tanımlama
Resminizi segmentlere ayırmak için önce aşağıdaki gibi bir SubjectSegmenterOptions örneği oluşturun:
Kotlin
val options = SubjectSegmenterOptions.Builder()
// enable options
.build()Java
SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
// enable options
.build();Her bir seçeneğin ayrıntıları aşağıda verilmiştir:
Ön plan güven maskesi
Ön plan güven maskesi, ön plandaki özneyi arka plandan ayırt etmenizi sağlar.
Seçeneklerdeki enableForegroundConfidenceMask() çağrısı, resmi işledikten sonra döndürülen SubjectSegmentationResult nesnesinde getForegroundMask() çağrısı yaparak ön plan maskesini daha sonra almanızı sağlar.
Kotlin
val options = SubjectSegmenterOptions.Builder() .enableForegroundConfidenceMask() .build()
Java
SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() .enableForegroundConfidenceMask() .build();
Ön plan bit eşlemi
Benzer şekilde, ön plandaki öznenin bit eşlemini de alabilirsiniz.
Seçeneklerdeki enableForegroundBitmap() çağrısı, daha sonra resim işlendikten sonra döndürülen SubjectSegmentationResult nesnesinde getForegroundBitmap() çağrısı yaparak ön plan bit eşlemini almanıza olanak tanır.
Kotlin
val options = SubjectSegmenterOptions.Builder() .enableForegroundBitmap() .build()
Java
SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() .enableForegroundBitmap() .build();
Çoklu özne güven maskesi
Ön plan seçeneklerinde olduğu gibi, her ön plan öznesi için güven maskesini etkinleştirmek üzere SubjectResultOptions kullanabilirsiniz:
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()Çoklu özne bit eşlemi
Benzer şekilde, her konu için bit eşlemi etkinleştirebilirsiniz:
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()Özne segmentasyon aracını oluşturma
SubjectSegmenterOptions seçeneklerini belirttikten sonra getClient() yöntemini çağırıp seçenekleri parametre olarak ileterek bir SubjectSegmenter örneği oluşturun:
Kotlin
val segmenter = SubjectSegmentation.getClient(options)
Java
SubjectSegmenter segmenter = SubjectSegmentation.getClient(options);
3. Görüntü işleme
Hazırlanan InputImage nesnesini SubjectSegmenter sınıfının process yöntemine iletin:
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. Özne segmentasyonu sonucunu alma
Ön plan maskelerini ve bit eşlemlerini alma
İşlendikten sonra, resim çağrınız için ön plan maskesini getForegroundConfidenceMask() şu şekilde alabilirsiniz:
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 );
getForegroundBitmap() yöntemini çağırarak resmin ön planının bit eşlemini de alabilirsiniz:
Kotlin
val foregroundBitmap = result.foregroundBitmap
Java
Bitmap foregroundBitmap = result.getForegroundBitmap();
Her özne için maskeleri ve bit eşlemleri alma
Benzer şekilde, her bir özne için getConfidenceMask() yöntemini çağırarak segmentlere ayrılmış öznelerin maskesini alabilirsiniz:
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 );
Segmentlere ayrılmış her bir öznenin bit eşlemine aşağıdaki şekilde de erişebilirsiniz:
Kotlin
val bitmaps = mutableListOf() for (subject in subjects) { bitmaps.add(subject.bitmap) }
Java
Listbitmaps = new ArrayList<>(); for (Subject subject : subjects) { bitmaps.add(subject.getBitmap()); }
Performansı artırmaya yönelik ipuçları
Her uygulama oturumunda, model başlatma nedeniyle ilk çıkarım genellikle sonraki çıkarımlardan daha yavaştır. Düşük gecikme süresi kritikse önceden "sahte" bir çıkarım çağrısı yapabilirsiniz.
Sonuçlarınızın kalitesi, giriş resminin kalitesine bağlıdır:
- ML Kit'in doğru bir segmentasyon sonucu elde edebilmesi için resmin en az 512x512 piksel olması gerekir.
- Resim odağının kötü olması da doğruluğu etkileyebilir. Kabul edilebilir sonuçlar alamazsanız kullanıcıdan resmi yeniden çekmesini isteyin.