ImranzamanML commited on
Commit
832ca96
·
verified ·
1 Parent(s): d5d03a7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +209 -0
app.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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'
35
+ os.makedirs(LOCAL_DIR, exist_ok=True)
36
+
37
+ """
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
+ """
86
+ def save_json(data, filepath):
87
+ with open(filepath, 'w+', encoding="utf8") as f:
88
+ json.dump(data, f)
89
+
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 ''
97
+
98
+ def fetch_location(ip_addr):
99
+ try:
100
+ req_data = {"ip": ip_addr, "token": "pkml123"}
101
+ url = "https://demos.pragnakalp.com/get-ip-location"
102
+ headers = {'Content-Type': 'application/json'}
103
+ response = requests.post(url, headers=headers, data=json.dumps(req_data)).json()
104
+ return response
105
+ except Exception as e:
106
+ print("Error while getting location -->", e)
107
+ return {}
108
+
109
+ def log_ocr_data(method, text_output, input_image):
110
+ print("Logging OCR data...")
111
+ ip_address = get_ip_address()
112
+ location_info = fetch_location(ip_address)
113
+ timestamp = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
114
+ save_dir = os.path.join(LOCAL_DIR, timestamp)
115
+ os.makedirs(save_dir, exist_ok=True)
116
+
117
+ image_filename = os.path.join(save_dir, 'image.png')
118
+ try:
119
+ PILImage.fromarray(input_image).save(image_filename)
120
+ except Exception:
121
+ raise Exception(f"Failed to save image as file")
122
+
123
+ metadata_file_path = os.path.join(save_dir, 'metadata.jsonl')
124
+ metadata = {
125
+ 'id': timestamp,
126
+ 'method': method,
127
+ 'file_name': 'image.png',
128
+ 'generated_text': text_output,
129
+ 'ip': ip_address,
130
+ 'location': location_info
131
+ }
132
+ save_json(metadata, metadata_file_path)
133
+
134
+ repo_image_path = os.path.join(REPOSITORY_DIR, os.path.join(timestamp, 'image.png'))
135
+ _ = upload_file(
136
+ path_or_fileobj=image_filename,
137
+ path_in_repo=repo_image_path,
138
+ repo_id=DATASET_REPO_ID,
139
+ repo_type='dataset',
140
+ token=HF_TOKEN
141
+ )
142
+
143
+ repo_json_path = os.path.join(REPOSITORY_DIR, os.path.join(timestamp, 'metadata.jsonl'))
144
+ _ = upload_file(
145
+ path_or_fileobj=metadata_file_path,
146
+ path_in_repo=repo_json_path,
147
+ repo_id=DATASET_REPO_ID,
148
+ repo_type='dataset',
149
+ token=HF_TOKEN
150
+ )
151
+
152
+ repo.git_pull()
153
+
154
+ url = 'http://pragnakalpdev35.pythonanywhere.com/HF_space_image_to_text'
155
+ payload = {
156
+ 'Method': method,
157
+ 'text_output': text_output,
158
+ 'img': input_image.tolist(),
159
+ 'ip_address': ip_address,
160
+ 'loc': location_info
161
+ }
162
+ response = requests.post(url, json=payload)
163
+ print("Mail status code:", response.status_code)
164
+
165
+ return "***** Logs saved successfully! *****"
166
+
167
+ """
168
+ OCR Generation
169
+ """
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)
182
+ except Exception as e:
183
+ print(e)
184
+ return text_output
185
+ else:
186
+ raise gr.Error("Please upload an image!")
187
+
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()