我正在尝试在后台处理图像以检测来自相机图像的对象,但是当我尝试在后台协程中处理它们时,它会出错。
java.lang.IllegalStateException: Error occurred when detecting the image: Invoke() failed.
at org.tensorflow.lite.task.vision.detector.ObjectDetector.detectNative(Native Method) at org.tensorflow.lite.task.vision.detector.ObjectDetector.detect(ObjectDetector.java:528)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.access$1200(ObjectDetector.java:88)
at org.tensorflow.lite.task.vision.detector.ObjectDetector$4.run(ObjectDetector.java:481)
at org.tensorflow.lite.task.vision.detector.ObjectDetector$4.run(ObjectDetector.java:477)
at org.tensorflow.lite.task.vision.core.BaseVisionTaskApi.run(BaseVisionTaskApi.java:49)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.detect(ObjectDetector.java:476)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.detect(ObjectDetector.java:446)
at com.nst.n692_android.ObjectDetectorHelper$detectAsync$2.invokeSuspend(ObjectDetectorHelper.kt:167)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@7613710, Dispatchers.Main.immediate]
我的代码是这样的:
return withContext(Dispatchers.IO){
// Preprocess the image and convert it into a TensorImage for detection.
val inputImage = TensorImage.fromBitmap(rotatedImage)
val outputs = objectDetector?.detect(inputImage)
if (outputs.isNullOrEmpty())
return@withContext null
val detectionResult = outputs[0]
val category = detectionResult?.boundingBox ?: return@withContext null
val cordLeft =
PointF(category.left, category.top)
val cordRight =
PointF(category.right, category.top)
inferenceTime = SystemClock.uptimeMillis() - inferenceTime
val rotatedCoordinatesLeft =
cordLeft.rotateCoordinates(rotatedImage.width, rotatedImage.height, imageRotation)
val rotatedCoordinatesRight =
cordRight.rotateCoordinates(rotatedImage.width, rotatedImage.height, imageRotation)
return@withContext DetectedObjectResult(
detectionResult.categories[0].score.toString(),
rotatedCoordinatesLeft,
rotatedCoordinatesRight
)
}