Skip to content

Commit 15cc14e

Browse files
committed
Catch (and log) OpenCV Exceptions during recognition to avoid blocking the whole thread and freezing the app
1 parent 404bff5 commit 15cc14e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

FaceMaskDetection/src/main/java/com/softbankrobotics/facemaskdetection/FaceMaskDetection.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class FaceMaskDetection(private val detector: FaceMaskDetector, private val came
2828

2929
private val detectionScope = CoroutineScope(
3030
ThreadPoolExecutor(1, 1, 30, TimeUnit.SECONDS,
31-
ArrayBlockingQueue(1), ThreadPoolExecutor.DiscardOldestPolicy()).asCoroutineDispatcher())
31+
ArrayBlockingQueue(1), ThreadPoolExecutor.DiscardOldestPolicy()).asCoroutineDispatcher())
3232

3333
sealed class Message {
3434
class FaceMaskDetect(
@@ -44,7 +44,7 @@ class FaceMaskDetection(private val detector: FaceMaskDetector, private val came
4444
is Message.FaceMaskDetect -> {
4545
try {
4646
msg.detectedFaces.complete(detector.recognize(msg.picture))
47-
} catch (e: CvException) {
47+
} catch (e: Throwable) {
4848
Log.e(TAG, "Error processing picture of ${msg.picture.height} x ${msg.picture.width} sent at ${msg.sentTime}")
4949
msg.detectedFaces.completeExceptionally(e)
5050
}
@@ -67,6 +67,10 @@ class FaceMaskDetection(private val detector: FaceMaskDetector, private val came
6767
try {
6868
val faces = detectedFacesDeferred.await()
6969
onFaceDetected(faces)
70+
} catch (e:CvException) {
71+
Log.w(TAG, "OpenCV Exception during face processing (sometimes happens), ignoring: $e")
72+
} catch (e:Throwable) {
73+
Log.e(TAG, "Unexpected exception from face mask processing: $e")
7074
} finally {
7175
busy = false
7276
}

0 commit comments

Comments
 (0)