perf(FoodModule): 提高食物查询性能并优化排序逻辑

- 将默认查询数量从15增加到50
- 移除不必要的分数阈值过滤条件
- 移除复杂的按名称分组和计数逻辑
- 简化排序算法,直接按分数排序
- 添加调试日志输出结果数据
This commit is contained in:
2026-04-13 17:20:51 +08:00
parent dde0bbc4d5
commit 7f455f2d8f
@@ -124,7 +124,7 @@ object FoodModule {
suspend fun queryFoodNameScore( suspend fun queryFoodNameScore(
floatArray: FloatArray?, floatArray: FloatArray?,
queryCount: Int = 15 queryCount: Int = 50
): List<IdNameScore> { ): List<IdNameScore> {
if (floatArray == null) return emptyList() if (floatArray == null) return emptyList()
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
@@ -151,7 +151,7 @@ object FoodModule {
return nameScoreList return nameScoreList
} }
suspend fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> { suspend fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 50): List<IdNameScore> {
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
val floatArray = bitmap2FloatArray(bitmap, false) val floatArray = bitmap2FloatArray(bitmap, false)
Timber.tag(TAG).d("bitmap2FloatArray,耗时:${System.currentTimeMillis() - startTime}") Timber.tag(TAG).d("bitmap2FloatArray,耗时:${System.currentTimeMillis() - startTime}")
@@ -161,28 +161,26 @@ object FoodModule {
return emptyList() return emptyList()
} }
val maxScoreList = nameScoreList val maxScoreList = nameScoreList
.filter { it.score < 1 - THRESHOLD } // .filter { it.score < 1 - THRESHOLD }
.groupBy { it.name } .groupBy { it.name }
.map { (_, value) -> value.minByOrNull { it.score }!! } .map { (_, value) -> value.minByOrNull { it.score }!! }
.toMutableList() .toMutableList()
val map = mutableMapOf<String, Int>() // val map = mutableMapOf<String, Int>()
nameScoreList.forEach { // nameScoreList.forEach {
val key = it.name // val key = it.name
val count = map[key] ?: 0 // val count = map[key] ?: 0
map[key] = count + 1 // map[key] = count + 1
} // }
val orderList = map.entries.sortedByDescending { it.value }.map { it.key }.toMutableList() // val orderList = map.entries.sortedByDescending { it.value }.map { it.key }.toMutableList()
val firstFood = nameScoreList[0].name // val firstFood = nameScoreList[0].name
orderList.remove(firstFood) // orderList.remove(firstFood)
orderList.add(0, firstFood) // orderList.add(0, firstFood)
//
val sortedScoreList = maxScoreList.sortedWith(compareBy { // val sortedScoreList = maxScoreList.sortedWith(compareBy {
orderList.indexOf(it.name) // orderList.indexOf(it.name)
}) // })
if (bitmap.isRecycled.not()) { val sortedScoreList = maxScoreList.sortedBy { it.score }
bitmap.recycle() Timber.tag("FoodModule").d("getFoodScoreList数据:${sortedScoreList.toString()}")
}
//Timber.tag("FoodModule").d("getFoodScoreList数据:${sortedScoreList.toJsonString()}")
return sortedScoreList return sortedScoreList
} }