Genzo1010 commited on
Commit
b613d69
·
verified ·
1 Parent(s): 85b7f8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -43
app.py CHANGED
@@ -16,8 +16,7 @@ import datasets
16
  from datasets import load_dataset, Image
17
  from PIL import Image
18
  from paddleocr import PaddleOCR
19
- from save_data import flag
20
-
21
  """
22
  Paddle OCR
23
  """
@@ -63,58 +62,51 @@ def ocr_with_easy(img):
63
  bounds = reader.readtext('image.png',paragraph="False",detail = 0)
64
  bounds = ''.join(bounds)
65
  return bounds
66
-
67
- """
68
- Generate OCR
69
- """
70
- def generate_ocr(Method,img):
71
-
72
  text_output = ''
73
- if (img).any():
74
- add_csv = []
75
- image_id = 1
76
- print("Method___________________",Method)
77
- if Method == 'EasyOCR':
78
- text_output = ocr_with_easy(img)
79
- if Method == 'KerasOCR':
80
- text_output = ocr_with_keras(img)
81
- if Method == 'PaddleOCR':
82
- text_output = ocr_with_paddle(img)
83
-
84
- try:
85
- flag(Method,text_output,img)
86
- except Exception as e:
87
- print(e)
88
- return text_output
89
  else:
90
- raise gr.Error("Please upload an image!!!!")
91
-
92
- # except Exception as e:
93
- # print("Error in ocr generation ==>",e)
94
- # text_output = "Something went wrong"
95
- # return text_output
96
 
 
97
 
98
- """
99
- Create user interface for OCR demo
100
- """
 
 
 
 
 
 
 
 
 
101
 
102
- # image = gr.Image(shape=(300, 300))
103
- image = gr.Image()
104
- method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR"],value="PaddleOCR")
105
  output = gr.Textbox(label="Output")
106
 
107
  demo = gr.Interface(
108
  generate_ocr,
109
- [method,image],
110
  output,
111
  title="Optical Character Recognition",
112
  css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
113
- article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
114
- <a href="mailto:[email protected]" target="_blank">[email protected]</a>
115
- <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
116
-
117
-
118
  )
119
- # demo.launch(enable_queue = False)
120
  demo.launch(show_error=True)
 
16
  from datasets import load_dataset, Image
17
  from PIL import Image
18
  from paddleocr import PaddleOCR
19
+
 
20
  """
21
  Paddle OCR
22
  """
 
62
  bounds = reader.readtext('image.png',paragraph="False",detail = 0)
63
  bounds = ''.join(bounds)
64
  return bounds
65
+
66
+ def generate_ocr(Method, file):
 
 
 
 
67
  text_output = ''
68
+ if isinstance(file, bytes): # Handle file uploaded as bytes
69
+ file = io.BytesIO(file)
70
+
71
+ if file.name.endswith('.pdf'):
72
+ # Convert PDF to images
73
+ images = convert_from_path(file)
74
+ for img in images:
75
+ img_np = np.array(img)
76
+ text_output += generate_text_from_image(Method, img_np) + "\n"
 
 
 
 
 
 
 
77
  else:
78
+ # Handle image file
79
+ img_np = np.array(Image.open(file))
80
+ text_output = generate_text_from_image(Method, img_np)
 
 
 
81
 
82
+ return text_output
83
 
84
+ def generate_text_from_image(Method, img):
85
+ text_output = ''
86
+ if Method == 'EasyOCR':
87
+ text_output = ocr_with_easy(img)
88
+ elif Method == 'KerasOCR':
89
+ text_output = ocr_with_keras(img)
90
+ elif Method == 'PaddleOCR':
91
+ text_output = ocr_with_paddle(img)
92
+ return text_output
93
+
94
+
95
+ import gradio as gr
96
 
97
+ image_or_pdf = gr.File(label="Upload an image or PDF")
98
+ method = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")
 
99
  output = gr.Textbox(label="Output")
100
 
101
  demo = gr.Interface(
102
  generate_ocr,
103
+ [method, image_or_pdf],
104
  output,
105
  title="Optical Character Recognition",
106
  css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
107
+ article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
108
+ <a href="mailto:[email protected]" target="_blank">[email protected]</a>
109
+ <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
 
 
110
  )
111
+
112
  demo.launch(show_error=True)