pragnakalp commited on
Commit
c10e31c
1 Parent(s): 964aca7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -183
app.py CHANGED
@@ -16,61 +16,6 @@ import datasets
16
  from datasets import load_dataset, Image, Features, Array3D
17
  from PIL import Image
18
  from paddleocr import PaddleOCR
19
- import socket
20
- # from send_email_user import send_user_email
21
- from huggingface_hub import HfApi
22
- import smtplib
23
-
24
- HF_TOKEN = os.environ.get("HF_TOKEN")
25
- # mydataset_name = "pragnakalp/OCR-img-to-text"
26
- # print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$",type(mydataset_name))
27
- # hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN,mydataset_name)
28
-
29
-
30
- DATASET_REPO_URL = "https://huggingface.co/datasets/pragnakalp/OCR-img-to-text"
31
- DATA_FILENAME = "ocr_data.csv"
32
- DATA_FILE = os.path.join("ocr_data", DATA_FILENAME)
33
- # DATA_FILENAME2 = "image"
34
- # DATA_FILE2 = os.path.join("ocr_data",DATA_FILENAME2)
35
- HF_TOKEN = os.environ.get("HF_TOKEN")
36
- DATASET_REPO_ID = "pragnakalp/OCR-img-to-text"
37
- print("is none?", HF_TOKEN is None)
38
- try:
39
- hf_hub_download(
40
- repo_id=DATASET_REPO_ID,
41
- filename=DATA_FILENAME,
42
- cache_dir=DATA_DIRNAME,
43
- force_filename=DATA_FILENAME
44
- )
45
-
46
- except:
47
- print("file not found")
48
-
49
- repo = Repository(
50
- local_dir="ocr_data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
51
- )
52
-
53
- def get_device_ip_address():
54
-
55
- if os.name == "nt":
56
- result = "Running on Windows"
57
- hostname = socket.gethostname()
58
- result += "\nHostname: " + hostname
59
- host = socket.gethostbyname(hostname)
60
- result += "\nHost-IP-Address:" + host
61
- return result
62
- elif os.name == "posix":
63
- gw = os.popen("ip -4 route show default").read().split()
64
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
65
- s.connect((gw[2], 0))
66
- ipaddr = s.getsockname()[0]
67
- gateway = gw[2]
68
- host = socket.gethostname()
69
- result = "\nIP address:\t\t" + ipaddr + "\r\nHost:\t\t" + host
70
- return result
71
- else:
72
- result = os.name + " not supported yet."
73
- return result
74
 
75
 
76
  """
@@ -118,147 +63,40 @@ def ocr_with_easy(img):
118
  bounds = reader.readtext('image.png',paragraph="False",detail = 0)
119
  bounds = ''.join(bounds)
120
  return bounds
121
-
122
- # def store_single_disk(image, image_id, label):
123
- # """ Stores a single image as a .png file on disk.
124
- # Parameters:
125
- # ---------------
126
- # image image array, (32, 32, 3) to be stored
127
- # image_id integer unique ID for image
128
- # label image label
129
- # """
130
- # Image.fromarray(image).save(disk_dir / f"{image_id}.png")
131
-
132
- # with open(disk_dir / f"{image_id}.csv", "wt") as csvfile:
133
- # writer = csv.writer(
134
- # csvfile, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL
135
- # )
136
- # writer.writerow([label])
137
 
138
  """
139
  Generate OCR
140
  """
141
  def generate_ocr(Method,img):
142
  try:
143
- text_output = ''
144
- add_csv = []
145
- image_id = 1
146
- print("Method___________________",Method)
147
- if Method == 'EasyOCR':
148
- text_output = ocr_with_easy(img)
149
- if Method == 'KerasOCR':
150
- text_output = ocr_with_keras(img)
151
- if Method == 'PaddleOCR':
152
- text_output = ocr_with_paddle(img)
153
-
154
- new_data=img.reshape(img.shape)
155
- imge = Image.fromarray(new_data.astype(np.uint8),'RGB')
156
- feature = datasets.Image(decode=False)
157
- add_csv = [Method,img,text_output]
158
- # new_image = {'image': feature.encode_example(imge)}
159
- # print("newimage: ",new_image)
160
- with open(DATA_FILE, "a") as f:
161
- writer = csv.writer(f)
162
- # write the data
163
- writer.writerow(add_csv)
164
- commit_url = repo.push_to_hub()
165
- print(commit_url)
166
-
167
- # print("^^%%",Image.fromarray(img).save(DATA_FILE2 +"/"+ f"{image_id}.png"))
168
- # with open(DATA_FILE2, "wt") as csvfile:
169
- # writer = csv.writer(
170
- # csvfile, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL
171
- # )
172
- # writer.writerow(['0'])
173
-
174
- # try:
175
- # dataset = load_dataset("pragnakalp/OCR-img-to-text", streaming=True)
176
- # print(dataset.features)
177
- # except Exception as e:
178
- # print("error in loading data",e)
179
-
180
-
181
- # with open(DATA_FILE, "a") as csvfile:
182
- # writer = csv.Writer(csvfile)
183
- # writer.writerow(add_csv)
184
- # commit_url = repo.push_to_hub()
185
- # print(commit_url)
186
-
187
- # save_details(Method,text_output,img)
188
- # sender="[email protected]"
189
- # password="httscgatatbbxxur"
190
- # reciever="[email protected]"
191
-
192
- # s = smtplib.SMTP('smtp.gmail.com', 587)
193
- # s.starttls()
194
- # s.ehlo()
195
- # s.login(sender,password)
196
-
197
- # message = """Subject : Appointment Booking\n\n
198
- # Hello,
199
- # Your OCR generated successfully"""
200
- # s.sendmail(sender, reciever, message)
201
- # s.quit()
202
- # mailsend=1
203
- # print("Send mail successfully")
204
- return text_output
205
 
206
  except Exception as e:
207
  print("Error in ocr generation ==>",e)
208
  text_output = "Something went wrong"
209
  return text_output
210
- """
211
- Save generated details
212
- """
213
- def save_details(Method,text_output,img):
214
- # print("//////////")
215
- hostname = get_device_ip_address()
216
- # url = 'https://pragnakalpdev33.pythonanywhere.com/HF_space_image_to_text'
217
- # url = 'http://pragnakalpdev35.pythonanywhere.com/HF_space_image_to_text'
218
- # myobj = {'Method': Method,'text_output':text_output,'img':img.tolist(),'hostname':hostname}
219
- # x = requests.post(url, json = myobj)
220
 
221
- # method = []
222
- # img_path = []
223
- # text = []
224
- # input_img = ''
225
- # hostname = ''
226
- # picture_path = "image.jpg"
227
- # curr_datetime = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
228
- # if text_output:
229
- # splitted_path = os.path.splitext(picture_path)
230
- # modified_picture_path = splitted_path[0] + curr_datetime + splitted_path[1]
231
- # cv2.imwrite("myimage.jpg", img)
232
- # with open('savedata.txt', 'w') as f:
233
- # print("write test")
234
- # f.write("testdata")
235
- # print("write Successfully")
236
- # # img = Image.open(r"/home/user/app/")
237
- # # img.save(modified_picture_path)
238
- # input_img = modified_picture_path
239
- # try:
240
- # df = pd.read_csv("AllDetails.csv")
241
- # df2 = {'method': Method, 'input_img': input_img, 'generated_text': text_output}
242
- # df = df.append(df2, ignore_index = True)
243
- # df.to_csv("AllDetails.csv", index=False)
244
- # except:
245
- # method.append(Method)
246
- # img_path.append(input_img)
247
- # text.append(text_output)
248
- # dict = {'method': method, 'input_img': img_path, 'generated_text': text}
249
- # df = pd.DataFrame(dict,index=None)
250
- # df.to_csv("AllDetails.csv")
251
-
252
-
253
- return send_user_email()
254
- # return x
255
 
256
  """
257
  Create user interface for OCR demo
258
  """
259
 
260
- image = gr.Image(shape=(224, 224),elem_id="img_div")
261
- method = gr.Radio(["EasyOCR", "KerasOCR", "PaddleOCR"],value="PaddleOCR",elem_id="radio_div")
262
  output = gr.Textbox(label="Output")
263
 
264
  demo = gr.Interface(
@@ -266,10 +104,11 @@ demo = gr.Interface(
266
  [method,image],
267
  output,
268
  title="Optical Character Recognition",
269
- description="Try OCR with different methods",
270
  css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
271
- allow_flagging = "manual"
272
- # flagging_dir = "flagged",
273
- # flagging_callback=hf_writer
 
 
274
  )
275
  demo.launch(enable_queue = False)
 
16
  from datasets import load_dataset, Image, Features, Array3D
17
  from PIL import Image
18
  from paddleocr import PaddleOCR
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  """
 
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
  try:
72
+ if img.any():
73
+ text_output = ''
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
+ return text_output
85
+ else:
86
+ raise gr.Error("Please upload an image!!!!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  except Exception as e:
89
  print("Error in ocr generation ==>",e)
90
  text_output = "Something went wrong"
91
  return text_output
 
 
 
 
 
 
 
 
 
 
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  """
95
  Create user interface for OCR demo
96
  """
97
 
98
+ image = gr.Image(shape=(300, 300))
99
+ method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR"],value="PaddleOCR")
100
  output = gr.Textbox(label="Output")
101
 
102
  demo = gr.Interface(
 
104
  [method,image],
105
  output,
106
  title="Optical Character Recognition",
 
107
  css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
108
+ article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
109
+ <a href="mailto:[email protected]" target="_blank">[email protected].</a>
110
+ <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
111
+
112
+
113
  )
114
  demo.launch(enable_queue = False)