我正在使用flutter软件包Firebase ML Vision
来检测我的应用中的人脸(加上轮廓)。大多数情况下,它运行良好。但是,如果给它提供一张光线不足,被头发遮住,戴着帽子,戴着眼镜等遮盖住的次佳脸的照片,那么它将连续处理图像,并且永远无法完成。
我正在寻找可能的解决方案。
我的代码(其中pickedImage
是File
,其中包含来自设备相机或设备库的图像):
Future _processImage() async {
//detector options
FaceDetectorOptions options = FaceDetectorOptions(
enableContours: true,
enableLandmarks: true,
mode: FaceDetectorMode.accurate);
//vison object
FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(pickedImage);
//Face detector object
FaceDetector faceDetector = FirebaseVision.instance.faceDetector(options);
List<Face> faces = await faceDetector.processImage(visionImage);
print('facedector completed');
}
如果图像质量好,则打印,如果图像完全随机(没有面孔),则打印,但是,如果图像是面孔,但有差异,则从不打印,仅在processImage()上等待。
我的解决方案是向processImage()添加一个超时,如下所示:
Future _processImage() async {
//detector options
FaceDetectorOptions options = FaceDetectorOptions(
enableContours: true,
enableLandmarks: true,
mode: FaceDetectorMode.accurate);
//vison object
FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(pickedImage);
//Face dector object
FaceDetector faceDetector = FirebaseVision.instance.faceDetector(options);
try {
//Get our faces from the image
List<Face> faces = await faceDetector
.processImage(visionImage)
.timeout(Duration(seconds: 30));
print('facedector completed');
} on TimeoutException catch (exception) {
print(exception.message);
faceDetector.close();
}
}
但是,此后如果我使用我知道可以使用的图像再次调用_processImage()
方法,它将继续进行永久分析,直到关闭并重新启动整个应用程序为止。我不太确定为什么会发生这种情况,但是如果有人知道如何解决此问题或进行任何形式的工作,我将非常感激。