openIMAJ面部检测 - 未捕获IOException

时间:2015-02-18 22:23:56

标签: java exception-handling face-detection openimaj

我有这种方法(简化)用于检测图像(URL)上的面(计数):

private int processImage(String urlString) {
    InputStream is = null;
    URLConnection resource;
    int facesCount = 0;
    try {
        resource = new URL(urlString).openConnection();
        resource.setConnectTimeout(200);
        resource.setReadTimeout(1000);
        resource.setRequestProperty("User-Agent", "Mozilla/5.0");

        String type = resource.getHeaderField("Content-Type");
        if (!type.startsWith("image/")) {
            throw new IOException("Not an image (Content-Type:" + type + ")");
        }
        is = resource.getInputStream();
        MBFImage mbfimage = ImageUtilities.readMBF(is);
        facesCount = faceDetector.detectFaces(Transforms.calculateIntensity(mbfimage)).size();
        is.close();
    } catch (IOException e) {
        System.out.println("oops");
    }
    return facesCount;
}

效果很好,但如果图像有点损坏(例如this test image),我会收到此错误:

  

错误:无法解码类型的图像:发生在:   com.sun.media.jai.opimage.CodecRIFUtil java.io.IOException:Source   流不支持向后搜索。在   com.sun.media.jai.codecimpl.CodecUtils.toIOException(CodecUtils.java:76)     在   com.sun.media.jai.codecimpl.FPXImageDecoder.decodeAsRenderedImage(FPXImageDecoder.java:40)     在   com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:88)     在com.sun.media.jai.opimage.FPXRIF.create(FPXRIF.java:46)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)at   javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)     在   javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)     在javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)     在com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:102)at at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)at   javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)     在   javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)     在javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)     在javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)at   javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)at at   javax.media.jai.RenderedOp.getColorModel(RenderedOp.java:2242)at at   javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2498)     在   javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2546)     在org.openimaj.image.ExtendedImageIO.read(ExtendedImageIO.java:162)     在org.openimaj.image.ImageUtilities.readMBF(ImageUtilities.java:273)     at image.ImageThread.processImage(ImageThread.java:233)at   image.ImageThread.main(ImageThread.java:255)引起:   java.lang.IllegalArgumentException:源流不支持   寻求倒退。在   com.sun.media.jai.codec.SegmentedSeekableStream。(SegmentedSeekableStream.java:200)     在   com.sun.media.jai.codec.SegmentedSeekableStream。(SegmentedSeekableStream.java:262)     在   com.sun.media.jai.codecimpl.fpx.StructuredStorage.getFat(StructuredStorage.java:238)     在   com.sun.media.jai.codecimpl.fpx.StructuredStorage。(StructuredStorage.java:131)     在com.sun.media.jai.codecimpl.fpx.FPXImage。(FPXImage.java:110)     在   com.sun.media.jai.codecimpl.FPXImageDecoder.decodeAsRenderedImage(FPXImageDecoder.java:38)     ......还有28个

程序挂起。我想抓住那个例外,记录并继续。

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。我使用的是旧版本的openIMAJ(1.1.3)。最新版本是1.3.1,效果很好。我没有注意到,因为我忽略了版本号。