• 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 
76 static const enum AVPixelFormat pix_fmts[] = {
77     AV_PIX_FMT_GRAY8,
78     AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
79     AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
80     AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
81     AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
82     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
83     AV_PIX_FMT_YUVJ411P,
84     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
85     AV_PIX_FMT_NONE
86 };
87 
filter_frame(AVFilterLink * inlink,AVFrame * in)88 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
89 {
90     AVDictionary **metadata = &in->metadata;
91     AVFilterContext *ctx = inlink->dst;
92     AVFilterLink *outlink = ctx->outputs[0];
93     OCRContext *s = ctx->priv;
94     char *result;
95     int *confs;
96 
97     result = TessBaseAPIRect(s->tess, in->data[0], 1,
98                              in->linesize[0], 0, 0, in->width, in->height);
99     confs = TessBaseAPIAllWordConfidences(s->tess);
100     av_dict_set(metadata, "lavfi.ocr.text", result, 0);
101     for (int i = 0; confs[i] != -1; i++) {
102         char number[256];
103 
104         snprintf(number, sizeof(number), "%d ", confs[i]);
105         av_dict_set(metadata, "lavfi.ocr.confidence", number, AV_DICT_APPEND);
106     }
107 
108     TessDeleteText(result);
109     TessDeleteIntArray(confs);
110 
111     return ff_filter_frame(outlink, in);
112 }
113 
uninit(AVFilterContext * ctx)114 static av_cold void uninit(AVFilterContext *ctx)
115 {
116     OCRContext *s = ctx->priv;
117 
118     TessBaseAPIEnd(s->tess);
119     TessBaseAPIDelete(s->tess);
120 }
121 
122 AVFILTER_DEFINE_CLASS(ocr);
123 
124 static const AVFilterPad ocr_inputs[] = {
125     {
126         .name         = "default",
127         .type         = AVMEDIA_TYPE_VIDEO,
128         .filter_frame = filter_frame,
129     },
130 };
131 
132 static const AVFilterPad ocr_outputs[] = {
133     {
134         .name         = "default",
135         .type         = AVMEDIA_TYPE_VIDEO,
136     },
137 };
138 
139 const AVFilter ff_vf_ocr = {
140     .name          = "ocr",
141     .description   = NULL_IF_CONFIG_SMALL("Optical Character Recognition."),
142     .priv_size     = sizeof(OCRContext),
143     .priv_class    = &ocr_class,
144     .init          = init,
145     .uninit        = uninit,
146     .flags         = AVFILTER_FLAG_METADATA_ONLY,
147     FILTER_INPUTS(ocr_inputs),
148     FILTER_OUTPUTS(ocr_outputs),
149     FILTER_PIXFMTS_ARRAY(pix_fmts),
150 };
151