hysts HF staff commited on
Commit
3b6fea8
1 Parent(s): 45f1543
Files changed (9) hide show
  1. .gitmodules +3 -0
  2. .pre-commit-config.yaml +46 -0
  3. .style.yapf +5 -0
  4. MangaLineExtraction_PyTorch +1 -0
  5. README.md +2 -2
  6. app.py +59 -0
  7. model.py +61 -0
  8. requirements.txt +4 -0
  9. style.css +11 -0
.gitmodules ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [submodule "MangaLineExtraction_PyTorch"]
2
+ path = MangaLineExtraction_PyTorch
3
+ url = https://github.com/ljsabc/MangaLineExtraction_PyTorch
.pre-commit-config.yaml ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ exclude: ^MangaLineExtraction_PyTorch
2
+ repos:
3
+ - repo: https://github.com/pre-commit/pre-commit-hooks
4
+ rev: v4.2.0
5
+ hooks:
6
+ - id: check-executables-have-shebangs
7
+ - id: check-json
8
+ - id: check-merge-conflict
9
+ - id: check-shebang-scripts-are-executable
10
+ - id: check-toml
11
+ - id: check-yaml
12
+ - id: double-quote-string-fixer
13
+ - id: end-of-file-fixer
14
+ - id: mixed-line-ending
15
+ args: ['--fix=lf']
16
+ - id: requirements-txt-fixer
17
+ - id: trailing-whitespace
18
+ - repo: https://github.com/myint/docformatter
19
+ rev: v1.4
20
+ hooks:
21
+ - id: docformatter
22
+ args: ['--in-place']
23
+ - repo: https://github.com/pycqa/isort
24
+ rev: 5.10.1
25
+ hooks:
26
+ - id: isort
27
+ - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v0.812
29
+ hooks:
30
+ - id: mypy
31
+ args: ['--ignore-missing-imports']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ args: ['--parallel', '--in-place']
37
+ - repo: https://github.com/kynan/nbstripout
38
+ rev: 0.5.0
39
+ hooks:
40
+ - id: nbstripout
41
+ args: ['--extra-keys', 'metadata.interpreter metadata.kernelspec cell.metadata.pycharm']
42
+ - repo: https://github.com/nbQA-dev/nbQA
43
+ rev: 1.3.1
44
+ hooks:
45
+ - id: nbqa-isort
46
+ - id: nbqa-yapf
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
MangaLineExtraction_PyTorch ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit 738ffab9ca4dd14da14b8c684b25278bc22556f8
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: MangaLineExtraction PyTorch
3
  emoji: 📈
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 3.0.14
8
  app_file: app.py
9
  pinned: false
10
  ---
 
1
  ---
2
+ title: MangaLineExtraction_PyTorch
3
  emoji: 📈
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 3.0.11
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+
7
+ import gradio as gr
8
+
9
+ from model import Model
10
+
11
+ DESCRIPTION = '''# MangaLineExtraction_PyTorch
12
+
13
+ This is an unofficial demo for [https://github.com/ljsabc/MangaLineExtraction_PyTorch](https://github.com/ljsabc/MangaLineExtraction_PyTorch).
14
+ '''
15
+ FOOTER = '<img id="visitor-badge" alt="visitor badge" src="https://visitor-badge.glitch.me/badge?page_id=hysts.mangalineextraction_pytorch" />'
16
+
17
+
18
+ def parse_args() -> argparse.Namespace:
19
+ parser = argparse.ArgumentParser()
20
+ parser.add_argument('--device', type=str, default='cpu')
21
+ parser.add_argument('--theme', type=str)
22
+ parser.add_argument('--share', action='store_true')
23
+ parser.add_argument('--port', type=int)
24
+ parser.add_argument('--disable-queue',
25
+ dest='enable_queue',
26
+ action='store_false')
27
+ return parser.parse_args()
28
+
29
+
30
+ def main():
31
+ args = parse_args()
32
+ model = Model(device=args.device)
33
+
34
+ with gr.Blocks(theme=args.theme, css='style.css') as demo:
35
+ gr.Markdown(DESCRIPTION)
36
+
37
+ with gr.Row():
38
+ with gr.Column():
39
+ with gr.Group():
40
+ input_image = gr.Image(label='Input', type='numpy')
41
+ run_button = gr.Button(value='Run')
42
+ with gr.Column():
43
+ result = gr.Image(label='Result',
44
+ type='numpy',
45
+ elem_id='result')
46
+
47
+ gr.Markdown(FOOTER)
48
+
49
+ run_button.click(fn=model.predict, inputs=input_image, outputs=result)
50
+
51
+ demo.launch(
52
+ enable_queue=args.enable_queue,
53
+ server_port=args.port,
54
+ share=args.share,
55
+ )
56
+
57
+
58
+ if __name__ == '__main__':
59
+ main()
model.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import pathlib
5
+ import sys
6
+
7
+ import cv2
8
+ import huggingface_hub
9
+ import numpy as np
10
+ import torch
11
+ import torch.nn as nn
12
+
13
+ current_dir = pathlib.Path(__file__).parent
14
+ submodule_dir = current_dir / 'MangaLineExtraction_PyTorch'
15
+ sys.path.insert(0, submodule_dir.as_posix())
16
+
17
+ from model_torch import res_skip
18
+
19
+ HF_TOKEN = os.environ['HF_TOKEN']
20
+
21
+ MAX_SIZE = 1000
22
+
23
+
24
+ class Model:
25
+ def __init__(self, device: str | torch.device):
26
+ self.device = torch.device(device)
27
+ self.model = self._load_model()
28
+
29
+ def _load_model(self) -> nn.Module:
30
+ ckpt_path = huggingface_hub.hf_hub_download(
31
+ 'hysts/MangaLineExtraction_PyTorch',
32
+ 'erika.pth',
33
+ use_auth_token=HF_TOKEN)
34
+ state_dict = torch.load(ckpt_path)
35
+ model = res_skip()
36
+ model.load_state_dict(state_dict)
37
+ model.to(self.device)
38
+ model.eval()
39
+ return model
40
+
41
+ @torch.inference_mode()
42
+ def predict(self, image: np.ndarray) -> np.ndarray:
43
+ gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
44
+
45
+ if max(gray.shape) > MAX_SIZE:
46
+ scale = MAX_SIZE / max(gray.shape)
47
+ gray = cv2.resize(gray, None, fx=scale, fy=scale)
48
+
49
+ h, w = gray.shape
50
+ size = 16
51
+ new_w = (w + size - 1) // size * size
52
+ new_h = (h + size - 1) // size * size
53
+
54
+ patch = np.ones((1, 1, new_h, new_w), dtype=np.float32)
55
+ patch[0, 0, :h, :w] = gray
56
+ tensor = torch.from_numpy(patch).to(self.device)
57
+ out = self.model(tensor)
58
+
59
+ res = out.cpu().numpy()[0, 0, :h, :w]
60
+ res = np.clip(res, 0, 255).astype(np.uint8)
61
+ return res
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy==1.22.4
2
+ opencv-python-headless==4.5.5.64
3
+ torch==1.11.0
4
+ torchvision==0.12.0
style.css ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+ div#result {
5
+ max-width: 600px;
6
+ max-height: 600px;
7
+ }
8
+ img#visitor-badge {
9
+ display: block;
10
+ margin: auto;
11
+ }