Cyber ai-forever commited on
Commit
fe76354
·
0 Parent(s):

Duplicate from ai-forever/NotebooksRecognition

Browse files

Co-authored-by: ai-forever <[email protected]>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +23 -0
  3. app.py +64 -0
  4. requirements.txt +1 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: NotebooksRecognition
3
+ emoji: 🐨
4
+ colorFrom: yellow
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.17.0
8
+ app_file: app.py
9
+ pinned: false
10
+ tags:
11
+ - OCR
12
+ - Segmentation
13
+ - HTR
14
+ models:
15
+ - sberbank-ai/ReadingPipeline-notebooks
16
+ datasets:
17
+ - sberbank-ai/school_notebooks_RU
18
+ - sberbank-ai/school_notebooks_EN
19
+ license: mit
20
+ duplicated_from: ai-forever/NotebooksRecognition
21
+ ---
22
+
23
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system("pip install git+https://github.com/ai-forever/ReadingPipeline.git")
3
+
4
+ import cv2
5
+ import json
6
+
7
+ import gradio as gr
8
+ from huggingface_hub import hf_hub_download
9
+
10
+ from ocrpipeline.predictor import PipelinePredictor
11
+ from ocrpipeline.linefinder import get_structured_text
12
+
13
+
14
+ def get_config_and_download_weights(repo_id, device='cpu'):
15
+ # download weights and configs
16
+ pipeline_config_path = hf_hub_download(repo_id, "pipeline_config.json")
17
+ ocr_model_path = hf_hub_download(repo_id, "ocr/ocr_model.onnx")
18
+ kenlm_path = hf_hub_download(repo_id, "ocr/kenlm_corpus.arpa")
19
+ ocr_config_path = hf_hub_download(repo_id, "ocr/ocr_config.json")
20
+ segm_model_path = hf_hub_download(repo_id, "segm/segm_model.onnx")
21
+ segm_config_path = hf_hub_download(repo_id, "segm/segm_config.json")
22
+
23
+ # change paths to downloaded weights and configs in main pipeline_config
24
+ with open(pipeline_config_path, 'r') as f:
25
+ pipeline_config = json.load(f)
26
+ pipeline_config['main_process']['SegmPrediction']['model_path'] = segm_model_path
27
+ pipeline_config['main_process']['SegmPrediction']['config_path'] = segm_config_path
28
+ pipeline_config['main_process']['SegmPrediction']['num_threads'] = 2
29
+ pipeline_config['main_process']['SegmPrediction']['device'] = device
30
+ pipeline_config['main_process']['SegmPrediction']['runtime'] = "OpenVino"
31
+
32
+ pipeline_config['main_process']['OCRPrediction']['model_path'] = ocr_model_path
33
+ pipeline_config['main_process']['OCRPrediction']['lm_path'] = kenlm_path
34
+ pipeline_config['main_process']['OCRPrediction']['config_path'] = ocr_config_path
35
+ pipeline_config['main_process']['OCRPrediction']['num_threads'] = 2
36
+ pipeline_config['main_process']['OCRPrediction']['device'] = device
37
+ pipeline_config['main_process']['OCRPrediction']['runtime'] = "OpenVino"
38
+
39
+ # save pipeline_config
40
+ with open(pipeline_config_path, 'w') as f:
41
+ json.dump(pipeline_config, f)
42
+
43
+ return pipeline_config_path
44
+
45
+
46
+ def predict(image_path):
47
+ image = cv2.imread(image_path)
48
+ rotated_image, pred_data = PREDICTOR(image)
49
+ structured_text = get_structured_text(pred_data, ['shrinked_text'])
50
+ result_text = [' '.join(line_text) for page_text in structured_text
51
+ for line_text in page_text]
52
+ return '\n'.join(result_text)
53
+
54
+
55
+ PIPELINE_CONFIG_PATH = get_config_and_download_weights("sberbank-ai/ReadingPipeline-notebooks")
56
+
57
+ PREDICTOR = PipelinePredictor(pipeline_config_path=PIPELINE_CONFIG_PATH)
58
+
59
+ gr.Interface(
60
+ predict,
61
+ inputs=gr.Image(label="Upload an image of handwritten school notebook", type="filepath"),
62
+ outputs=gr.Textbox(label="Text on the image"),
63
+ title="School notebook recognition",
64
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ torch>=1.6.0