File size: 10,284 Bytes
f1554a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c42b13
f1554a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c42b13
f1554a2
 
 
 
 
 
 
 
1c42b13
f1554a2
 
 
 
 
 
 
 
 
 
1c42b13
f1554a2
 
 
 
 
 
 
 
 
1c42b13
f1554a2
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import CDM.detect_text.ocr as ocr
from CDM.detect_text.Text import Text
import numpy as np
import cv2
import json
import time
import os
from os.path import join as pjoin
# from paddleocr import PaddleOCR
import pytesseract

# paddle_model = PaddleOCR(use_angle_cls=True, lang="en") #'ch' for chinese and english, 'en' for english


def save_detection_json(file_path, texts, img_shape):
    f_out = open(file_path, 'w')
    output = {'img_shape': img_shape, 'texts': []}
    for text in texts:
        c = {'id': text.id, 'content': text.content}
        loc = text.location
        c['column_min'], c['row_min'], c['column_max'], c['row_max'] = loc['left'], loc['top'], loc['right'], loc['bottom']
        c['width'] = text.width
        c['height'] = text.height
        output['texts'].append(c)
    json.dump(output, f_out, indent=4)


def visualize_texts(org_img, texts, shown_resize_height=None, show=False, write_path=None):
    img = org_img.copy()
    for text in texts:
        text.visualize_element(img, line=2)

    img_resize = img
    if shown_resize_height is not None:
        img_resize = cv2.resize(img, (int(shown_resize_height * (img.shape[1]/img.shape[0])), shown_resize_height))

    if show:
        cv2.imshow('texts', img_resize)
        cv2.waitKey(0)
        cv2.destroyWindow('texts')
    if write_path is not None:
        cv2.imwrite(write_path, img)


def text_sentences_recognition(texts):
    '''
    Merge separate words detected by Google ocr into a sentence
    '''
    changed = True
    while changed:
        changed = False
        temp_set = []
        for text_a in texts:
            merged = False
            for text_b in temp_set:
                if text_a.is_on_same_line(text_b, 'h', bias_justify=0.2 * min(text_a.height, text_b.height), bias_gap=2 * max(text_a.word_width, text_b.word_width)):
                    text_b.merge_text(text_a)
                    merged = True
                    changed = True
                    break
            if not merged:
                temp_set.append(text_a)
        texts = temp_set.copy()

    for i, text in enumerate(texts):
        text.id = i
    return texts


def merge_intersected_texts(texts):
    '''
    Merge intersected texts (sentences or words)
    '''
    changed = True
    while changed:
        changed = False
        temp_set = []
        for text_a in texts:
            merged = False
            for text_b in temp_set:
                if text_a.is_intersected(text_b, bias=2):
                    text_b.merge_text(text_a)
                    merged = True
                    changed = True
                    break
            if not merged:
                temp_set.append(text_a)
        texts = temp_set.copy()
    return texts


def text_cvt_orc_format(ocr_result):
    texts = []
    if ocr_result is not None:
        for i, result in enumerate(ocr_result):
            error = False
            x_coordinates = []
            y_coordinates = []
            text_location = result['boundingPoly']['vertices']
            content = result['description']
            for loc in text_location:
                if 'x' not in loc or 'y' not in loc:
                    error = True
                    break
                x_coordinates.append(loc['x'])
                y_coordinates.append(loc['y'])
            if error: continue
            location = {'left': min(x_coordinates), 'top': min(y_coordinates),
                        'right': max(x_coordinates), 'bottom': max(y_coordinates)}
            texts.append(Text(i, content, location))
    return texts


def text_cvt_orc_format_paddle(paddle_result):
    texts = []
    for i, line in enumerate(paddle_result):
        points = np.array(line[0])
        # points = points * 5
        location = {'left': int(min(points[:, 0])), 'top': int(min(points[:, 1])), 'right': int(max(points[:, 0])),
                    'bottom': int(max(points[:, 1]))}
        content = line[1][0]
        texts.append(Text(i, content, location))
    return texts


def text_cvt_orc_format_tesseract(tesseract_result):
    # texts = []
    # i_real = 0
    # for i, line in enumerate(tesseract_result['text']):
    #     content = line.strip()
    #     location = {
    #         'left': int(tesseract_result['left'][i]),
    #         'top': int(tesseract_result['top'][i]),
    #         'right': int(tesseract_result['left'][i]) + int(tesseract_result['width'][i]),
    #         'bottom': int(tesseract_result['top'][i]) + int(tesseract_result['height'][i])
    #     }
    #     if len(content) > 0:
    #         texts.append(Text(i_real, content, location))
    #         i_real = i_real + 1

    # Extract line boxes
    texts = []
    i_real = 0
    line_boxes = []
    n_boxes = len(tesseract_result['level'])
    for i in range(n_boxes):
        if tesseract_result['level'][i] == 4 and len(tesseract_result['text'][i].strip()) > 0:
            # (x, y, w, h) = (tesseract_result['left'][i], tesseract_result['top'][i], tesseract_result['width'][i], tesseract_result['height'][i])
            content = tesseract_result['text'][i].strip()
            location = {
                'left': int(tesseract_result['left'][i]),
                'top': int(tesseract_result['top'][i]),
                'right': int(tesseract_result['left'][i]) + int(tesseract_result['width'][i]),
                'bottom': int(tesseract_result['top'][i]) + int(tesseract_result['height'][i])
            }
            texts.append(Text(i_real, content, location))
            i_real = i_real + 1
    # print("ocr result: ", texts)

    return texts

def text_cvt_orc_format_tesseract_by_line(data):

    # line_data = []
    line_num = None
    line_text = []
    line_box = [0, 0, 0, 0]
    texts = []
    i_real = 0

    for i in range(len(data['level'])):
        # check if the level is word
        if data['level'][i] == 5:
            if line_num != data['line_num'][i]:
                if line_num is not None:  # append the previous line data to line_data
                    content = ' '.join(line_text)
                    location = {
                        'left': line_box[0],
                        'top': line_box[1],
                        'right': line_box[2],
                        'bottom': line_box[3]
                    }
                    texts.append(Text(i_real, content, location))
                    i_real = i_real + 1

                # start a new line
                line_num = data['line_num'][i]
                line_text = [data['text'][i]]
                line_box = [
                    data['left'][i],
                    data['top'][i],
                    data['left'][i] + data['width'][i],
                    data['top'][i] + data['height'][i],
                ]
            else:  # add a word to the current line
                line_text.append(data['text'][i])
                line_box[2] = max(line_box[2], data['left'][i] + data['width'][i])
                line_box[3] = max(line_box[3], data['top'][i] + data['height'][i])

        # append the last line data to line_data
    if line_text:
        content = ' '.join(line_text)
        location = {
            'left': line_box[0],
            'top': line_box[1],
            'right': line_box[2],
            'bottom': line_box[3]
        }
        texts.append(Text(i_real, content, location))
        i_real = i_real + 1

    return texts


def text_filter_noise(texts):
    valid_texts = []
    for text in texts:
        if len(text.content) <= 1 and text.content.lower() not in ['a', ',', '.', '!', '?', '$', '%', ':', '&', '+']:
            continue
        valid_texts.append(text)
    return valid_texts


def text_detection(input_file='../data/input/30800.jpg', output_file='../data/output', show=False, method='google', paddle_model=None):
    '''
    :param method: google or paddle
    :param paddle_model: the preload paddle model for paddle ocr
    '''
    start = time.time()
    name = input_file.split('/')[-1][:-4]
    ocr_root = pjoin(output_file, 'ocr')
    img = cv2.imread(input_file)
    if img is None:
        print("imread nothing!")

    # resize the img to speed up the ocr
    # img = cv2.resize(img, (int(img.shape[1]/5), int(img.shape[0]/5)))
    # cv2.imshow("img", img)
    # cv2.waitKey(0)

    if method == 'google':
        print('*** Detect Text through Google OCR ***')
        ocr_result = ocr.ocr_detection_google(input_file)
        texts = text_cvt_orc_format(ocr_result)
        texts = merge_intersected_texts(texts)
        texts = text_filter_noise(texts)
        texts = text_sentences_recognition(texts)
        ocr_time_cost = time.time() - start
    elif method == 'paddle':
        # The import of the paddle ocr can be separate to the beginning of the program if you decide to use this method
        # from paddleocr import PaddleOCR
        print('*** Detect Text through Paddle OCR ***')
        # if paddle_model is None:
            # paddle_model = PaddleOCR(use_angle_cls=True, lang="en") #'ch' for chinese and english, 'en' for english
            # None
        result = paddle_model.ocr(input_file, cls=True)
        ocr_time_cost = time.time() - start
        texts = text_cvt_orc_format_paddle(result)

    elif method == 'pytesseract':

        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # Perform OCR using Tesseract
        result = pytesseract.image_to_data(img_rgb, output_type=pytesseract.Output.DICT)
        print("ocr result: ", result)

        ocr_time_cost = time.time() - start

        # Convert the Tesseract result to the desired format
        texts = text_cvt_orc_format_tesseract_by_line(result)
        print("texts: ", texts)
    else:
        raise ValueError('Method has to be "google" or "paddle" or "pytesseract"')

    visualize_texts(img, texts, shown_resize_height=800, show=show, write_path=pjoin(ocr_root, name+'.png'))
    save_detection_json(pjoin(ocr_root, name+'.json'), texts, img.shape)
    # ocr_time_cost = time.time() - start
    print("[Text Detection Completed in %.3f s] Input: %s Output: %s" % (ocr_time_cost, input_file, pjoin(ocr_root, name+'.json')))

    # print("!!! detected content !!!")
    # for text in texts:
    #     print(text.content)

    return ocr_time_cost


# text_detection()