mohammad2928git commited on
Commit
3599d1b
·
verified ·
1 Parent(s): ed67370

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import keras_ocr
4
+ import requests
5
+ import cv2
6
+ import os
7
+ import csv
8
+ import numpy as np
9
+ import pandas as pd
10
+ import huggingface_hub
11
+ from huggingface_hub import Repository
12
+ from datetime import datetime
13
+ import scipy.ndimage.interpolation as inter
14
+ import easyocr
15
+ 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
+ import pytesseract
21
+ from pdf2image import convert_from_path
22
+ from PIL import Image
23
+ import os
24
+
25
+
26
+
27
+ # Function to perform OCR
28
+ def ocr(input_file, lang='fas'): # 'fas': Persian language (Farsi)
29
+ extracted_text = ""
30
+
31
+ # Check if the input file is a PDF or an image
32
+ if isinstance(input_file, str) and input_file.endswith('.pdf'): # Check if the file is a PDF
33
+ # Convert PDF to images
34
+ images = convert_from_path(input_file)
35
+
36
+ # Loop through each image and perform OCR
37
+ for page_number, image in enumerate(images):
38
+ text = pytesseract.image_to_string(image, lang=lang)
39
+ extracted_text += text
40
+
41
+ elif isinstance(input_file, Image.Image): # If the input is an image
42
+ text = pytesseract.image_to_string(input_file, lang=lang)
43
+ extracted_text = text
44
+
45
+ return extracted_text
46
+
47
+ """
48
+ Paddle OCR
49
+ """
50
+ def ocr_with_paddle(img):
51
+ finaltext = ''
52
+ ocr = PaddleOCR(lang='en', use_angle_cls=True)
53
+ # img_path = 'exp.jpeg'
54
+ result = ocr.ocr(img)
55
+
56
+ for i in range(len(result[0])):
57
+ text = result[0][i][1][0]
58
+ finaltext += ' '+ text
59
+ return finaltext
60
+
61
+ """
62
+ Keras OCR
63
+ """
64
+ def ocr_with_keras(img):
65
+ output_text = ''
66
+ pipeline=keras_ocr.pipeline.Pipeline()
67
+ images=[keras_ocr.tools.read(img)]
68
+ predictions=pipeline.recognize(images)
69
+ first=predictions[0]
70
+ for text,box in first:
71
+ output_text += ' '+ text
72
+ return output_text
73
+
74
+ """
75
+ easy OCR
76
+ """
77
+ # gray scale image
78
+ def get_grayscale(image):
79
+ return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
80
+
81
+ # Thresholding or Binarization
82
+ def thresholding(src):
83
+ return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
84
+ def ocr_with_easy(img):
85
+ gray_scale_image=get_grayscale(img)
86
+ thresholding(gray_scale_image)
87
+ cv2.imwrite('image.png',gray_scale_image)
88
+ reader = easyocr.Reader(['th','en'])
89
+ bounds = reader.readtext('image.png',paragraph="False",detail = 0)
90
+ bounds = ''.join(bounds)
91
+ return bounds
92
+
93
+ """
94
+ Generate OCR
95
+ """
96
+
97
+ def process(input_type, file, lang):
98
+ if input_type == "PDF":
99
+ extracted_text = ocr(file.name, lang)
100
+ else:
101
+ image = Image.open(file.name)
102
+ extracted_text = ocr(image, lang)
103
+ return extracted_text
104
+
105
+
106
+ def generate_ocr(Method,img):
107
+
108
+ text_output = ''
109
+ if (img).any():
110
+ add_csv = []
111
+ image_id = 1
112
+ print("Method___________________",Method)
113
+ if Method == 'EasyOCR':
114
+ text_output = ocr_with_easy(img)
115
+ if Method == 'KerasOCR':
116
+ text_output = ocr_with_keras(img)
117
+ if Method == 'PaddleOCR':
118
+ text_output = ocr_with_paddle(img)
119
+ if Method == 'tesseract':
120
+ text_output = process("img", img, "fas")
121
+ try:
122
+ flag(Method,text_output,img)
123
+ except Exception as e:
124
+ print(e)
125
+ return text_output
126
+ else:
127
+ raise gr.Error("Please upload an image!!!!")
128
+
129
+ """
130
+ Create user interface for OCR demo
131
+ """
132
+
133
+ # image = gr.Image(shape=(300, 300))
134
+ image = gr.Image()
135
+ method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR", "tesseract"],value="PaddleOCR")
136
+ output = gr.Textbox(label="Output")
137
+
138
+ demo = gr.Interface(
139
+ generate_ocr,
140
+ [method, image],
141
+ output,
142
+ title="Optical Character Recognition",
143
+ css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
144
+ article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
145
+ <a href="mailto:[email protected]" target="_blank">[email protected]</a>
146
+ <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
147
+
148
+
149
+ )
150
+ # demo.launch(enable_queue = False)
151
+ demo.launch()
152
+
153
+