File size: 3,305 Bytes
ff33835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
import numpy as np
import gradio as gr

import pytesseract

def text_read_tesseract(img):
    '''
    To read wriiten texts on images
    Required: 
        pip install pytesseract
        sudo apt install tesseract-ocr 
        sudo apt install libtesseract-dev
    API: https://pypi.org/project/pytesseract/
    '''

    pytesseract.pytesseract.tesseract_cmd='/usr/bin/tesseract'
    
    imgWords = img.copy()
    imgDigits = img.copy()
    imgRGB = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2RGB)
    
    # Character detection
    texts = pytesseract.image_to_string(imgRGB)
    boxes = pytesseract.image_to_boxes(imgRGB)

    # print(boxes)

    himg, wimg, _ = img.shape
    for b in boxes.splitlines():
        b = b.split(' ')
        if b[0] != '~':
            x1, y1, x2, y2 = int(b[1]), int(b[2]), int(b[3]), int(b[4])
            cv2.rectangle(img, pt1=(x1, himg-y1), pt2=(x2, himg-y2), color=(0,0,255), thickness=1)

    # cv2.imshow("Image char", img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    return texts, img

import easyocr


def text_read_easyocr(img, text_threshold):
    # [[189, 75], [469, 75], [469, 165], [189, 165]]
    easyocr_reader = easyocr.Reader(lang_list=['en'], gpu=False)
    result = easyocr_reader.readtext(img, text_threshold=text_threshold)
    himg, wimg, _ = img.shape
    text = ""
    for box, word, confidence in result:
        text += word + "\n"
        # x2, y2, x1, y1 = box[0][0], box[0][1], box[2][0], box[2][1]
        # cv2.rectangle(img, pt1=(x1, himg-y1), pt2=(x2, himg-y2), color=(0,0,255), thickness=1)
        tl, tr, br, bl = box
        tl = (int(tl[0]), int(tl[1]))
        tr = (int(tr[0]), int(tr[1]))
        br = (int(br[0]), int(br[1]))
        bl = (int(bl[0]), int(bl[1]))
        cv2.rectangle(img, tl, br, (0, 255, 0), 2)
    
    # print(text)
    # cv2.imshow("Image char", img)
    # cv2.waitKey(0)
    return text, img


def read_text(img, choice,text_threshold):
    if choice == 0:
        return text_read_tesseract(img)
    if choice == 1:
        return text_read_easyocr(img, text_threshold)

# img = cv2.imread("../images/license.jpeg")
# text_read_tesseract(img)
# text_read_easyocr(img)

# demo = gr.Interface(fn=text_read_tesseract, inputs="image", outputs=[gr.Textbox(lines=2, placeholder="Detected txts here..."), "image"])
# demo = gr.Interface(fn=text_read_easyocr, inputs="image", outputs=[gr.Textbox(lines=2, placeholder="Detected txts here..."), "image"])

demo = gr.Interface(fn=read_text,
                    title="Optical character recognition",
                    description="Author: Rituraj Kaushik",
                    examples=[["images/bill.png", "Easy OCR", 0.6],
                            ["images/passport.jpg", "Easy OCR", 0.6]],
                    inputs=[gr.Image(label="Input image"), 
                            gr.Radio(choices=["Tesseract", "Easy OCR"],
                                    value= "Easy OCR",
                                    type="index"), 
                            gr.Slider(minimum=0, maximum=1, label="Text confidence threshold", value=0.6, step=0.01)
                            ],
                    outputs=[gr.Textbox(lines=2, placeholder="Detected txts here..."), "image"],
                    allow_flagging="never")


demo.launch()