struct lib_hardsubx_ctx* _init_hardsubx(struct ccx_s_options *options) { // Initialize HardsubX data structures struct lib_hardsubx_ctx *ctx = (struct lib_hardsubx_ctx *)malloc(sizeof(struct lib_hardsubx_ctx)); if(!ctx) fatal(EXIT_NOT_ENOUGH_MEMORY, "lib_hardsubx_ctx"); memset(ctx, 0, sizeof(struct lib_hardsubx_ctx)); ctx->tess_handle = TessBaseAPICreate(); if(TessBaseAPIInit3(ctx->tess_handle, NULL, "eng") != 0) { fatal(EXIT_NOT_ENOUGH_MEMORY, "Not enough memory to initialize Tesseract!"); } //Initialize attributes common to lib_ccx context ctx->basefilename = get_basename(options->output_filename);//TODO: Check validity, add stdin, network ctx->current_file = -1; ctx->inputfile = options->inputfile; ctx->num_input_files = options->num_input_files; ctx->extension = get_file_extension(options->write_format); ctx->write_format = options->write_format; ctx->subs_delay = options->subs_delay; ctx->cc_to_stdout = options->cc_to_stdout; //Initialize subtitle text parameters ctx->cur_conf = 0.0; ctx->prev_conf = 0.0; ctx->ocr_mode = options->hardsubx_ocr_mode; ctx->subcolor = options->hardsubx_subcolor; ctx->min_sub_duration = options->hardsubx_min_sub_duration; ctx->detect_italics = options->hardsubx_detect_italics; ctx->conf_thresh = options->hardsubx_conf_thresh; ctx->hue = options->hardsubx_hue; ctx->lum_thresh = options->hardsubx_lum_thresh; //Initialize subtitle structure memory ctx->dec_sub = (struct cc_subtitle *)malloc(sizeof(struct cc_subtitle)); memset (ctx->dec_sub, 0,sizeof(struct cc_subtitle)); return ctx; }
void* init_ocr(int lang_index) { int ret; struct ocrCtx* ctx; ctx = (struct ocrCtx*)malloc(sizeof(struct ocrCtx)); if(!ctx) return NULL; ctx->api = TessBaseAPICreate(); /* if language was undefined use english */ if(lang_index == 0) { /* select english */ lang_index = 1; } /* if langauge pack not found use english */ ret = search_language_pack("tessdata",language[lang_index]); if(ret < 0 ) { /* select english */ lang_index = 1; } ret = TessBaseAPIInit3(ctx->api, NULL, language[lang_index]); if(ret < 0) { goto fail; } return ctx; fail: delete_ocr((void**)&ctx); return NULL; }
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; }