refactor: use SharedDepthEstimator for depth estimation tasks
This commit is contained in:
1 parent
cb78ba052d
commit
082c542878
6 files changed
+73
-32
No files matched your search
@@ -205,7 +205,7 @@ class LutManager(private val context: Context) {
|
||||
|
||||
// 查找 LUT 信息
|
||||
val lutInfo = getLutInfo(id) ?: run {
|
||||
PLog.w(TAG, "LUT not found: $id")
|
||||
// PLog.w(TAG, "LUT not found: $id")
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.nio.ByteOrder
|
||||
|
||||
class DepthEstimator(
|
||||
context: Context,
|
||||
private val modelAssetName: String = MODEL_MIDAS
|
||||
val modelAssetName: String = MODEL_MIDAS
|
||||
) {
|
||||
private var interpreter: Interpreter? = null
|
||||
private var gpuDelegate: GpuDelegate? = null
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.hinnka.mycamera.ml
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
object SharedDepthEstimator {
|
||||
private val mutex = Mutex()
|
||||
@Volatile
|
||||
private var estimator: DepthEstimator? = null
|
||||
|
||||
suspend fun prewarm(context: Context, modelAssetName: String = DepthEstimator.MODEL_MIDAS) {
|
||||
withEstimator(context, modelAssetName) { }
|
||||
}
|
||||
|
||||
suspend fun estimateDepth(
|
||||
context: Context,
|
||||
inputBitmap: Bitmap,
|
||||
modelAssetName: String = DepthEstimator.MODEL_MIDAS
|
||||
): Bitmap? {
|
||||
return withEstimator(context, modelAssetName) { estimator ->
|
||||
estimator.estimateDepth(inputBitmap)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun <T> withEstimator(
|
||||
context: Context,
|
||||
modelAssetName: String,
|
||||
block: (DepthEstimator) -> T
|
||||
): T = withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
val current = estimator
|
||||
val resolved = if (current?.modelAssetName == modelAssetName) {
|
||||
current
|
||||
} else {
|
||||
current?.close()
|
||||
DepthEstimator(context.applicationContext, modelAssetName).also {
|
||||
estimator = it
|
||||
}
|
||||
}
|
||||
block(resolved)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun close() {
|
||||
withContext(Dispatchers.Default) {
|
||||
mutex.withLock {
|
||||
estimator?.close()
|
||||
estimator = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.hinnka.mycamera.preview
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import com.hinnka.mycamera.ml.DepthEstimator
|
||||
import com.hinnka.mycamera.ml.SharedDepthEstimator
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import com.hinnka.mycamera.utils.StartupTrace
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -22,8 +22,8 @@ class PreviewDepthProcessor(private val context: Context) {
|
||||
}
|
||||
|
||||
private val scope = CoroutineScope(Dispatchers.Default + Job())
|
||||
private var depthEstimator: DepthEstimator? = null
|
||||
private var isPrewarming = false
|
||||
private var isPrewarmed = false
|
||||
private var isProcessing = false
|
||||
|
||||
// Expose the latest depth map as a StateFlow for the UI or GL pipeline
|
||||
@@ -31,14 +31,15 @@ class PreviewDepthProcessor(private val context: Context) {
|
||||
val latestDepthMap = _latestDepthMap.asStateFlow()
|
||||
|
||||
fun prewarm() {
|
||||
if (depthEstimator != null || isPrewarming) {
|
||||
if (isPrewarmed || isPrewarming) {
|
||||
return
|
||||
}
|
||||
isPrewarming = true
|
||||
scope.launch {
|
||||
try {
|
||||
StartupTrace.mark("PreviewDepthProcessor.prewarm start")
|
||||
depthEstimator = DepthEstimator(context)
|
||||
SharedDepthEstimator.prewarm(context)
|
||||
isPrewarmed = true
|
||||
StartupTrace.mark("PreviewDepthProcessor.prewarm end")
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to prewarm depth estimator", e)
|
||||
@@ -57,8 +58,7 @@ class PreviewDepthProcessor(private val context: Context) {
|
||||
return
|
||||
}
|
||||
|
||||
val estimator = depthEstimator
|
||||
if (estimator == null) {
|
||||
if (!isPrewarmed) {
|
||||
prewarm()
|
||||
return
|
||||
}
|
||||
@@ -68,7 +68,7 @@ class PreviewDepthProcessor(private val context: Context) {
|
||||
// Process on background thread
|
||||
scope.launch {
|
||||
try {
|
||||
val depthMap = estimator.estimateDepth(bitmap)
|
||||
val depthMap = SharedDepthEstimator.estimateDepth(context, bitmap)
|
||||
_latestDepthMap.value = depthMap
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Error processing preview bitmap for depth", e)
|
||||
@@ -79,7 +79,6 @@ class PreviewDepthProcessor(private val context: Context) {
|
||||
}
|
||||
|
||||
fun release() {
|
||||
depthEstimator?.close()
|
||||
depthEstimator = null
|
||||
isPrewarmed = false
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import com.hinnka.mycamera.gallery.GalleryManager
|
||||
import com.hinnka.mycamera.ml.DepthEstimator
|
||||
import com.hinnka.mycamera.ml.SharedDepthEstimator
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
@@ -21,10 +21,6 @@ class DepthBokehProcessor(context: Context) {
|
||||
|
||||
private val appContext = context.applicationContext
|
||||
private val processor = OglBokehProcessor()
|
||||
private val depthEstimator by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
// DepthEstimator(appContext, DepthEstimator.MODEL_DEPTH_ANYTHING)
|
||||
DepthEstimator(appContext, DepthEstimator.MODEL_MIDAS)
|
||||
}
|
||||
private val mutex = Mutex()
|
||||
|
||||
/**
|
||||
@@ -59,7 +55,7 @@ class DepthBokehProcessor(context: Context) {
|
||||
val inputForBokeh = ensureArgb8888(originalImage)
|
||||
|
||||
if (depthMap == null) {
|
||||
depthMap = depthEstimator.estimateDepth(inputForBokeh)
|
||||
depthMap = SharedDepthEstimator.estimateDepth(appContext, inputForBokeh)
|
||||
|
||||
if (depthMap != null && depthFile != null) {
|
||||
try {
|
||||
@@ -111,7 +107,5 @@ class DepthBokehProcessor(context: Context) {
|
||||
return converted ?: bitmap
|
||||
}
|
||||
|
||||
fun close() {
|
||||
depthEstimator.close()
|
||||
}
|
||||
fun close() = Unit
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import android.media.Image
|
||||
import android.opengl.*
|
||||
import com.hinnka.mycamera.data.ContentRepository
|
||||
import com.hinnka.mycamera.camera.AspectRatio
|
||||
import com.hinnka.mycamera.ml.DepthEstimator
|
||||
import com.hinnka.mycamera.ml.SharedDepthEstimator
|
||||
import com.hinnka.mycamera.utils.BitmapUtils
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import com.hinnka.mycamera.utils.RawProcessor
|
||||
@@ -201,17 +201,9 @@ class RawDemosaicProcessor {
|
||||
private var gfHeight = 0
|
||||
private var gfChromaTexId = 0
|
||||
|
||||
private var depthEstimator: DepthEstimator? = null
|
||||
|
||||
private fun getDepthEstimator(context: Context): DepthEstimator {
|
||||
return depthEstimator ?: synchronized(this) {
|
||||
depthEstimator ?: DepthEstimator(context.applicationContext).also { depthEstimator = it }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun prewarmDepthEstimator(context: Context) = withContext(Dispatchers.Default) {
|
||||
val start = System.currentTimeMillis()
|
||||
getDepthEstimator(context.applicationContext)
|
||||
SharedDepthEstimator.prewarm(context.applicationContext)
|
||||
PLog.d(TAG, "RAW DepthEstimator prewarmed, took=${System.currentTimeMillis() - start}ms")
|
||||
}
|
||||
|
||||
@@ -2132,7 +2124,7 @@ class RawDemosaicProcessor {
|
||||
drawQuad(linearProgram)
|
||||
}
|
||||
|
||||
private fun resolveRawAutoExposureEv(
|
||||
private suspend fun resolveRawAutoExposureEv(
|
||||
context: Context,
|
||||
metadata: RawMetadata,
|
||||
sourceTextureId: Int,
|
||||
@@ -2170,7 +2162,7 @@ class RawDemosaicProcessor {
|
||||
bitmap.copyPixelsFromBuffer(buffer)
|
||||
buffer.position(0) // Reset for MeteringSystem
|
||||
|
||||
val depthMap = getDepthEstimator(context).estimateDepth(bitmap)
|
||||
val depthMap = SharedDepthEstimator.estimateDepth(context, bitmap)
|
||||
val weightBuffer = depthMap?.let {
|
||||
val wb = ByteBuffer.allocateDirect(it.byteCount)
|
||||
it.copyPixelsToBuffer(wb)
|
||||
|
||||
Reference in new issue
Block a user