vf_ocr.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. ff_set_common_formats(ctx, fmts_list);
  79. return 0;
  80. }
  81. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  82. {
  83. AVDictionary **metadata = avpriv_frame_get_metadatap(in);
  84. AVFilterContext *ctx = inlink->dst;
  85. AVFilterLink *outlink = ctx->outputs[0];
  86. OCRContext *s = ctx->priv;
  87. char *result;
  88. result = TessBaseAPIRect(s->tess, in->data[0], 1,
  89. in->linesize[0], 0, 0, in->width, in->height);
  90. av_dict_set(metadata, "lavfi.ocr.text", result, 0);
  91. TessDeleteText(result);
  92. return ff_filter_frame(outlink, in);
  93. }
  94. static av_cold void uninit(AVFilterContext *ctx)
  95. {
  96. OCRContext *s = ctx->priv;
  97. TessBaseAPIEnd(s->tess);
  98. TessBaseAPIDelete(s->tess);
  99. }
  100. AVFILTER_DEFINE_CLASS(ocr);
  101. static const AVFilterPad ocr_inputs[] = {
  102. {
  103. .name = "default",
  104. .type = AVMEDIA_TYPE_VIDEO,
  105. .filter_frame = filter_frame,
  106. },
  107. { NULL }
  108. };
  109. static const AVFilterPad ocr_outputs[] = {
  110. {
  111. .name = "default",
  112. .type = AVMEDIA_TYPE_VIDEO,
  113. },
  114. { NULL }
  115. };
  116. AVFilter ff_vf_ocr = {
  117. .name = "ocr",
  118. .description = NULL_IF_CONFIG_SMALL("Optical Character Recognition."),
  119. .priv_size = sizeof(OCRContext),
  120. .priv_class = &ocr_class,
  121. .query_formats = query_formats,
  122. .init = init,
  123. .uninit = uninit,
  124. .inputs = ocr_inputs,
  125. .outputs = ocr_outputs,
  126. };