Uygulamanıza konu segmentasyonu özellikleri 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 | Boyut yaklaşık 200 KB artar. |
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ı inceleyin.
Başlamadan önce
- Proje düzeyindeki
build.gradle
dosyanızda, Google'ın Maven deposunu hembuildscript
hem deallprojects
bölümlerinize eklediğinizden emin olun. - ML Kit özne segmentasyonu kitaplığına ait bağımlılığı, modülünüzün uygulama düzeyindeki Gradle dosyasına ekleyin. Bu dosya genellikle
app/build.gradle
:
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 modelin kullanılabilirliğini açıkça kontrol edebilir ve Google Play Hizmetleri üzerinden indirme isteğinde bulunabilirsiniz.
Yükleme sırasında model indirmelerini etkinleştirmezseniz veya açık indirme isteğinde bulunmazsanız model, segmentleyiciyi ilk kez çalıştırdığınızda indirilir. İndirme tamamlanmadan önce gönderdiğiniz istekler sonuç vermez.
1. Giriş resmini hazırlama
Bir resimde segmentasyon yapmak için cihazdaki bir Bitmap
, media.Image
, ByteBuffer
, bayt dizisi veya dosyadan InputImage
nesnesi oluşturun.
Farklı kaynaklardan InputImage
nesnesi oluşturabilirsiniz. Bunları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 resim çekerken) media.Image
nesnesini ve resmin dönme açısını InputImage.fromMediaImage()
'e 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 // ... } } }
Resmin dönme derecesini gösteren bir kamera kitaplığı kullanmıyorsanız bunu cihazın dönme derecesinden ve cihazdaki kamera sensörünün yöneliminden 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
Dosya URI'sinden InputImage
nesnesi oluşturmak için uygulama bağlamını ve dosya URI'sini InputImage.fromFilePath()
'a iletin. Bu, kullanıcıdan galeri uygulamasından bir resim seçmesini istemek için ACTION_GET_CONTENT
intent'i kullandığınızda kullanışlıdır.
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
öğesinden InputImage
nesnesi oluşturmak için önce media.Image
girişi için daha önce açıklandığı gibi görüntünün döndürme derecesini hesaplayın.
Ardından, resmin yüksekliği, genişliği, renk kodlama biçimi ve döndürme derecesiyle birlikte arabelleği veya diziyi kullanarak 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
Bitmap
nesnesinden InputImage
nesnesi oluşturmak için aşağıdaki beyanı yapın:
Kotlin
val image = InputImage.fromBitmap(bitmap, 0)
Java
InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);
Resim, döndürme dereceleriyle birlikte bir Bitmap
nesnesi ile temsil edilir.
2. SubjectSegmenter örneği oluşturma
Segmentör 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üvenilirlik maskesi
Ön plan güven maskesi, ön plandaki nesneyi arka plandan ayırt etmenizi sağlar.
Seçenekler bölümünde enableForegroundConfidenceMask()
çağrısı, resmin işlenmesi sonrasında döndürülen SubjectSegmentationResult
nesnesinde getForegroundMask()
çağrısı yaparak daha sonra ön plan maskesini almanıza olanak tanır.
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şlemesini de alabilirsiniz.
Seçeneklerde enableForegroundBitmap()
çağrısı, resim işlendikten sonra döndürülen SubjectSegmentationResult
nesnesinde getForegroundBitmap()
çağrısı yaparak daha sonra ön plan bitmap'ini almanıza olanak tanır.
Kotlin
val options = SubjectSegmenterOptions.Builder() .enableForegroundBitmap() .build()
Java
SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder() .enableForegroundBitmap() .build();
Çok özneli güven maskesi
Ön plan seçeneklerinde olduğu gibi, her ön plan öznesi için güven maskesini etkinleştirmek üzere SubjectResultOptions
simgesini aşağıdaki gibi 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()
Çok özneli bit eşlemi
Benzer şekilde, her konu için bit eşlemesini 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()
Konu segmentleyiciyi oluşturma
SubjectSegmenterOptions
seçeneklerini belirttikten sonra getClient()
'yi çağıran ve seçenekleri parametre olarak ileten bir SubjectSegmenter
örneği oluşturun:
Kotlin
val segmenter = SubjectSegmentation.getClient(options)
Java
SubjectSegmenter segmenter = SubjectSegmentation.getClient(options);
3. Resim işleme
Hazırlanan InputImage
nesnesi, SubjectSegmenter
nesnesinin process
yöntemine iletilir:
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. Konu segmentasyonu sonucunu alma
Ön plan maskelerini ve bit eşlemelerini alma
İşleme tamamlandıktan sonra, resminizin ön plan maskesini almak için aşağıdaki gibi getForegroundConfidenceMask()
çağrısı yapabilirsiniz:
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 );
Ayrıca, getForegroundBitmap()
çağrısı yaparak resmin ön planının bit haritasını da 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 üzerinde getConfidenceMask()
'yi aşağıdaki gibi ç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 );
Her segmente ayrılmış öznenin bit eşlemesine 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ı iyileştirmeye yönelik ipuçları
Her uygulama oturumunda, modelin başlatılması nedeniyle ilk çıkarım genellikle sonraki çıkarımlardan daha yavaştır. Düşük gecikme kritik öneme sahipse önceden "sahte" bir çıkarım çağırabilirsiniz.
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.
- Resmin odaklanmaması da doğruluğu etkileyebilir. Kabul edilebilir sonuçlar alamazsanız kullanıcıdan resmi yeniden çekmesini isteyin.