import pytesseract from PIL import Image import pandas as pd import os import gradio as gr import numpy as np import gradio as gr def ocr_df_using_pytesseract(image): #pytesseract.pytesseract.tesseract_cmd =r"C:\Users\amold\Desktop\Upwork\pdf to image and pytesseract\tesseact_exe\Tesseract-OCR\tesseract.exe" pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract' #image = Image.open(example['image_path']) width, height = image.size # apply ocr to the image ocr_df = pytesseract.image_to_data(image, output_type='data.frame') float_cols = ocr_df.select_dtypes('float').columns ocr_df = ocr_df.dropna().reset_index(drop=True) ocr_df[float_cols] = ocr_df[float_cols].round(0).astype(int) ocr_df = ocr_df.replace(r'^\s*$', np.nan, regex=True) ocr_df = ocr_df.dropna().reset_index(drop=True) ocr_df ocr_df['X1']=ocr_df['left'] ocr_df['Y1']=ocr_df['top'] ocr_df['X2']= ocr_df['left'] + ocr_df['width'] ocr_df['Y2']= ocr_df['top'] + ocr_df['height'] return ocr_df def image_to_text(image): ocr_df= ocr_df_using_pytesseract(image) grouped_text = ocr_df.groupby(['block_num', 'line_num'])['text'].agg(' '.join).reset_index() # sort the text by line numbers within each block grouped_text = grouped_text.sort_values(['block_num', 'line_num']) # join the text by blocks and add newlines result = '' for i, row in grouped_text.iterrows(): if i > 0 and row['block_num'] != grouped_text.loc[i-1, 'block_num']: result += '\n\n' result += row['text'].rstrip() + '\n' return result demo = gr.Interface(fn=image_to_text, inputs= gr.Image(type="pil"), outputs=["text"], title="Menu image to text", description= "Upload Menu image here") demo.launch(share=False)