Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,19 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import cv2
|
4 |
from pytesseract import pytesseract
|
5 |
from transformers import AutoModel, AutoTokenizer
|
6 |
import gradio as gr
|
7 |
|
8 |
# Model and Tesseract Configuration
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
def perform_ocr(img, language, model_eng, tesseract_config):
|
20 |
img_path = "/tmp/uploaded_image.png"
|
21 |
img.save(img_path)
|
22 |
|
@@ -33,8 +30,8 @@ def perform_ocr(img, language, model_eng, tesseract_config):
|
|
33 |
return res_eng, res_hin
|
34 |
|
35 |
# Keyword Search Functionality
|
36 |
-
def ocr_and_search(image, language, keyword
|
37 |
-
english_text, hindi_text = perform_ocr(image, language
|
38 |
|
39 |
extracted_english = f"Extracted English Text:\n{english_text}" if english_text else "No English text extracted."
|
40 |
extracted_hindi = f"Extracted Hindi Text:\n{hindi_text}" if hindi_text else "No Hindi text extracted."
|
@@ -53,25 +50,18 @@ def ocr_and_search(image, language, keyword, model_eng, tesseract_config):
|
|
53 |
return extracted_english, extracted_hindi, search_output
|
54 |
|
55 |
# Gradio Interface Setup
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
return app
|
70 |
-
|
71 |
-
def main():
|
72 |
-
tokenizer_eng, model_eng, tesseract_config = load_models()
|
73 |
-
app = create_interface(model_eng, tesseract_config)
|
74 |
-
app.launch()
|
75 |
-
|
76 |
if __name__ == "__main__":
|
77 |
-
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
from pytesseract import pytesseract
|
3 |
from transformers import AutoModel, AutoTokenizer
|
4 |
import gradio as gr
|
5 |
|
6 |
# Model and Tesseract Configuration
|
7 |
+
# Load GOT2 model for English text and configure Tesseract for Hindi text
|
8 |
+
tokenizer_eng = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
9 |
+
model_eng = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, device_map='cpu').eval()
|
10 |
+
|
11 |
+
# Define Tesseract path and configuration for Hindi
|
12 |
+
pytesseract.tesseract_cmd = '/usr/bin/tesseract'
|
13 |
+
tesseract_config = '--oem 3 --psm 6 -l hin'
|
14 |
+
|
15 |
+
# Perform OCR function
|
16 |
+
def perform_ocr(img, language):
|
|
|
17 |
img_path = "/tmp/uploaded_image.png"
|
18 |
img.save(img_path)
|
19 |
|
|
|
30 |
return res_eng, res_hin
|
31 |
|
32 |
# Keyword Search Functionality
|
33 |
+
def ocr_and_search(image, language, keyword):
|
34 |
+
english_text, hindi_text = perform_ocr(image, language)
|
35 |
|
36 |
extracted_english = f"Extracted English Text:\n{english_text}" if english_text else "No English text extracted."
|
37 |
extracted_hindi = f"Extracted Hindi Text:\n{hindi_text}" if hindi_text else "No Hindi text extracted."
|
|
|
50 |
return extracted_english, extracted_hindi, search_output
|
51 |
|
52 |
# Gradio Interface Setup
|
53 |
+
with gr.Blocks() as app:
|
54 |
+
gr.Markdown("### OCR Application")
|
55 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
56 |
+
language_selection = gr.Radio(choices=["English", "Hindi", "Both"], label="Select Language")
|
57 |
+
keyword_input = gr.Textbox(placeholder="Enter keyword to search", label="Keyword Search")
|
58 |
+
output_english = gr.Textbox(label="Extracted English Text", interactive=False)
|
59 |
+
output_hindi = gr.Textbox(label="Extracted Hindi Text", interactive=False)
|
60 |
+
output_search = gr.Textbox(label="Search Results", interactive=False)
|
61 |
+
|
62 |
+
submit_button = gr.Button("Submit")
|
63 |
+
submit_button.click(fn=ocr_and_search, inputs=[image_input, language_selection, keyword_input], outputs=[output_english, output_hindi, output_search])
|
64 |
+
|
65 |
+
# Application Launch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
if __name__ == "__main__":
|
67 |
+
app.launch()
|