Skip to content

A library providing C and C++ bindings for mangaocr

License

Notifications You must be signed in to change notification settings

ripose-jp/libmocr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libmocr

A library providing C and C++ bindings for mangaocr

Building

Dependencies

  • Python
  • A C99 compatible compiler
  • A C++11 compatible compiler

Building

Make sure CMake is installed, navigate to the root of the repository, and run the following:

mkdir build
cd build
cmake ..
cmake --build . --config Release

The libraries should be in the build folder.

Installing (Linux)

Navigate to the build folder and run:

sudo cmake --install . --config Release
sudo ldconfig /usr/local/lib

Compiling Projects with libmocr

This section assume you are on Linux and the dependencies are installed to a known location such as /usr/local/lib.

Compiling with the C API:

gcc example.c -lmocr -o example

Compiling with the C++ API:

g++ example.cpp -lmocr++ -o example

Notes

libmocr does not bundle mangaocr. The mangaocr Python library must be available to Python at runtime just like any other Python library.

mangaocr can be installed from pip by running:

pip3 install manga-ocr

Usage

Below are simple programs that read in an image file from the command line and prints out the extracted text to stdout.

C

#include <stdio.h>
#include <mocr.h>

int main(int argc, char **argv)
{
    if (argc != 2) return 1;
    mocr_ctx *ctx = mocr_init(DEFAULT_MODEL, 0);
    char *text = mocr_read_file(ctx, argv[1]);
    puts(text);
    mocr_free(text);
    mocr_destroy(ctx);
    return 0;
}

C++

#include <iostream>
#include <mocr++.h>

int main(int argc, char **argv)
{
    if (argc != 2) return 1;
    mocr::model mod;
    if (!mod) return 1;
    std::cout << mod.read(argv[1]) << std::endl;
    return 0;
}

Troubleshooting

ModuleNotFoundError: No module named 'manga_ocr'

Make sure mangaocr is installed by running

pip3 install manga-ocr

If you have multiple Python versions installed, it's possible that libmocr is linked to a version that doesn't have mangaocr installed.

Run pip3 --version to find the Python version connected to pip. Recompile libmocr with the -DEXACT_PYTHON_VERSION=<version> option set to the same version as the Python version number reported by pip.

This can be done by running in the build directory:

cmake -DEXACT_PYTHON_VERSION=3.10 ..

EXC_BAD_ACCESS on macOS

This bug can be reproduced by calling mocr_init() for the C API or constructor for the C++ API in a pthread on macOS. This happens due to macOS's default pthread stack size of 512KB being too small to initialize a context. This can be fixed by using pthread_attr_setstacksize to set the stack size to a larger value such as 8MB.

Acknowledgements

Thanks to kha-white and the other mangaocr contributors for creating this library, the model, and compiling the test data.

Testing done with GoogleTest.

The stb project for the stb_image.h library used for testing.

About

A library providing C and C++ bindings for mangaocr

Topics

Resources

License

Stars

Watchers

Forks