优化
This commit is contained in:
@@ -11,6 +11,8 @@ import com.sw.dualscreen.utils.ImageUtil
|
||||
import com.sw.plate.utils.ToastUtils
|
||||
import io.objectbox.Box
|
||||
import io.objectbox.kotlin.boxFor
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.pytorch.IValue
|
||||
import org.pytorch.Module
|
||||
import org.pytorch.torchvision.TensorImageUtils
|
||||
@@ -25,8 +27,7 @@ object FoodModule {
|
||||
private const val THRESHOLD = 0.8
|
||||
|
||||
// private const val THRESHOLD = 0.0
|
||||
private var module: Module?=null
|
||||
private var box: Box<Food>?=null
|
||||
private var module: Module? = null
|
||||
|
||||
// private lateinit var embeddingsList: List<List<Float>>
|
||||
// private lateinit var labelsList: IntArray
|
||||
@@ -40,25 +41,21 @@ object FoodModule {
|
||||
private const val MODEL_INPUT_HEIGHT = 224
|
||||
|
||||
@SuppressLint("SuspiciousIndentation")
|
||||
fun init(context: Context, block: () -> Unit = {}) {
|
||||
Thread {
|
||||
suspend fun init(context: Context, block: () -> Unit = {}) {
|
||||
// Thread {}.start()
|
||||
withContext(Dispatchers.IO) {
|
||||
val modelPath = copyAssetToCache(context, "best_embedding_model_mobile.pt")
|
||||
// ToastUtils.showToast("modelPath==null:${modelPath == null}")
|
||||
module = Module.load(modelPath)
|
||||
// ToastUtils.showToast("module==null:${module == null}")
|
||||
box = ObjectBox.boxStore.boxFor(Food::class)
|
||||
//初始化默认重新拉取数据,先清空本地数据
|
||||
// ObjectBox.boxStore.runInTx {
|
||||
box?.run {
|
||||
if (all.isNotEmpty()) {
|
||||
removeAll()
|
||||
}
|
||||
val list = ObjectBox.getAll()
|
||||
if (list.isNotEmpty()) {
|
||||
ObjectBox.removeAll()
|
||||
}
|
||||
//if (box.all.isEmpty()) {
|
||||
// initDefFoodData(context)
|
||||
//}
|
||||
block()
|
||||
}.start()
|
||||
}
|
||||
}
|
||||
|
||||
// fun uri2FloatArray(uri: Uri): FloatArray? {
|
||||
@@ -125,24 +122,21 @@ object FoodModule {
|
||||
// return queryFoodNameScore(floatArray, queryCount)
|
||||
// }
|
||||
|
||||
fun queryFoodNameScore(floatArray: FloatArray?, queryCount: Int = 15): List<IdNameScore> {
|
||||
suspend fun queryFoodNameScore(
|
||||
floatArray: FloatArray?,
|
||||
queryCount: Int = 15
|
||||
): List<IdNameScore> {
|
||||
if (floatArray == null) return emptyList()
|
||||
val startTime = System.currentTimeMillis()
|
||||
Timber.tag(TAG)
|
||||
.d("queryFoodNameScore-已采集向量总数:${box?.all?.filter { it.isDel.not() }?.size}")
|
||||
//box.store.runInTx { }
|
||||
val query = box?.query()
|
||||
?.equal(Food_.isDel, false)
|
||||
?.and()
|
||||
?.nearestNeighbors(Food_.foodVector, floatArray, queryCount)
|
||||
?.build()
|
||||
val size = ObjectBox.getAll().filter { it.isDel.not() }.size
|
||||
Timber.tag(TAG).d("queryFoodNameScore-已采集向量总数:${size}")
|
||||
//查询比较分数
|
||||
// val tempList = query.findWithScores().sortedBy { it.score }.map { "${it.get().name}|${it.get().foodIdx}|${it.score}" }
|
||||
// val idScoreList = query.findIdsWithScores()
|
||||
val objScoreList = query?.findWithScores()
|
||||
val objScoreList = ObjectBox.query(floatArray, queryCount)
|
||||
Timber.tag(TAG).d("idScoreList:${GsonUtils.toJson(objScoreList)}")
|
||||
val nameScoreList = mutableListOf<IdNameScore>()
|
||||
objScoreList?.forEach {
|
||||
objScoreList.forEach {
|
||||
val food = it.get()
|
||||
nameScoreList.add(
|
||||
IdNameScore(
|
||||
@@ -153,13 +147,11 @@ object FoodModule {
|
||||
)
|
||||
}
|
||||
val nameScoreData = GsonUtils.toJson(nameScoreList)
|
||||
Timber.tag(TAG)
|
||||
.d("queryFood,耗时:${System.currentTimeMillis() - startTime},数据:$nameScoreData")
|
||||
query?.close()
|
||||
Timber.tag(TAG).d("queryFood,耗时:${System.currentTimeMillis() - startTime},数据:$nameScoreData")
|
||||
return nameScoreList
|
||||
}
|
||||
|
||||
fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {
|
||||
suspend fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {
|
||||
val startTime = System.currentTimeMillis()
|
||||
val floatArray = bitmap2FloatArray(bitmap, false)
|
||||
Timber.tag(TAG).d("bitmap2FloatArray,耗时:${System.currentTimeMillis() - startTime}")
|
||||
|
||||
Reference in New Issue
Block a user