lakshya-raj commited on
Commit
5a766cf
·
1 Parent(s): 1bd0a3d

v2.0.1-Simple OCR using Tesseract and OpenCV

Browse files
Files changed (3) hide show
  1. app.py +35 -2
  2. content/image.png +0 -0
  3. requirements.txt +3 -1
app.py CHANGED
@@ -1,7 +1,40 @@
1
  import gradio as gr
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  def greet(name):
4
  return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
+ # try:
4
+ # from PIL import Image
5
+ # except ImportError:
6
+ # import Image
7
+ import cv2
8
+ import pytesseract
9
+
10
+ sample_img = cv2.imread(r'/content/image.png')
11
+
12
+ def ocr_core(img):
13
+ text = pytesseract.image_to_string(img)
14
+ return text
15
+
16
+ def get_grayscale(image):
17
+ return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
18
+
19
+ def remove_noise(image):
20
+ return cv2.medianBlur(image,5)
21
+
22
+ def thresholding(image):
23
+ return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
24
+
25
+ def final_ocr(img):
26
+ gray = get_grayscale(img)
27
+ thresh = thresholding(gray)
28
+ noiseless = remove_noise(thresh)
29
+ return ocr_core(noiseless)
30
+
31
  def greet(name):
32
  return "Hello " + name + "!!"
33
 
34
+ # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
35
+
36
+ # iface.launch()
37
+ demo = gr.Interface(fn=final_ocr,
38
+ inputs=gr.inputs.Image(type="pil"),
39
+ outputs=gr.outputs.Textbox())
40
+ demo.launch()
content/image.png ADDED
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- gradio
 
 
 
1
+ pytesseract
2
+ PIL
3
+ opencv_python