ImranzamanML commited on
Commit
562ff76
·
verified ·
1 Parent(s): 831ca76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -73
app.py CHANGED
@@ -1,34 +1,18 @@
1
  import os
2
  import numpy as np
3
  import json
4
- import shutil
5
  import requests
6
- import re as r
7
  from urllib.request import urlopen
8
  from datetime import datetime
9
  import gradio as gr
10
- import tensorflow as tf
11
- import keras_ocr
12
- import cv2
13
- import csv
14
- import pandas as pd
15
- import huggingface_hub
16
- from huggingface_hub import Repository, upload_file
17
- import scipy.ndimage.interpolation as inter
18
- import easyocr
19
- from datasets import load_dataset, Image
20
- from PIL import Image as PILImage
21
  from paddleocr import PaddleOCR
22
- import pytesseract
23
- import torch
24
  import spaces
25
 
26
  # Global Variables
27
  HF_TOKEN = os.environ.get("HF_TOKEN")
28
- DATASET_NAME = "image_to_text_ocr"
29
  DATASET_REPO_URL = "https://huggingface.co/ImranzamanML/image_to_text_ocr"
30
- DATA_FILENAME = "ocr_data.csv"
31
- DATA_FILE_PATH = os.path.join("ocr_data", DATA_FILENAME)
32
  DATASET_REPO_ID = "ImranzamanML/image_to_text_ocr"
33
  REPOSITORY_DIR = "data"
34
  LOCAL_DIR = 'data_local'
@@ -38,48 +22,15 @@ os.makedirs(LOCAL_DIR, exist_ok=True)
38
  OCR using PaddleOCR
39
  """
40
  @spaces.GPU
41
- def paddle_ocr_processor(image):
42
  final_text = ''
43
  ocr = PaddleOCR(use_gpu=True, lang='en', use_angle_cls=True)
44
  result = ocr.ocr(image)
45
- for i in range(len(result[0])):
46
- text = result[0][i][1][0]
47
  final_text += ' ' + text
48
  return final_text
49
 
50
- """
51
- OCR using Keras OCR
52
- """
53
- @spaces.GPU
54
- def keras_ocr_processor(image):
55
- output_text = ''
56
- pipeline = keras_ocr.pipeline.Pipeline()
57
- images = [keras_ocr.tools.read(image)]
58
- predictions = pipeline.recognize(images)
59
- first_prediction = predictions[0]
60
- for text, box in first_prediction:
61
- output_text += ' ' + text
62
- return output_text
63
-
64
- """
65
- OCR using EasyOCR
66
- """
67
- def convert_to_grayscale(image):
68
- return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
69
-
70
- def apply_thresholding(src):
71
- return cv2.threshold(src, 127, 255, cv2.THRESH_TOZERO)[1]
72
-
73
- @spaces.GPU
74
- def easy_ocr_processor(image):
75
- gray_image = convert_to_grayscale(image)
76
- apply_thresholding(gray_image)
77
- cv2.imwrite('processed_image.png', gray_image)
78
- reader = easyocr.Reader(['th', 'en'])
79
- detected_text = reader.readtext('processed_image.png', paragraph="False", detail=0)
80
- detected_text = ''.join(detected_text)
81
- return detected_text
82
-
83
  """
84
  Utility Functions
85
  """
@@ -90,7 +41,7 @@ def save_json(data, filepath):
90
  def get_ip_address():
91
  try:
92
  response = str(urlopen('http://checkip.dyndns.com/').read())
93
- return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(response).group(1)
94
  except Exception as e:
95
  print("Error while getting IP address -->", e)
96
  return ''
@@ -170,12 +121,7 @@ OCR Generation
170
  def generate_ocr_text(method, image):
171
  text_output = ''
172
  if image.any():
173
- if method == 'EasyOCR':
174
- text_output = easy_ocr_processor(image)
175
- elif method == 'KerasOCR':
176
- text_output = keras_ocr_processor(image)
177
- elif method == 'PaddleOCR':
178
- text_output = paddle_ocr_processor(image)
179
 
180
  try:
181
  log_ocr_data(method, text_output, image)
@@ -188,22 +134,46 @@ def generate_ocr_text(method, image):
188
  """
189
  Create user interface for OCR demo
190
  """
191
- image_input = gr.Image(label="Upload Image")
192
- method_input = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR", label="Select OCR Method")
193
- output_textbox = gr.Textbox(label="Recognized Text")
194
 
195
  demo = gr.Interface(
196
  fn=generate_ocr_text,
197
- inputs=[method_input, image_input],
198
  outputs=output_textbox,
199
- title="Enhanced OCR Demo",
200
- description="Choose an OCR method and upload an image to extract text.",
201
- theme="huggingface",
202
  css="""
203
- .gradio-container {background-color: #f5f5f5; font-family: Arial, sans-serif;}
204
- #method_input {background-color: #FFC107; font-size: 18px; padding: 10px;}
205
- #output_textbox {font-size: 16px; color: #333;}
206
- """
207
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  demo.launch()
 
1
  import os
2
  import numpy as np
3
  import json
 
4
  import requests
 
5
  from urllib.request import urlopen
6
  from datetime import datetime
7
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
8
  from paddleocr import PaddleOCR
9
+ from PIL import Image as PILImage
10
+ from huggingface_hub import Repository, upload_file
11
  import spaces
12
 
13
  # Global Variables
14
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
15
  DATASET_REPO_URL = "https://huggingface.co/ImranzamanML/image_to_text_ocr"
 
 
16
  DATASET_REPO_ID = "ImranzamanML/image_to_text_ocr"
17
  REPOSITORY_DIR = "data"
18
  LOCAL_DIR = 'data_local'
 
22
  OCR using PaddleOCR
23
  """
24
  @spaces.GPU
25
+ def process_image_with_paddleocr(image):
26
  final_text = ''
27
  ocr = PaddleOCR(use_gpu=True, lang='en', use_angle_cls=True)
28
  result = ocr.ocr(image)
29
+ for line in result[0]:
30
+ text = line[1][0]
31
  final_text += ' ' + text
32
  return final_text
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  """
35
  Utility Functions
36
  """
 
41
  def get_ip_address():
42
  try:
43
  response = str(urlopen('http://checkip.dyndns.com/').read())
44
+ return re.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(response).group(1)
45
  except Exception as e:
46
  print("Error while getting IP address -->", e)
47
  return ''
 
121
  def generate_ocr_text(method, image):
122
  text_output = ''
123
  if image.any():
124
+ text_output = process_image_with_paddleocr(image)
 
 
 
 
 
125
 
126
  try:
127
  log_ocr_data(method, text_output, image)
 
134
  """
135
  Create user interface for OCR demo
136
  """
137
+ image_input = gr.Image(label="Upload Image", type="numpy", tool="editor")
138
+ output_textbox = gr.Textbox(label="Recognized Text", lines=5, placeholder="OCR results will appear here...")
 
139
 
140
  demo = gr.Interface(
141
  fn=generate_ocr_text,
142
+ inputs=[gr.Hidden("PaddleOCR"), image_input],
143
  outputs=output_textbox,
144
+ title="PaddleOCR - Optical Character Recognition",
145
+ description="Upload an image and extract text using PaddleOCR. This tool supports multiple languages and handles complex layouts.",
146
+ theme="default",
147
  css="""
148
+ .gradio-container {
149
+ background-color: #f0f4f8;
150
+ font-family: 'Roboto', sans-serif;
151
+ padding: 20px;
152
+ border-radius: 10px;
153
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
154
+ }
155
+ .gr-button {
156
+ background-color: #007bff;
157
+ color: white;
158
+ border-radius: 5px;
159
+ padding: 10px 20px;
160
+ font-size: 16px;
161
+ cursor: pointer;
162
+ }
163
+ .gr-button:hover {
164
+ background-color: #0056b3;
165
+ }
166
+ .gr-textbox {
167
+ background-color: #ffffff;
168
+ border: 1px solid #ced4da;
169
+ border-radius: 5px;
170
+ padding: 10px;
171
+ font-size: 16px;
172
+ }
173
+ .gr-textbox:focus {
174
+ border-color: #007bff;
175
+ box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
176
+ }
177
+ """)
178
 
179
  demo.launch()