-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPortraitModePlugin.kt
More file actions
89 lines (74 loc) · 3.04 KB
/
PortraitModePlugin.kt
File metadata and controls
89 lines (74 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package co.stonephone.stonecamera.plugins
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.media.Image
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import co.stonephone.stonecamera.MyApplication
import co.stonephone.stonecamera.StoneCameraViewModel
import co.stonephone.stonecamera.utils.ImageSegmentationUtils
import co.stonephone.stonecamera.utils.ImageSegmentationUtils.SegmentationListener
import kotlinx.coroutines.CompletableDeferred
import org.tensorflow.lite.task.vision.segmenter.Segmentation
class PortraitModePlugin: IPlugin, SegmentationListener {
override val id: String = "portraitModePlugin"
override val name: String = "Portrait Mode"
private lateinit var imageSegmentationUtils: ImageSegmentationUtils
private lateinit var bitmapBuffer: Bitmap
private var imageAnalyzer: ImageAnalysis? = null
override fun initialize(viewModel: StoneCameraViewModel) {
println("Test");
imageSegmentationUtils = ImageSegmentationUtils(
context = MyApplication.getAppContext(),
imageSegmentationListener = this
)
}
@RequiresApi(Build.VERSION_CODES.Q)
@SuppressLint("RestrictedApi")
override val onImageAnalysis: ((StoneCameraViewModel, ImageProxy, Image) -> CompletableDeferred<Unit>)? =
{ _: StoneCameraViewModel,
imageProxy: ImageProxy,
image: Image
->
println("Test")
Log.e("Test", "in here")
val deferred = CompletableDeferred<Unit>()
if (!::bitmapBuffer.isInitialized) {
// The image rotation and RGB image buffer are initialized only once
// the analyzer has started running
bitmapBuffer = Bitmap.createBitmap(
image.width,
image.height,
Bitmap.Config.ARGB_8888
)
}
segmentImage(imageProxy)
deferred
}
//TODO Image vs imageproxy
@RequiresApi(Build.VERSION_CODES.Q)
private fun segmentImage(image: ImageProxy) {
// Copy out RGB bits to the shared bitmap buffer
image.use { bitmapBuffer.copyPixelsFromBuffer(image.planes[0].buffer) }
val imageRotation = image.imageInfo.rotationDegrees
// Pass Bitmap and rotation to the image segmentation helper for processing and segmentation
imageSegmentationUtils.segment(bitmapBuffer, imageRotation)
}
override val settings: List<PluginSetting> = emptyList()
override fun onError(error: String) {
Log.e("Segmentation", "Failed segmentation");
}
override fun onResults(
results: List<Segmentation>?,
inferenceTime: Long,
imageHeight: Int,
imageWidth: Int
) {
Log.e("Segmentation", results.toString());
TODO("Not yet implemented")
//Segmentation has been complete. Can update UI to place segments on the screen.
}
}