radames HF staff commited on
Commit
b9e018b
1 Parent(s): e871a21
Files changed (1) hide show
  1. app.py +34 -6
app.py CHANGED
@@ -1,15 +1,20 @@
1
- try:
2
- import Image
3
- except ImportError:
4
- from PIL import Image
5
  import pytesseract
 
 
 
 
 
 
 
 
6
 
7
  # If you don't have tesseract executable in your PATH, include the following:
8
  # pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
9
  # Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
10
 
11
  # Simple image to string
12
- print(pytesseract.image_to_string(Image.open('eurotext.png')))
13
 
14
  # # French text image to string
15
  # print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))
@@ -21,4 +26,27 @@ print(pytesseract.image_to_string(Image.open('eurotext.png')))
21
  # print(pytesseract.image_to_data(Image.open('test.png')))
22
 
23
  # # Get information about orientation and script detection
24
- # print(pytesseract.image_to_osd(Image.open('test.png'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
 
 
 
2
  import pytesseract
3
+ import gradio as gr
4
+ import os
5
+ langs = []
6
+
7
+ choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
8
+
9
+ blocks = gr.Blocks()
10
+
11
 
12
  # If you don't have tesseract executable in your PATH, include the following:
13
  # pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
14
  # Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
15
 
16
  # Simple image to string
17
+ # print(pytesseract.image_to_string(Image.open('eurotext.png')))
18
 
19
  # # French text image to string
20
  # print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))
 
26
  # print(pytesseract.image_to_data(Image.open('test.png')))
27
 
28
  # # Get information about orientation and script detection
29
+ # print(pytesseract.image_to_osd(Image.open('test.png'))
30
+
31
+
32
+ def run(image, lang=None):
33
+ result = pytesseract.image_to_string(
34
+ image, lang=None if lang == [] else lang)
35
+ return result
36
+
37
+
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("## Hello pytesseract!")
40
+ with gr.Row():
41
+ with gr.Column():
42
+ image_in = gr.Image(type="pil")
43
+ lang = gr.Dropdown(choices)
44
+ btn = gr.Button("Run")
45
+ with gr.Column():
46
+ text_out = gr.TextArea()
47
+
48
+ examples = gr.Examples([["./eurotext.png", None]], fn=run, inputs=[
49
+ image_in, lang], outputs=[text_out], cache_examples=False)
50
+ btn.click(fn=run, inputs=[image_in, lang], outputs=[text_out])
51
+
52
+ demo.launch()