Keemoz0 commited on
Commit
32ccca8
·
1 Parent(s): 0fc8569

reaplce tesseract with easyocr

Browse files

because tesseract assumed not working in gradio

Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
  from PIL import Image
4
  import torch
5
- import pytesseract
6
  from transformers import AutoImageProcessor, AutoModelForObjectDetection
7
 
8
  # Load the processor and model for table structure recognition
@@ -13,6 +13,9 @@ model = AutoModelForObjectDetection.from_pretrained("microsoft/table-transformer
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  model.to(device)
15
 
 
 
 
16
  # Define the inference and OCR function
17
  def predict(image):
18
  # Preprocess the input image
@@ -53,8 +56,8 @@ def predict(image):
53
  # Crop the image to the bounding box area
54
  cropped_image = image.crop((left, top, right, bottom))
55
 
56
- # Perform OCR on the cropped image
57
- ocr_text = pytesseract.image_to_string(cropped_image)
58
 
59
  # Append OCR result for this box
60
  ocr_results.append({
@@ -74,4 +77,3 @@ interface = gr.Interface(
74
 
75
  # Launch the Gradio app
76
  interface.launch()
77
- #recheck gradio bugging
 
2
  from huggingface_hub import hf_hub_download
3
  from PIL import Image
4
  import torch
5
+ import easyocr
6
  from transformers import AutoImageProcessor, AutoModelForObjectDetection
7
 
8
  # Load the processor and model for table structure recognition
 
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  model.to(device)
15
 
16
+ # Initialize EasyOCR Reader
17
+ reader = easyocr.Reader(['en']) # You can specify the language (e.g., 'en' for English)
18
+
19
  # Define the inference and OCR function
20
  def predict(image):
21
  # Preprocess the input image
 
56
  # Crop the image to the bounding box area
57
  cropped_image = image.crop((left, top, right, bottom))
58
 
59
+ # Perform OCR using EasyOCR
60
+ ocr_text = reader.readtext(cropped_image, detail=0) # detail=0 returns just the text
61
 
62
  # Append OCR result for this box
63
  ocr_results.append({
 
77
 
78
  # Launch the Gradio app
79
  interface.launch()