vf_ocr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2015 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <tesseract/capi.h>
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "formats.h"
  24. #include "internal.h"
  25. #include "video.h"
  26. typedef struct OCRContext {
  27. const AVClass *class;
  28. char *datapath;
  29. char *language;
  30. char *whitelist;
  31. char *blacklist;
  32. TessBaseAPI *tess;
  33. } OCRContext;
  34. #define OFFSET(x) offsetof(OCRContext, x)
  35. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  36. static const AVOption ocr_options[] = {
  37. { "datapath", "set datapath", OFFSET(datapath), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  38. { "language", "set language", OFFSET(language), AV_OPT_TYPE_STRING, {.str="eng"}, 0, 0, FLAGS },
  39. { "whitelist", "set character whitelist", OFFSET(whitelist), AV_OPT_TYPE_STRING, {.str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:;,-+_!?\"'[]{}()<>|/\\=*&%$#@!~"}, 0, 0, FLAGS },
  40. { "blacklist", "set character blacklist", OFFSET(blacklist), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS },
  41. { NULL }
  42. };
  43. static av_cold int init(AVFilterContext *ctx)
  44. {
  45. OCRContext *s = ctx->priv;
  46. s->tess = TessBaseAPICreate();
  47. if (TessBaseAPIInit3(s->tess, s->datapath, s->language) == -1) {
  48. av_log(ctx, AV_LOG_ERROR, "failed to init tesseract\n");
  49. return AVERROR(EINVAL);
  50. }
  51. if (!TessBaseAPISetVariable(s->tess, "tessedit_char_whitelist", s->whitelist)) {
  52. av_log(ctx, AV_LOG_ERROR, "failed to set whitelist\n");
  53. return AVERROR(EINVAL);
  54. }
  55. if (!TessBaseAPISetVariable(s->tess, "tessedit_char_blacklist", s->blacklist)) {
  56. av_log(ctx, AV_LOG_ERROR, "failed to set blacklist\n");
  57. return AVERROR(EINVAL);
  58. }
  59. av_log(ctx, AV_LOG_DEBUG, "Tesseract version: %s\n", TessVersion());
  60. return 0;
  61. }
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. static const enum AVPixelFormat pix_fmts[] = {
  65. AV_PIX_FMT_GRAY8,
  66. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  67. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  68. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  69. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  70. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  71. AV_PIX_FMT_YUVJ411P,
  72. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  73. AV_PIX_FMT_NONE
  74. };
  75. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  76. if (!fmts_list)
  77. return AVERROR(ENOMEM);
  78. return ff_set_common_formats(ctx, fmts_list);
  79. }
  80. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  81. {
  82. AVDictionary **metadata = &in->metadata;
  83. AVFilterContext *ctx = inlink->dst;
  84. AVFilterLink *outlink = ctx->outputs[0];
  85. OCRContext *s = ctx->priv;
  86. char *result;
  87. int *confs;
  88. result = TessBaseAPIRect(s->tess, in->data[0], 1,
  89. in->linesize[0], 0, 0, in->width, in->height);
  90. confs = TessBaseAPIAllWordConfidences(s->tess);
  91. av_dict_set(metadata, "lavfi.ocr.text", result, 0);
  92. for (int i = 0; confs[i] != -1; i++) {
  93. char number[256];
  94. snprintf(number, sizeof(number), "%d ", confs[i]);
  95. av_dict_set(metadata, "lavfi.ocr.confidence", number, AV_DICT_APPEND);
  96. }
  97. TessDeleteText(result);
  98. TessDeleteIntArray(confs);
  99. return ff_filter_frame(outlink, in);
  100. }
  101. static av_cold void uninit(AVFilterContext *ctx)
  102. {
  103. OCRContext *s = ctx->priv;
  104. TessBaseAPIEnd(s->tess);
  105. TessBaseAPIDelete(s->tess);
  106. }
  107. AVFILTER_DEFINE_CLASS(ocr);
  108. static const AVFilterPad ocr_inputs[] = {
  109. {
  110. .name = "default",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .filter_frame = filter_frame,
  113. },
  114. { NULL }
  115. };
  116. static const AVFilterPad ocr_outputs[] = {
  117. {
  118. .name = "default",
  119. .type = AVMEDIA_TYPE_VIDEO,
  120. },
  121. { NULL }
  122. };
  123. AVFilter ff_vf_ocr = {
  124. .name = "ocr",
  125. .description = NULL_IF_CONFIG_SMALL("Optical Character Recognition."),
  126. .priv_size = sizeof(OCRContext),
  127. .priv_class = &ocr_class,
  128. .query_formats = query_formats,
  129. .init = init,
  130. .uninit = uninit,
  131. .inputs = ocr_inputs,
  132. .outputs = ocr_outputs,
  133. };