搜索菜品入库改为自动拍照保存,取消手动拍照操作;增加向量数据库相关日志

This commit is contained in:
mazengfei
2026-03-03 15:39:11 +08:00
parent b034e9371f
commit 9a5258ad34
10 changed files with 157 additions and 95 deletions
@@ -12,6 +12,7 @@ import com.sw.inbound.MyApp
import com.sw.inbound.objbox.ObjectBox.boxStore
import com.sw.inbound.utils.AssetsTool
import com.sw.inbound.utils.ImageUtil
import com.sw.inbound.utils.LogSaveUtil
import com.sw.inbound.utils.ext.toJsonString
import io.objectbox.Box
import io.objectbox.kotlin.boxFor
@@ -82,6 +83,7 @@ object FoodModule {
return outputTensor.dataAsFloatArray
} catch (e: OutOfMemoryError) {
e.printStackTrace()
logInfo("bitmap2FloatArray异常:${e.message}")
} finally {
if (rgb565Bitmap != null && rgb565Bitmap.isRecycled.not()) {
rgb565Bitmap.recycle()
@@ -130,20 +132,20 @@ object FoodModule {
// query.close()
// }
Timber.tag("FoodModule").d("queryFood向量:${floatArray.slice(0 until 50).toJsonString()}")
logInfo("queryFood向量:${floatArray.slice(0 until 50).toJsonString()}")
val idScoreList = ObjectBox.query(floatArray, queryCount)
val nameScoreList = mutableListOf<IdNameScore>()
idScoreList.forEach {
val name = ObjectBox.get(it.id)?.name ?: ""
nameScoreList.add(IdNameScore(id = it.id, name = name, score = it.score))
}
Timber.tag("FoodModule").d("queryFood数据:${nameScoreList.toJsonString()}")
logInfo("queryFood数据:${nameScoreList.toJsonString()}")
return nameScoreList
}
suspend fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {
val floatArray = bitmap2FloatArray(bitmap, false) ?: return emptyList()
Timber.tag("FoodModule").d("向量:${floatArray.toJsonString()}")
logInfo("向量:${floatArray.toJsonString()}")
val nameScoreList = queryFoodNameScore(floatArray, queryCount)
if (nameScoreList.isEmpty()) {
return emptyList()
@@ -168,7 +170,7 @@ object FoodModule {
val sortedScoreList = maxScoreList.sortedWith(compareBy {
orderList.indexOf(it.name)
})
Timber.tag("FoodModule").d("getFoodScoreList数据:${sortedScoreList.toJsonString()}")
logInfo("getFoodScoreList数据:${sortedScoreList.toJsonString()}")
return sortedScoreList
}
@@ -211,7 +213,7 @@ object FoodModule {
val size = embeddingsList.size
val size2 = labelsList.size
val size3 = foodMap.size
Log.d("TAG", "initDefFoodData: $size,$size2,$size3")
logInfo("initDefFoodData: $size,$size2,$size3")
embeddingsList.forEachIndexed { index, floatList ->
val classIdx = labelsList[index]
val foodName = foodMap["$classIdx"]
@@ -227,7 +229,7 @@ object FoodModule {
action()
} catch (e: Exception) {
e.printStackTrace()
Log.d("TAG", "initDefFoodData: ---${e.message}--")
logInfo("initDefFoodData: ---${e.message}--")
}
}
@@ -299,7 +301,7 @@ object FoodModule {
return cacheFile.absolutePath
} catch (e: IOException) {
e.printStackTrace()
Timber.tag("FoodModule").d("文件拷贝失败 fileName=$fileName, error=${e.message}")
logInfo("文件拷贝失败 fileName=$fileName, error=${e.message}")
// 拷贝失败时删除残缺文件,避免下次读取到损坏文件
if (cacheFile.exists()) {
cacheFile.delete()
@@ -311,4 +313,9 @@ object FoodModule {
return null
}
private fun logInfo(msg:String) {
Timber.tag("FoodModule").d(msg)
LogSaveUtil.saveLogFile(msg)
}
}
@@ -21,6 +21,7 @@ import android.content.Context
import android.os.Environment
import android.util.Log
import com.sw.inbound.MyApp
import com.sw.inbound.utils.LogSaveUtil
import io.objectbox.Box
import io.objectbox.BoxStore
import io.objectbox.BoxStoreBuilder
@@ -86,8 +87,10 @@ object ObjectBox {
// Failed to build BoxStore due to database file issue, store message;
// checked in NoteListActivity to notify user.
dbExceptionMessage = e.toString()
LogSaveUtil.saveLogFile("ObjectBox.init异常1:$dbExceptionMessage")
return
} else {
LogSaveUtil.saveLogFile("ObjectBox.init异常2:${e.message}")
// Failed to build BoxStore due to developer error.
throw e
}
@@ -141,12 +144,14 @@ object ObjectBox {
} catch (e: FileCorruptException) {
// 操作中触发损坏,尝试重建数据库
e.printStackTrace()
LogSaveUtil.saveLogFile("safeDbOp出现FileCorruptException异常:${e.message},${e.errorCode}")
//val context = boxStore.context
//deleteDbFiles(context)
init(MyApp.instance!!)
null
} catch (e: Exception) {
e.printStackTrace()
LogSaveUtil.saveLogFile("safeDbOp出现其它异常:${e.message}")
null
}
}