updated the code and model
Browse files- app.py +121 -0
- model/config.json +182 -0
- model/generation_config.json +12 -0
- model/model.safetensors +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoFeatureExtractor, AutoTokenizer, TrOCRProcessor, VisionEncoderDecoderModel
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# def sauvola_thresholding(grayImage_, window_size=15):
|
| 8 |
+
# """
|
| 9 |
+
# Sauvola thresholds are local thresholding techniques that are
|
| 10 |
+
# useful for images where the background is not uniform, especially for text recognition.
|
| 11 |
+
|
| 12 |
+
# grayImage_ --- Input image should be in 2-Dimension Gray Scale format.
|
| 13 |
+
# window_size --- It represents the filter window size.
|
| 14 |
+
# """
|
| 15 |
+
# # Assert the input conditions
|
| 16 |
+
# assert len(grayImage_.shape) == 2, "Input image must be a 2-dimensional gray scale image."
|
| 17 |
+
# assert isinstance(window_size, int) and window_size > 0, "Window size must be a positive integer."
|
| 18 |
+
|
| 19 |
+
# thresh_sauvolavalue = threshold_sauvola(grayImage_, window_size=window_size)
|
| 20 |
+
# thresholdImage_ = (grayImage_ > thresh_sauvolavalue)
|
| 21 |
+
|
| 22 |
+
# return thresholdImage_
|
| 23 |
+
|
| 24 |
+
class OCRModel:
|
| 25 |
+
def __init__(self, encoder_model, decoder_model, trained_model_path):
|
| 26 |
+
# Load processor and model
|
| 27 |
+
self.feature_extractor = AutoFeatureExtractor.from_pretrained(encoder_model)
|
| 28 |
+
self.decoder_tokenizer = AutoTokenizer.from_pretrained(decoder_model)
|
| 29 |
+
self.processor = TrOCRProcessor(feature_extractor=self.feature_extractor, tokenizer=self.decoder_tokenizer)
|
| 30 |
+
self.model = VisionEncoderDecoderModel.from_pretrained(trained_model_path)
|
| 31 |
+
|
| 32 |
+
# Configure model settings
|
| 33 |
+
self.model.config.decoder_start_token_id = self.processor.tokenizer.cls_token_id
|
| 34 |
+
self.model.config.pad_token_id = self.processor.tokenizer.pad_token_id
|
| 35 |
+
self.model.config.vocab_size = self.model.config.decoder.vocab_size
|
| 36 |
+
self.model.config.eos_token_id = self.processor.tokenizer.sep_token_id
|
| 37 |
+
self.model.config.max_length = 64
|
| 38 |
+
self.model.config.early_stopping = True
|
| 39 |
+
self.model.config.no_repeat_ngram_size = 3
|
| 40 |
+
self.model.config.length_penalty = 2.0
|
| 41 |
+
self.model.config.num_beams = 4
|
| 42 |
+
|
| 43 |
+
def read_and_show(self, image_path):
|
| 44 |
+
"""
|
| 45 |
+
Reads an image from the provided path and converts it to RGB.
|
| 46 |
+
:param image_path: String, path to the input image.
|
| 47 |
+
:return: PIL Image object
|
| 48 |
+
"""
|
| 49 |
+
image = Image.open(image_path).convert('RGB')
|
| 50 |
+
return image
|
| 51 |
+
|
| 52 |
+
def ocr(self, image):
|
| 53 |
+
"""
|
| 54 |
+
Performs OCR on the given image.
|
| 55 |
+
:param image: PIL Image object.
|
| 56 |
+
:return: Extracted text from the image.
|
| 57 |
+
"""
|
| 58 |
+
# Preprocess the image
|
| 59 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 60 |
+
pixel_values = self.processor(image, return_tensors='pt').pixel_values.to(device)
|
| 61 |
+
|
| 62 |
+
# Generate text
|
| 63 |
+
generated_ids = self.model.generate(pixel_values)
|
| 64 |
+
generated_text = self.processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 65 |
+
return generated_text
|
| 66 |
+
|
| 67 |
+
# Initialize the OCR model
|
| 68 |
+
ocr_model = OCRModel(encoder_model="google/vit-base-patch16-224-in21k",
|
| 69 |
+
decoder_model="surajp/RoBERTa-hindi-guj-san",
|
| 70 |
+
trained_model_path="./model/") #'sabaridsnfuji/Tamil_Offline_Handwritten_OCR')#"./model/")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def main(image_path):
|
| 75 |
+
# Process the image and extract text
|
| 76 |
+
image = ocr_model.read_and_show(image_path)
|
| 77 |
+
text = ocr_model.ocr(image)
|
| 78 |
+
|
| 79 |
+
return image, text
|
| 80 |
+
|
| 81 |
+
# Gradio Interface function
|
| 82 |
+
def gradio_interface(image):
|
| 83 |
+
# Save the uploaded image locally
|
| 84 |
+
image_path = "uploaded_image.png"
|
| 85 |
+
image.save(image_path)
|
| 86 |
+
|
| 87 |
+
# Call the main function to process the image and get the result
|
| 88 |
+
processed_image, result_text = main(image_path)
|
| 89 |
+
|
| 90 |
+
return processed_image, result_text
|
| 91 |
+
|
| 92 |
+
# Sample images for demonstration (make sure these image paths exist)
|
| 93 |
+
sample_images = [
|
| 94 |
+
"./sample/16.jpg", # replace with actual image paths
|
| 95 |
+
"./sample/20.jpg", # replace with actual image paths
|
| 96 |
+
"./sample/21.jpg", # replace with actual image paths
|
| 97 |
+
"./sample/31.jpg", # replace with actual image paths
|
| 98 |
+
"./sample/35.jpg", # replace with actual image paths
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
# Ensure sample images directory exists
|
| 102 |
+
os.makedirs("samples", exist_ok=True)
|
| 103 |
+
# Save some dummy sample images if they don't exist (you should replace these with actual images)
|
| 104 |
+
for i, sample in enumerate(sample_images):
|
| 105 |
+
if not os.path.exists(sample):
|
| 106 |
+
img = Image.new("RGB", (224, 224), color=(i * 50, i * 50, i * 50))
|
| 107 |
+
img.save(sample)
|
| 108 |
+
|
| 109 |
+
# Gradio UI setup with examples
|
| 110 |
+
gr_interface = gr.Interface(
|
| 111 |
+
fn=gradio_interface,
|
| 112 |
+
inputs=gr.Image(type="pil"), # Updated to gr.Image
|
| 113 |
+
outputs=[gr.Image(type="pil"), gr.Textbox()], # Updated to gr.Image and gr.Textbox
|
| 114 |
+
title="Hindi Handwritten OCR Recognition",
|
| 115 |
+
description="Upload a cropped image containing a word, or use the sample images below to recognize the text. This is a word recognition model. Currently, text detection is not supported.",
|
| 116 |
+
examples=sample_images # Add the examples here
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Launch the Gradio interface
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
gr_interface.launch()
|
model/config.json
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 7 |
+
"_attn_implementation_autoset": true,
|
| 8 |
+
"_name_or_path": "surajp/RoBERTa-hindi-guj-san",
|
| 9 |
+
"add_cross_attention": true,
|
| 10 |
+
"architectures": [
|
| 11 |
+
"RobertaForMaskedLM"
|
| 12 |
+
],
|
| 13 |
+
"attention_probs_dropout_prob": 0.1,
|
| 14 |
+
"bad_words_ids": null,
|
| 15 |
+
"begin_suppress_tokens": null,
|
| 16 |
+
"bos_token_id": 0,
|
| 17 |
+
"chunk_size_feed_forward": 0,
|
| 18 |
+
"classifier_dropout": null,
|
| 19 |
+
"cross_attention_hidden_size": null,
|
| 20 |
+
"decoder_start_token_id": null,
|
| 21 |
+
"diversity_penalty": 0.0,
|
| 22 |
+
"do_sample": false,
|
| 23 |
+
"early_stopping": false,
|
| 24 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 25 |
+
"eos_token_id": 2,
|
| 26 |
+
"exponential_decay_length_penalty": null,
|
| 27 |
+
"finetuning_task": null,
|
| 28 |
+
"forced_bos_token_id": null,
|
| 29 |
+
"forced_eos_token_id": null,
|
| 30 |
+
"gradient_checkpointing": false,
|
| 31 |
+
"hidden_act": "gelu",
|
| 32 |
+
"hidden_dropout_prob": 0.1,
|
| 33 |
+
"hidden_size": 768,
|
| 34 |
+
"id2label": {
|
| 35 |
+
"0": "LABEL_0",
|
| 36 |
+
"1": "LABEL_1"
|
| 37 |
+
},
|
| 38 |
+
"initializer_range": 0.02,
|
| 39 |
+
"intermediate_size": 3072,
|
| 40 |
+
"is_decoder": true,
|
| 41 |
+
"is_encoder_decoder": false,
|
| 42 |
+
"label2id": {
|
| 43 |
+
"LABEL_0": 0,
|
| 44 |
+
"LABEL_1": 1
|
| 45 |
+
},
|
| 46 |
+
"layer_norm_eps": 1e-05,
|
| 47 |
+
"length_penalty": 1.0,
|
| 48 |
+
"max_length": 20,
|
| 49 |
+
"max_position_embeddings": 514,
|
| 50 |
+
"min_length": 0,
|
| 51 |
+
"model_type": "roberta",
|
| 52 |
+
"no_repeat_ngram_size": 0,
|
| 53 |
+
"num_attention_heads": 12,
|
| 54 |
+
"num_beam_groups": 1,
|
| 55 |
+
"num_beams": 1,
|
| 56 |
+
"num_hidden_layers": 6,
|
| 57 |
+
"num_return_sequences": 1,
|
| 58 |
+
"output_attentions": false,
|
| 59 |
+
"output_hidden_states": false,
|
| 60 |
+
"output_scores": false,
|
| 61 |
+
"pad_token_id": 1,
|
| 62 |
+
"position_embedding_type": "absolute",
|
| 63 |
+
"prefix": null,
|
| 64 |
+
"problem_type": null,
|
| 65 |
+
"pruned_heads": {},
|
| 66 |
+
"remove_invalid_values": false,
|
| 67 |
+
"repetition_penalty": 1.0,
|
| 68 |
+
"return_dict": true,
|
| 69 |
+
"return_dict_in_generate": false,
|
| 70 |
+
"sep_token_id": null,
|
| 71 |
+
"suppress_tokens": null,
|
| 72 |
+
"task_specific_params": null,
|
| 73 |
+
"temperature": 1.0,
|
| 74 |
+
"tf_legacy_loss": false,
|
| 75 |
+
"tie_encoder_decoder": false,
|
| 76 |
+
"tie_word_embeddings": true,
|
| 77 |
+
"tokenizer_class": null,
|
| 78 |
+
"top_k": 50,
|
| 79 |
+
"top_p": 1.0,
|
| 80 |
+
"torch_dtype": null,
|
| 81 |
+
"torchscript": false,
|
| 82 |
+
"type_vocab_size": 1,
|
| 83 |
+
"typical_p": 1.0,
|
| 84 |
+
"use_bfloat16": false,
|
| 85 |
+
"use_cache": true,
|
| 86 |
+
"vocab_size": 30522
|
| 87 |
+
},
|
| 88 |
+
"decoder_start_token_id": 0,
|
| 89 |
+
"early_stopping": null,
|
| 90 |
+
"encoder": {
|
| 91 |
+
"_attn_implementation_autoset": true,
|
| 92 |
+
"_name_or_path": "google/vit-base-patch16-224-in21k",
|
| 93 |
+
"add_cross_attention": false,
|
| 94 |
+
"architectures": [
|
| 95 |
+
"ViTModel"
|
| 96 |
+
],
|
| 97 |
+
"attention_probs_dropout_prob": 0.0,
|
| 98 |
+
"bad_words_ids": null,
|
| 99 |
+
"begin_suppress_tokens": null,
|
| 100 |
+
"bos_token_id": null,
|
| 101 |
+
"chunk_size_feed_forward": 0,
|
| 102 |
+
"cross_attention_hidden_size": null,
|
| 103 |
+
"decoder_start_token_id": null,
|
| 104 |
+
"diversity_penalty": 0.0,
|
| 105 |
+
"do_sample": false,
|
| 106 |
+
"early_stopping": false,
|
| 107 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 108 |
+
"encoder_stride": 16,
|
| 109 |
+
"eos_token_id": null,
|
| 110 |
+
"exponential_decay_length_penalty": null,
|
| 111 |
+
"finetuning_task": null,
|
| 112 |
+
"forced_bos_token_id": null,
|
| 113 |
+
"forced_eos_token_id": null,
|
| 114 |
+
"hidden_act": "gelu",
|
| 115 |
+
"hidden_dropout_prob": 0.0,
|
| 116 |
+
"hidden_size": 768,
|
| 117 |
+
"id2label": {
|
| 118 |
+
"0": "LABEL_0",
|
| 119 |
+
"1": "LABEL_1"
|
| 120 |
+
},
|
| 121 |
+
"image_size": 224,
|
| 122 |
+
"initializer_range": 0.02,
|
| 123 |
+
"intermediate_size": 3072,
|
| 124 |
+
"is_decoder": false,
|
| 125 |
+
"is_encoder_decoder": false,
|
| 126 |
+
"label2id": {
|
| 127 |
+
"LABEL_0": 0,
|
| 128 |
+
"LABEL_1": 1
|
| 129 |
+
},
|
| 130 |
+
"layer_norm_eps": 1e-12,
|
| 131 |
+
"length_penalty": 1.0,
|
| 132 |
+
"max_length": 20,
|
| 133 |
+
"min_length": 0,
|
| 134 |
+
"model_type": "vit",
|
| 135 |
+
"no_repeat_ngram_size": 0,
|
| 136 |
+
"num_attention_heads": 12,
|
| 137 |
+
"num_beam_groups": 1,
|
| 138 |
+
"num_beams": 1,
|
| 139 |
+
"num_channels": 3,
|
| 140 |
+
"num_hidden_layers": 12,
|
| 141 |
+
"num_return_sequences": 1,
|
| 142 |
+
"output_attentions": false,
|
| 143 |
+
"output_hidden_states": false,
|
| 144 |
+
"output_scores": false,
|
| 145 |
+
"pad_token_id": null,
|
| 146 |
+
"patch_size": 16,
|
| 147 |
+
"prefix": null,
|
| 148 |
+
"problem_type": null,
|
| 149 |
+
"pruned_heads": {},
|
| 150 |
+
"qkv_bias": true,
|
| 151 |
+
"remove_invalid_values": false,
|
| 152 |
+
"repetition_penalty": 1.0,
|
| 153 |
+
"return_dict": true,
|
| 154 |
+
"return_dict_in_generate": false,
|
| 155 |
+
"sep_token_id": null,
|
| 156 |
+
"suppress_tokens": null,
|
| 157 |
+
"task_specific_params": null,
|
| 158 |
+
"temperature": 1.0,
|
| 159 |
+
"tf_legacy_loss": false,
|
| 160 |
+
"tie_encoder_decoder": false,
|
| 161 |
+
"tie_word_embeddings": true,
|
| 162 |
+
"tokenizer_class": null,
|
| 163 |
+
"top_k": 50,
|
| 164 |
+
"top_p": 1.0,
|
| 165 |
+
"torch_dtype": null,
|
| 166 |
+
"torchscript": false,
|
| 167 |
+
"typical_p": 1.0,
|
| 168 |
+
"use_bfloat16": false
|
| 169 |
+
},
|
| 170 |
+
"eos_token_id": 2,
|
| 171 |
+
"is_encoder_decoder": true,
|
| 172 |
+
"length_penalty": null,
|
| 173 |
+
"max_length": null,
|
| 174 |
+
"model_type": "vision-encoder-decoder",
|
| 175 |
+
"no_repeat_ngram_size": null,
|
| 176 |
+
"num_beams": null,
|
| 177 |
+
"pad_token_id": 1,
|
| 178 |
+
"tie_word_embeddings": false,
|
| 179 |
+
"torch_dtype": "float32",
|
| 180 |
+
"transformers_version": "4.46.3",
|
| 181 |
+
"vocab_size": 30522
|
| 182 |
+
}
|
model/generation_config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token_id": 0,
|
| 3 |
+
"decoder_start_token_id": 0,
|
| 4 |
+
"early_stopping": true,
|
| 5 |
+
"eos_token_id": 2,
|
| 6 |
+
"length_penalty": 2.0,
|
| 7 |
+
"max_length": 64,
|
| 8 |
+
"no_repeat_ngram_size": 3,
|
| 9 |
+
"num_beams": 4,
|
| 10 |
+
"pad_token_id": 1,
|
| 11 |
+
"transformers_version": "4.46.3"
|
| 12 |
+
}
|
model/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aff44c11a9f00953a66d58e95e327f7883df105aea2c2140ca80583dddd818d2
|
| 3 |
+
size 670287760
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
datasets
|
| 4 |
+
Pillow
|