Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	
		sberbank-ai
		
	commited on
		
		
					Commit 
							
							·
						
						c53b2c3
	
1
								Parent(s):
							
							6a52051
								
Create app.py
Browse files
    	
        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()
         | 
