Exemple #1
0
void delete_ocr (void** arg)
{
	struct ocrCtx* ctx = *arg;
        TessBaseAPIEnd(ctx->api);
        TessBaseAPIDelete(ctx->api);
	freep(arg);
}
Exemple #2
0
void _dinit_hardsubx(struct lib_hardsubx_ctx **ctx)
{
	struct lib_hardsubx_ctx *lctx = *ctx;
	// Free all memory allocated to everything in the context

	// Free OCR
	TessBaseAPIEnd(lctx->tess_handle);
	TessBaseAPIDelete(lctx->tess_handle);

	//Free subtitle
	freep(lctx->dec_sub);
	freep(ctx);
}
Exemple #3
0
static const VSFrameRef *VS_CC OCRGetFrame(int n, int activationReason,
                                           void **instanceData,
                                           void **frameData,
                                           VSFrameContext *frameCtx,
                                           VSCore *core,
                                           const VSAPI *vsapi)
{
    OCRData *d = (OCRData *) * instanceData;

    if (activationReason == arInitial) {
        vsapi->requestFrameFilter(n, d->node, frameCtx);
    } else if (activationReason == arAllFramesReady) {
        const VSFrameRef *src = vsapi->getFrameFilter(n, d->node, frameCtx);
        VSFrameRef *dst = vsapi->copyFrame(src, core);
        VSMap *m = vsapi->getFramePropsRW(dst);

        const uint8_t *srcp = vsapi->getReadPtr(src, 0);
        int width = vsapi->getFrameWidth(src, 0);
        int height = vsapi->getFrameHeight(src, 0);
        int stride = vsapi->getStride(src, 0);

        TessBaseAPI *api = TessBaseAPICreate();
        if (TessBaseAPIInit3(api, d->datapath, d->language) == -1) {
            vsapi->setFilterError("Failed to initialize Tesseract", frameCtx);

            TessBaseAPIDelete(api);
            vsapi->freeFrame(src);
            vsapi->freeFrame(dst);

            return 0;
        }

        if (d->options) {
            int i, err;
            int nopts = vsapi->propNumElements(d->options, "options");

            for (i = 0; i < nopts; i += 2) {
                const char *key = vsapi->propGetData(d->options, "options",
                                                     i, &err);
                const char *value = vsapi->propGetData(d->options, "options",
                                                       i + 1, &err);

                if (!TessBaseAPISetVariable(api, key, value)) {
                    char msg[200];

                    snprintf(msg, 200,
                             "Failed to set Tesseract option '%s'", key);

                    vsapi->setFilterError(msg, frameCtx);

                    TessBaseAPIEnd(api);
                    TessBaseAPIDelete(api);
                    vsapi->freeFrame(src);
                    vsapi->freeFrame(dst);

                    return 0;
                }
            }
        }

        {
            unsigned i;

            char *result = TessBaseAPIRect(api, srcp, 1,
                                           stride, 0, 0, width, height);
            int *confs = TessBaseAPIAllWordConfidences(api);
            int length = strlen(result);

            for (; length > 0 && isspace(result[length - 1]); length--);
            vsapi->propSetData(m, "OCRString", result, length, paReplace);

            for (i = 0; confs[i] != -1; i++) {
                vsapi->propSetInt(m, "OCRConfidence", confs[i], paAppend);
            }

            free(confs);
            free(result);
        }

        TessBaseAPIEnd(api);
        TessBaseAPIDelete(api);
        vsapi->freeFrame(src);

        return dst;
    }

    return 0;
}