gocv icon indicating copy to clipboard operation
gocv copied to clipboard

Better error handling

Open zak905 opened this issue 5 years ago • 5 comments

It seems like a CVError or cv::Exception shuts down the program. This is inconvenient if gocv is run in a web server because the server is unable to handle other requests.

For example, gocv.AddWeighted(decodedImage1, request.Alpha, decodedImage2, 1-request.Alpha, 0.0, &result) will cause the program to shut down if images are not of the same size

is there a way to implement better error handling ? I can contribute.

zak905 avatar Aug 18 '18 10:08 zak905

Please see this previous issue https://github.com/hybridgroup/gocv/issues/186 related to Go issue https://github.com/golang/go/issues/13672 discussing. Seems like Windows exception handling is the obstacle here.

deadprogram avatar Aug 22 '18 09:08 deadprogram

Can this be a potential solution ? https://artem.krylysov.com/blog/2017/04/13/handling-cpp-exceptions-in-go/

zak905 avatar Aug 28 '18 10:08 zak905

Hello @zak905 this does not work on Windows, which is why we have not added to GoCV so far.

deadprogram avatar Aug 29 '18 18:08 deadprogram

It definitely needs a solution, even if it doesn't work on Windows.

sknick avatar Aug 17 '20 15:08 sknick

I found a solution on Windows that works with mingw. Any suggestions on how to incorporate this to the entire library?

#include <windows.h>

LONG WINAPI
VectoredHandlerSkip(
    struct _EXCEPTION_POINTERS *ExceptionInfo
    )
{
    PCONTEXT Context;

    Context = ExceptionInfo->ContextRecord;
#ifdef _AMD64_
    Context->Rip++;
#else
    Context->Eip++;
#endif
    return EXCEPTION_EXECUTE_HANDLER;
}

double FindTransformECC(Mat templateImage, Mat inputImage, Mat warpMatrix, int motionType, TermCriteria criteria, Mat inputMask, int gaussFiltSize){
    auto handle = AddVectoredExceptionHandler(1, VectoredHandlerSkip);
    double res;
    try {
        res = cv::findTransformECC(*templateImage, *inputImage, *warpMatrix, motionType, *criteria, *inputMask, gaussFiltSize);
    } catch(std::exception &e) {
        res = -1;
    }
    RemoveVectoredExceptionHandler(handle);
    return res;
}

fumin avatar Apr 10 '22 15:04 fumin