识别图片处理、查询菜品数据

This commit is contained in:
2025-10-17 14:47:28 +08:00
parent dd51448aea
commit a27cc5f7ee
10 changed files with 221 additions and 31 deletions
@@ -29,15 +29,15 @@ object FoodModule {
fun init(context: Context) {
module_mobile = Module.load(copyAssetToCache(context, "best_embedding_model_mobile.pt"))
box = ObjectBox.boxStore.boxFor(Food::class)
if (box.all.isNotEmpty()) {
box.removeAll()
}
// if (box.all.isNotEmpty()) {
// box.removeAll()
// }
if (box.all.isEmpty()) {
initFoodData(context)
}
}
fun queryFood(bitmap: Bitmap, queryCount:Int = 30): MutableList<FoodQueryResult> {
fun queryFood(bitmap: Bitmap, queryCount:Int = 15): List<String> {
val inputTensor = TensorImageUtils.bitmapToFloat32Tensor(
bitmap,
NO_MEAN_RGB, // [0.485, 0.456, 0.406] TORCHVISION_NORM_MEAN_RGB
@@ -48,23 +48,21 @@ object FoodModule {
return queryFood(floatArray,queryCount)
}
fun queryFood(floatArray: FloatArray, queryCount:Int = 30): MutableList<FoodQueryResult> {
fun queryFood(floatArray: FloatArray, queryCount:Int = 15): List<String> {
val query: Query<Food> = box.query(Food_.foodVector.nearestNeighbors(floatArray, queryCount)).build()
//查询比较分数
// val tempList = query.findWithScores().sortedBy { it.score }.map { "${it.get().name}|${it.get().foodIdx}|${it.score}" }
val resultList = mutableListOf<FoodQueryResult>()
val map = mutableMapOf<String, Int>()
query.findIdsWithScores().forEach {
val food = box.get(it.id)
resultList.add(
FoodQueryResult(
id = it.id,
name = food.name,
foodIdx = food.foodIdx,
score = it.score
)
)
//FoodQueryResult(id = it.id, name = food.name, foodIdx = food.foodIdx, score = it.score)
food.name?.let { key ->
val count = map[key] ?: 0
map.put(key, count + 1)
}
}
return resultList
val list = map.entries.sortedByDescending { it.value }.map { it.key }
return list
}