|
import gradio as gr |
|
import torch |
|
import pytesseract |
|
from transformers import AutoTokenizer, AutoModel |
|
|
|
|
|
pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract' |
|
|
|
|
|
tokenizer_eng = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True) |
|
model_eng = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True).eval() |
|
|
|
def perform_ocr(image, language): |
|
|
|
img_cv = image |
|
|
|
if language == "English": |
|
|
|
res_eng = model_eng.chat(tokenizer_eng, img_cv, ocr_type='ocr') |
|
return res_eng |
|
elif language == "Hindi": |
|
|
|
res_hin = pytesseract.image_to_string(img_cv, lang='hin', config='--psm 6') |
|
return res_hin |
|
else: |
|
return "Unsupported language selected." |
|
|
|
def ocr_and_search(image, language): |
|
|
|
extracted_text = perform_ocr(image, language) |
|
|
|
|
|
|
|
return extracted_text |
|
|
|
|
|
iface = gr.Interface( |
|
fn=ocr_and_search, |
|
inputs=[ |
|
gr.Image(type="numpy", label="Upload Image"), |
|
gr.Dropdown(choices=["English", "Hindi"], label="Select Language") |
|
], |
|
outputs=gr.Textbox(label="Extracted Text"), |
|
title="OCR Application", |
|
description="Upload an image to extract text using OCR." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|