[FFmpeg-devel] [PATCH] avfilter: add ocr filter
Paul B Mahol
onemda at gmail.com
Wed Sep 9 17:23:06 CEST 2015
Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
Use with crop & scale for fun.
---
configure | 4 ++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_ocr.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 138 insertions(+)
create mode 100644 libavfilter/vf_ocr.c
diff --git a/configure b/configure
index cd0c22a..c2908b2 100755
--- a/configure
+++ b/configure
@@ -246,6 +246,7 @@ External library support:
--enable-libspeex enable Speex de/encoding via libspeex [no]
--enable-libssh enable SFTP protocol via libssh [no]
--enable-libstagefright-h264 enable H.264 decoding via libstagefright [no]
+ --enable-libtesseract enable Tesseract, needed for ocr filter [no]
--enable-libtheora enable Theora encoding via libtheora [no]
--enable-libtwolame enable MP2 encoding via libtwolame [no]
--enable-libutvideo enable Ut Video encoding and decoding via libutvideo [no]
@@ -1412,6 +1413,7 @@ EXTERNAL_LIBRARY_LIST="
libspeex
libssh
libstagefright_h264
+ libtesseract
libtheora
libtwolame
libutvideo
@@ -2777,6 +2779,7 @@ mptestsrc_filter_deps="gpl"
negate_filter_deps="lut_filter"
perspective_filter_deps="gpl"
pp7_filter_deps="gpl"
+ocr_filter_deps="libtesseract"
ocv_filter_deps="libopencv"
owdenoise_filter_deps="gpl"
pan_filter_deps="swresample"
@@ -5277,6 +5280,7 @@ enabled libspeex && require_pkg_config speex speex/speex.h speex_decode
enabled libstagefright_h264 && require_cpp libstagefright_h264 "binder/ProcessState.h media/stagefright/MetaData.h
media/stagefright/MediaBufferGroup.h media/stagefright/MediaDebug.h media/stagefright/MediaDefs.h
media/stagefright/OMXClient.h media/stagefright/OMXCodec.h" android::OMXClient -lstagefright -lmedia -lutils -lbinder -lgnustl_static
+enabled libtesseract && require_pkg_config tesseract tesseract/capi.h TessBaseAPICreate
enabled libtheora && require libtheora theora/theoraenc.h th_info_init -ltheoraenc -ltheoradec -logg
enabled libtwolame && require libtwolame twolame.h twolame_init -ltwolame &&
{ check_lib twolame.h twolame_encode_buffer_float32_interleaved -ltwolame ||
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 11a8646..d3e282f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -169,6 +169,7 @@ OBJS-$(CONFIG_NEGATE_FILTER) += vf_lut.o
OBJS-$(CONFIG_NOFORMAT_FILTER) += vf_format.o
OBJS-$(CONFIG_NOISE_FILTER) += vf_noise.o
OBJS-$(CONFIG_NULL_FILTER) += vf_null.o
+OBJS-$(CONFIG_OCR_FILTER) += vf_ocr.o
OBJS-$(CONFIG_OCV_FILTER) += vf_libopencv.o
OBJS-$(CONFIG_OPENCL) += deshake_opencl.o unsharp_opencl.o
OBJS-$(CONFIG_OVERLAY_FILTER) += vf_overlay.o dualinput.o framesync.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 9df2d53..639e473 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -191,6 +191,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(NOFORMAT, noformat, vf);
REGISTER_FILTER(NOISE, noise, vf);
REGISTER_FILTER(NULL, null, vf);
+ REGISTER_FILTER(OCR, ocr, vf);
REGISTER_FILTER(OCV, ocv, vf);
REGISTER_FILTER(OVERLAY, overlay, vf);
REGISTER_FILTER(OWDENOISE, owdenoise, vf);
diff --git a/libavfilter/vf_ocr.c b/libavfilter/vf_ocr.c
new file mode 100644
index 0000000..fcdaec7
--- /dev/null
+++ b/libavfilter/vf_ocr.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2015 Paul B Mahol
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <tesseract/capi.h>
+
+#include "float.h"
+
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct OCRContext {
+ const AVClass *class;
+
+ char *whitelist;
+ char *datapath;
+ char *language;
+ TessBaseAPI *tess;
+} OCRContext;
+
+#define OFFSET(x) offsetof(OCRContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption ocr_options[] = {
+ { "d", "set datapath", OFFSET(datapath), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
+ { "l", "set language", OFFSET(language), AV_OPT_TYPE_STRING, {.str="eng"}, 0, 0, FLAGS },
+ { "w", "set character whitelist", OFFSET(whitelist), AV_OPT_TYPE_STRING, {.str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:;,-!?\"'"}, 0, 0, FLAGS },
+ { NULL }
+};
+
+static av_cold int init(AVFilterContext *ctx)
+{
+ OCRContext *s = ctx->priv;
+
+ s->tess = TessBaseAPICreate();
+ if (TessBaseAPIInit3(s->tess, s->datapath, s->language) == -1)
+ return AVERROR(EINVAL);
+
+ if (!TessBaseAPISetVariable(s->tess, "tessedit_char_whitelist", s->whitelist)) {
+ av_log(ctx, AV_LOG_ERROR, "failed to set whitelist\n");
+ return AVERROR(EINVAL);
+ }
+
+ return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat pix_fmts[] = {
+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUV420P,
+ AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
+ AV_PIX_FMT_NONE
+ };
+
+ AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+ if (!fmts_list)
+ return AVERROR(ENOMEM);
+ ff_set_common_formats(ctx, fmts_list);
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AVFilterLink *outlink = ctx->outputs[0];
+ OCRContext *s = ctx->priv;
+ char *result;
+
+ result = TessBaseAPIRect(s->tess, in->data[0], 1,
+ in->linesize[0], 0, 0, in->width, in->height);
+ av_log(ctx, AV_LOG_INFO, "%s\n", result);
+
+ return ff_filter_frame(outlink, in);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ OCRContext *s = ctx->priv;
+
+ TessBaseAPIDelete(s->tess);
+}
+
+AVFILTER_DEFINE_CLASS(ocr);
+
+static const AVFilterPad ocr_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ },
+ { NULL }
+};
+
+static const AVFilterPad ocr_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_ocr = {
+ .name = "ocr",
+ .description = NULL_IF_CONFIG_SMALL("Optical Character Recognition."),
+ .priv_size = sizeof(OCRContext),
+ .priv_class = &ocr_class,
+ .query_formats = query_formats,
+ .init = init,
+ .uninit = uninit,
+ .inputs = ocr_inputs,
+ .outputs = ocr_outputs,
+};
--
1.7.11.2
More information about the ffmpeg-devel
mailing list