llmat commited on
Commit
f5a94e3
·
verified ·
1 Parent(s): 001f24f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -22
app.py CHANGED
@@ -1,32 +1,44 @@
1
  import gradio as gr
2
  import cv2
3
- import pytesseract
4
  from PIL import Image
5
 
6
- def ocr(image):
7
- # Step 1: Convert the image to gray scale
8
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
 
10
- # Step 2: Perform OCR on the image
11
- text = pytesseract.image_to_string(gray)
12
 
13
- # Step 3: Prepare the output
14
- output = {
15
- "step1": "The first step is to convert the image to grayscale. This often improves the accuracy of OCR.",
16
- "step1_image": gray,
17
- "step2": "The second step is to perform OCR on the grayscale image using pytesseract.",
18
- "step2_text": text
19
- }
 
20
 
21
- return output
 
 
 
 
 
 
 
22
 
23
- def process_output(output):
24
- output["step1_image"] = Image.fromarray(output["step1_image"])
25
- return output
26
 
27
- iface = gr.Interface(fn=ocr,
28
- inputs=gr.Image(type="pil"),
29
- outputs=["text", "image", "text", "text"])
 
 
 
 
 
 
 
30
 
31
- iface.process_output = process_output
32
- iface.launch()
 
1
  import gradio as gr
2
  import cv2
3
+ import easyocr
4
  from PIL import Image
5
 
6
+ def get_grayscale(image):
7
+ return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
8
 
9
+ def thresholding(src):
10
+ return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
11
 
12
+ def ocr_with_easy(img):
13
+ gray_scale_image = get_grayscale(img)
14
+ thresholding(gray_scale_image)
15
+ cv2.imwrite('image.png', gray_scale_image)
16
+ reader = easyocr.Reader(['en'])
17
+ bounds = reader.readtext('image.png', paragraph="False", detail=0)
18
+ bounds = ''.join(bounds)
19
+ return bounds
20
 
21
+ def generate_ocr(img):
22
+ text_output = ''
23
+ if (img).any():
24
+ if img is not None:
25
+ text_output = ocr_with_easy(img)
26
+ else:
27
+ raise gr.Error("Please upload an image!!!!")
28
+ return text_output
29
 
30
+ image = gr.Image()
31
+ output = gr.Textbox(label="Output")
 
32
 
33
+ demo = gr.Interface(
34
+ generate_ocr,
35
+ image,
36
+ output,
37
+ title="Optical Character Recognition",
38
+ css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
39
+ article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
40
+ <a href="mailto:[email protected]" target="_blank">[email protected]</a>
41
+ <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
42
+ )
43
 
44
+ demo.launch()