• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <tesseract/capi.h>
22 
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 typedef struct OCRContext {
30     const AVClass *class;
31 
32     char *datapath;
33     char *language;
34     char *whitelist;
35     char *blacklist;
36 
37     TessBaseAPI *tess;
38 } OCRContext;
39 
40 #define OFFSET(x) offsetof(OCRContext, x)
41 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
42 
43 static const AVOption ocr_options[] = {
44     { "datapath",  "set datapath",            OFFSET(datapath),  AV_OPT_TYPE_STRING, {.str=NULL},  0, 0, FLAGS },
45     { "language",  "set language",            OFFSET(language),  AV_OPT_TYPE_STRING, {.str="eng"}, 0, 0, FLAGS },
46     { "whitelist", "set character whitelist", OFFSET(whitelist), AV_OPT_TYPE_STRING, {.str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:;,-+_!?\"'[]{}()<>|/\\=*&%$#@!~ "}, 0, 0, FLAGS },
47     { "blacklist", "set character blacklist", OFFSET(blacklist), AV_OPT_TYPE_STRING, {.str=""},    0, 0, FLAGS },
48     { NULL }
49 };
50 
init(AVFilterContext * ctx)51 static av_cold int init(AVFilterContext *ctx)
52 {
53     OCRContext *s = ctx->priv;
54 
55     s->tess = TessBaseAPICreate();
56     if (TessBaseAPIInit3(s->tess, s->datapath, s->language) == -1) {
57         av_log(ctx, AV_LOG_ERROR, "failed to init tesseract\n");
58         return AVERROR(EINVAL);
59     }
60 
61     if (!TessBaseAPISetVariable(s->tess, "tessedit_char_whitelist", s->whitelist)) {
62         av_log(ctx, AV_LOG_ERROR, "failed to set whitelist\n");
63         return AVERROR(EINVAL);
64     }
65 
66     if (!TessBaseAPISetVariable(s->tess, "tessedit_char_blacklist", s->blacklist)) {
67         av_log(ctx, AV_LOG_ERROR, "failed to set blacklist\n");
68         return AVERROR(EINVAL);
69     }
70 
71     av_log(ctx, AV_LOG_DEBUG, "Tesseract version: %s\n", TessVersion());
72 
73     return 0;
74 }
75 
query_formats(AVFilterContext * ctx)76 static int query_formats(AVFilterContext *ctx)
77 {
78     static const enum AVPixelFormat pix_fmts[] = {
79         AV_PIX_FMT_GRAY8,
80         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
81         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
82         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
83         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
84         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
85         AV_PIX_FMT_YUVJ411P,
86         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
87         AV_PIX_FMT_NONE
88     };
89 
90     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
91     if (!fmts_list)
92         return AVERROR(ENOMEM);
93     return ff_set_common_formats(ctx, fmts_list);
94 }
95 
filter_frame(AVFilterLink * inlink,AVFrame * in)96 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
97 {
98     AVDictionary **metadata = &in->metadata;
99     AVFilterContext *ctx = inlink->dst;
100     AVFilterLink *outlink = ctx->outputs[0];
101     OCRContext *s = ctx->priv;
102     char *result;
103     int *confs;
104 
105     result = TessBaseAPIRect(s->tess, in->data[0], 1,
106                              in->linesize[0], 0, 0, in->width, in->height);
107     confs = TessBaseAPIAllWordConfidences(s->tess);
108     av_dict_set(metadata, "lavfi.ocr.text", result, 0);
109     for (int i = 0; confs[i] != -1; i++) {
110         char number[256];
111 
112         snprintf(number, sizeof(number), "%d ", confs[i]);
113         av_dict_set(metadata, "lavfi.ocr.confidence", number, AV_DICT_APPEND);
114     }
115 
116     TessDeleteText(result);
117     TessDeleteIntArray(confs);
118 
119     return ff_filter_frame(outlink, in);
120 }
121 
uninit(AVFilterContext * ctx)122 static av_cold void uninit(AVFilterContext *ctx)
123 {
124     OCRContext *s = ctx->priv;
125 
126     TessBaseAPIEnd(s->tess);
127     TessBaseAPIDelete(s->tess);
128 }
129 
130 AVFILTER_DEFINE_CLASS(ocr);
131 
132 static const AVFilterPad ocr_inputs[] = {
133     {
134         .name         = "default",
135         .type         = AVMEDIA_TYPE_VIDEO,
136         .filter_frame = filter_frame,
137     },
138     { NULL }
139 };
140 
141 static const AVFilterPad ocr_outputs[] = {
142     {
143         .name         = "default",
144         .type         = AVMEDIA_TYPE_VIDEO,
145     },
146     { NULL }
147 };
148 
149 AVFilter ff_vf_ocr = {
150     .name          = "ocr",
151     .description   = NULL_IF_CONFIG_SMALL("Optical Character Recognition."),
152     .priv_size     = sizeof(OCRContext),
153     .priv_class    = &ocr_class,
154     .query_formats = query_formats,
155     .init          = init,
156     .uninit        = uninit,
157     .inputs        = ocr_inputs,
158     .outputs       = ocr_outputs,
159 };
160