kha-white / mokuro

Read Japanese manga inside browser with selectable text.
GNU General Public License v3.0
845 stars 64 forks source link

Avif format support #57

Open Jenrykster opened 1 year ago

Jenrykster commented 1 year ago

Are there any plans to support avif image files in mokuro ? By looking at the code it seems that only jpg, jpeg and png are supported at the moment.

Jenrykster commented 1 year ago

There's an open PR in the opencv repo that adds support for avif image files. I built the python package using this fork and changed the file format tuple inside overlay_generator.py and got it working perfectly 👍

avc1657 commented 1 year ago

Could you please explain how to do that? I want AVIF support for mokuro.

Jenrykster commented 1 year ago

Could you please explain how to do that? I want AVIF support for mokuro.

It seems that for now, the OpenCV team will treat avif support as experimental so you'll probably need to build the lib yourself.

I used this repo to compile a custom opencv python package using the mentioned PR fork.

Basically what I did was:

After that, I just used mokuro directly from my cloned repository

Also, It took me hours of trial and error to make it work since I have almost no experience with either Python or C++, so maybe there's a better way to do this

xrishox commented 1 year ago

I've tried to install it using your steps and it doesn't appear to work for me. I manually compiled and installed opencv from their github (the pr has been merged now). I installed the git version of opencv-python. I cloned the mokuro repository with submodules and made the modifications and then I installed it using

pip3 install -e .

I wind up getting this error though:

(mokuro) [anonymous@fedora stuff]$ mokuro v01_Naruto/Volume\ 01\ -\ Naruto\ Uzumaki/

Paths to process:

/stuff/v01_Naruto/Volume 01 - Naruto Uzumaki

Each of the paths above will be treated as one volume. Continue? [yes/no]
yes
2023-06-18 14:00:38.461 | INFO     | mokuro.run:run:43 - Processing 1/1: /stuff/v01_Naruto/Volume 01 - Naruto Uzumaki
Processing pages...:   0%|                                                                                                                                   | 0/183 [00:00<?, ?it/s]2023-06-18 14:00:38.465 | INFO     | mokuro.manga_page_ocr:__init__:30 - Initializing text detector
2023-06-18 14:00:38.681 | INFO     | manga_ocr.ocr:__init__:13 - Loading OCR model from kha-white/manga-ocr-base
2023-06-18 14:00:40.256 | INFO     | manga_ocr.ocr:__init__:19 - Using CUDA
2023-06-18 14:00:42.120 | INFO     | manga_ocr.ocr:__init__:29 - OCR ready
Processing pages...:   0%|                                                                                                                                   | 0/183 [00:03<?, ?it/s]
2023-06-18 14:00:42.120 | ERROR    | mokuro.run:run:47 - Error while processing /stuff/v01_Naruto/Volume 01 - Naruto Uzumaki
Traceback (most recent call last):

  File "/home/anonymous/anaconda3/envs/mokuro/bin/mokuro", line 33, in <module>
    sys.exit(load_entry_point('mokuro', 'console_scripts', 'mokuro')())
    │   │    └ <function importlib_load_entry_point at 0x7f8958d984a0>
    │   └ <built-in function exit>
    └ <module 'sys' (built-in)>

  File "/home/anonymous/mokuro/mokuro/__main__.py", line 7, in main
    fire.Fire(run)
    │    │    └ <function run at 0x7f895887a480>
    │    └ <function Fire at 0x7f88535c1f80>
    └ <module 'fire' from '/home/anonymous/anaconda3/envs/mokuro/lib/python3.11/site-packages/fire/__init__.py'>

  File "/home/anonymous/anaconda3/envs/mokuro/lib/python3.11/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
                      │     │          │     │                 │        └ 'mokuro'
                      │     │          │     │                 └ {}
                      │     │          │     └ Namespace(verbose=False, interactive=False, separator='-', completion=None, help=False, trace=False)
                      │     │          └ ['v01_Naruto/Volume 01 - Naruto Uzumaki/']
                      │     └ <function run at 0x7f895887a480>
                      └ <function _Fire at 0x7f88535f42c0>
  File "/home/anonymous/anaconda3/envs/mokuro/lib/python3.11/site-packages/fire/core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
    │                           └ <function _CallAndUpdateTrace at 0x7f88535f4400>
    └ <function run at 0x7f895887a480>
  File "/home/anonymous/anaconda3/envs/mokuro/lib/python3.11/site-packages/fire/core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
                │   │          └ {}
                │   └ ['v01_Naruto/Volume 01 - Naruto Uzumaki/']
                └ <function run at 0x7f895887a480>

> File "/home/anonymous/mokuro/mokuro/run.py", line 45, in run
    ovg.process_dir(path, as_one_file=as_one_file)
    │   │           │                 └ True
    │   │           └ PosixPath('/stuff/v01_Naruto/Volume 01 - Naruto Uzumaki')
    │   └ <function OverlayGenerator.process_dir at 0x7f8853561800>
    └ <mokuro.overlay_generator.OverlayGenerator object at 0x7f88535f1ed0>

  File "/home/anonymous/mokuro/mokuro/overlay_generator.py", line 85, in process_dir
    result = self.mpocr(img_path)
             │    │     └ PosixPath('/stuff/v01_Naruto/Volume 01 - Naruto Uzumaki/000.avif')
             │    └ <mokuro.manga_page_ocr.MangaPageOcr object at 0x7f88531193d0>
             └ <mokuro.overlay_generator.OverlayGenerator object at 0x7f88535f1ed0>

  File "/home/anonymous/mokuro/mokuro/manga_page_ocr.py", line 38, in __call__
    H, W, *_ = img.shape
               └ None

AttributeError: 'NoneType' object has no attribute 'shape'
2023-06-18 14:00:42.122 | INFO     | mokuro.run:run:51 - Processed successfully: 0/1
(mokuro) [anonymous@fedora stuff]$ 

Any ideas?

Jenrykster commented 1 year ago

It seems that the version you compiled still doesn't have avif support. The PR was merged but it seems that the WITH_AVIF flag is off by default. So if you didn't change it will end up being the same as the pip version.

Later I'll try to compile from the post-merge repo and fork from mokuro to see if I can make the process a bit easier.

xrishox commented 1 year ago

Yep after about 3 hours of crying to chatgpt i managed to get it all working. I might be able to simplify it for other people who happen across this thread.

I attached the compiled linux python .so file with avif support. For me since I'm using conda I placed it in

/home/anonymous/anaconda3/envs/mokuro/lib/python3.11/site-packages/cv2.so

where another person would have to put it would need to be modified for where their stuff is stored. I'm not sure if this also requires the opencv software itself to be installed as well or if this .so file is all that is actually needed. From there you just modify the file that Jenry mentioned earlier and install it.

mokuro has submodules that it depends on, so git clone isn't enough to get it to work. You need to pull it down like this:

git clone --recurse-submodules -j8 https://github.com/kha-white/mokuro

then you change

img_paths = [p for p in path.glob('**/*') if p.is_file() and p.suffix.lower() in ('.jpg', '.jpeg', '.png')]

in

/mokuro/overlay_generator.py

to

img_paths = [p for p in path.glob('**/*') if p.is_file() and p.suffix.lower() in ('.jpg', '.jpeg', '.png', '.avif')]

then you can install it by going to the mokuro rootdir and typing

pip3 install -e .

cv2.so.zip

Hopefully I remembered all the steps correctly and hopefully this is useful to someone else.

Edit: note this probably won't work on windows since .so is a linux library format. WSL might work though.

eristocrates commented 3 months ago

For Windows, i recommend just bulk converting avif to png with something like XnConvert