yashbyname commited on
Commit
41eaee6
·
verified ·
1 Parent(s): c2e789f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -1,37 +1,35 @@
1
  import gradio as gr
2
  import torch
3
  import pytesseract
 
 
4
  from transformers import AutoTokenizer, AutoModel
5
 
6
- # Set Tesseract executable path
7
- pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'
8
 
9
  # Load the tokenizer and model
10
  tokenizer_eng = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
11
  model_eng = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True).eval()
12
 
13
  def perform_ocr(image, language):
14
- # Convert the Gradio image input to the format suitable for pytesseract
15
- img_cv = image # Assuming image is already in the correct format
16
-
17
- if language == "English":
18
- # Perform OCR using the model for English
19
- res_eng = model_eng.chat(tokenizer_eng, img_cv, ocr_type='ocr')
20
- return res_eng # Return results for English
21
- elif language == "Hindi":
22
- # Perform OCR using pytesseract for Hindi
23
- res_hin = pytesseract.image_to_string(img_cv, lang='hin', config='--psm 6')
24
- return res_hin # Return results for Hindi
25
- else:
26
- return "Unsupported language selected."
27
 
28
  def ocr_and_search(image, language):
29
  # Call the perform_ocr function
30
- extracted_text = perform_ocr(image, language)
31
- # You may also want to implement any searching functionality here
32
- # ...
33
 
34
- return extracted_text # Return the OCR result for the selected language
35
 
36
  # Create Gradio interface
37
  iface = gr.Interface(
 
1
  import gradio as gr
2
  import torch
3
  import pytesseract
4
+ import cv2
5
+ import tempfile
6
  from transformers import AutoTokenizer, AutoModel
7
 
8
+ pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract' # Update this if necessary
 
9
 
10
  # Load the tokenizer and model
11
  tokenizer_eng = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
12
  model_eng = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True).eval()
13
 
14
  def perform_ocr(image, language):
15
+ # Save the NumPy array as an image file temporarily
16
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
17
+ temp_filename = temp_file.name
18
+ cv2.imwrite(temp_filename, image)
19
+
20
+ # Perform OCR for English
21
+ res_eng = model_eng.chat(tokenizer_eng, temp_filename, ocr_type='ocr')
22
+
23
+ # Clean up temporary file if needed
24
+ # os.remove(temp_filename)
25
+
26
+ return res_eng # Return results for English
 
27
 
28
  def ocr_and_search(image, language):
29
  # Call the perform_ocr function
30
+ english_text = perform_ocr(image, language)
 
 
31
 
32
+ return english_text # Return the OCR result for English
33
 
34
  # Create Gradio interface
35
  iface = gr.Interface(