프로젝트

일반

사용자정보

개정판 e266373b

IDe266373b3fcecacfe92671c875b2df1456338a42
상위 59560085
하위 1e67c5f1, 039c0a17

백흠경이(가) 6년 이상 전에 추가함

fixed issue #478:
- change logic to extract text from image

차이점 보기:

DTI_PID/DTI_PID/tesseract_ocr_module.py
170 170
    return textInfoList
171 171

  
172 172

  
173
'''
173
"""
174 174
    @history    2018.04.26  Jeongwoo    Make TextInfo object with Calculated Coords (with BoundBox Coords)
175 175
                2018.04.30  Jeongwoo    Add QRect.setHeight() in if-statement [(lineRect is not None and currentRect is not None) and lineRect.intersects(currentRect)]
176 176
                2018.05.09  Jeongwoo    Check split text' length
......
179 179
                2018.06.08  Jeongwoo    Add angle Parameter and get rotated image's coords
180 180
                2018.06.14  Jeongwoo    Add try-except. If exception occured, return None
181 181
                2018.06.20  Jeongwoo    Remove variable [lastCharHeight] / Change variable [cey], [ch] / Change method to calculate text line height
182
'''
182
                humkyung 2018.10.12 change logic to extract text which first get bounding box and then extract character
183
"""
183 184
def getTextInfo(img, startPoint, angle = 0, flag = FLAG_IMAGE_TO_BOXES, conf = DEFAULT_CONF):
184 185
    try:
186
        textInfoList = []
187

  
185 188
        docData = AppDocData.instance()
186 189
        configs = docData.getConfigs('Text Size', 'Min Text Size')
187 190
        minSize = int(configs[0].value) if 1 == len(configs) else 30
......
192 195
        im = im.rotate(-angle, expand=True)
193 196
        imgWidth = im.width
194 197
        imgHeight = im.height
195
        textOnlyOcrData = pytesseract.image_to_string(im, config=conf, lang='eng')
196
        textInfoList = []
197
        ADJUST = 2
198
        if textOnlyOcrData:
199
            boundaryOcrData = pytesseract.image_to_boxes(im, config=conf, lang='eng')
200
            text = textOnlyOcrData
201
            text = re.sub('\n{1,}', '\n', text)
202
            splitText = text.split('\n')
203
            textGroupIndex = 0
204

  
205
            lastRT = (-1, -1)
206
            tempText = ''
207
            charWidth = -1
208
            charHeight = -1
209
            lineSp = (-1, -1)
210
            lineRect = None
211
            for index in range(len(boundaryOcrData.split('\n'))):
212
                data = boundaryOcrData.split('\n')[index]
213
                sData = data.split(' ')
214
                if len(sData) >= 5:
215
                    char = sData[0]
216
                    csx = int(sData[1])
217
                    csy = imgHeight - int(sData[4])
218
                    cex = int(sData[3])
219
                    cey = imgHeight - int(sData[2])
220
                    cw = cex - csx
221
                    ch = abs(cey - csy)
222
                    charWidth = max(charWidth, cw)
223
                    charHeight = max(charHeight, ch)
224
                    currentRect = None
225
                    if lastRT != (-1, -1):
226
                        currentRect = QRect(csx, csy, csx + cw, csy + ch)
227
                    if lastRT == (-1, -1) and lineRect is None:
228
                        tempText = tempText + char
229
                        lastRT = (csx+cw, csy)
230
                        lineSp = (csx, csy)
231
                        lineRect = QRect(lineSp[0], lineSp[1], imgWidth - lineSp[0], charHeight)
232
                    elif (lineRect is not None and currentRect is not None) and lineRect.intersects(currentRect):
233
                        tempText = tempText + char
234
                        lastRT = (csx+cw, min(lineSp[1], csy))
235
                        lineSp = (lineSp[0], min(lineSp[1], csy))
236
                        lineHeight = max(max(lineRect.height(), abs(cey - lineSp[1])), abs((lineRect.y()+lineRect.height()) - lineSp[1]))
237
                        lineRect.setX(lineSp[0])
238
                        lineRect.setY(lineSp[1])
239
                        lineRect.setHeight(lineHeight)
198
        boundaryOcrData = pytesseract.image_to_boxes(im, config=conf, lang='eng')
199
        bounding_boxes = boundaryOcrData.split('\n')
200
        merged_boxes = []
201
        for box in bounding_boxes:
202
            if merged_boxes:
203
                tokens = box.split(' ')
204
                if len(tokens) >= 5:
205
                    minx = int(tokens[1])
206
                    miny = int(tokens[2])
207
                    maxx = int(tokens[3])
208
                    maxy = int(tokens[4])
209

  
210
                    top = merged_boxes[-1].top()
211
                    bottom = merged_boxes[-1].bottom()
212

  
213
                    if maxy < top or miny > bottom:
214
                        merged_boxes.append(QRect(minx, miny, maxx - minx, maxy - miny))
240 215
                    else:
241
                        # Save previous line
242
                        if len(splitText) > textGroupIndex:
243
                            rect = QRect(lineSp[0], lineSp[1], lastRT[0]-lineSp[0], lineRect.height())
244
                            if rect.height() >= minSize and rect.height() <= maxSize:
245
                                if angle == 90 or angle == 270:
246
                                    transform = QTransform()
247
                                    transform.translate(imgHeight*0.5, imgWidth*0.5)
248
                                    transform.rotate(-angle)
249
                                    transform.translate(-imgWidth*0.5, -imgHeight*0.5)
250
                                    rect = transform.mapRect(rect)
251
                                prevLineText = ti.TextInfo(splitText[textGroupIndex], startPoint[0]+rect.left(), startPoint[1]+rect.top(), rect.width(), rect.height(), angle)
252
                                textInfoList.append(prevLineText)
253
                            textGroupIndex = textGroupIndex + 1
254

  
255
                            # Start new line
256
                            tempText = char
257
                            charWidth = cw
258
                            charHeight = ch
259
                            lastRT = (csx + cw, csy)
260
                            lineSp = (csx, csy)
261
                            lineRect = QRect(lineSp[0], lineSp[1], imgWidth - lineSp[0], ch)
262

  
263
            if not (len(textInfoList) > textGroupIndex) and textGroupIndex < len(splitText):
264
                rect = QRect(lineSp[0], lineSp[1], lastRT[0]-lineSp[0], lineRect.height())
265
                if rect.height() >= minSize and rect.height() <= maxSize:
266
                    if angle == 90 or angle == 270:
267
                        transform = QTransform()
268
                        transform.translate(imgHeight*0.5, imgWidth*0.5)
269
                        transform.rotate(-angle)
270
                        transform.translate(-imgWidth*0.5, -imgHeight*0.5)
271
                        rect = transform.mapRect(rect)
272
                    textInfo = ti.TextInfo(splitText[textGroupIndex], startPoint[0]+rect.left(), startPoint[1]+rect.top(), rect.width(), rect.height(), angle)
273
                    if textInfo not in textInfoList:
274
                        textInfoList.append(textInfo)
216
                        merged_boxes[-1].setLeft(min(merged_boxes[-1].left(), minx))
217
                        merged_boxes[-1].setTop(min(merged_boxes[-1].top(), miny))
218
                        merged_boxes[-1].setRight(max(merged_boxes[-1].right(), maxx))
219
                        merged_boxes[-1].setBottom(max(merged_boxes[-1].bottom(), maxy))
275 220
            else:
276
                return None
277
        else:
278
            pass
221
                tokens = box.split(' ')
222
                if len(tokens) >= 5:
223
                    minx = int(tokens[1])
224
                    miny = int(tokens[2])
225
                    maxx = int(tokens[3])
226
                    maxy = int(tokens[4])
227
                    merged_boxes.append(QRect(minx, miny, maxx - minx, maxy - miny))
228
        
229
        for rect in merged_boxes:
230
            cropped = im.crop((rect.left(), imgHeight - rect.bottom(), rect.right(), imgHeight - rect.top()))
231
            text = pytesseract.image_to_string(cropped, config=conf, lang='eng')
232

  
233
            if rect.height() >= minSize and rect.height() <= maxSize:
234
                text_rect = QRect(rect.left(), imgHeight - rect.bottom(), rect.width(), rect.height())
235
                if angle == 90 or angle == 270:
236
                    transform  = QTransform()
237
                    transform.translate(imgHeight*0.5, imgWidth*0.5)
238
                    transform.rotate(-angle)
239
                    transform.translate(-imgWidth*0.5, -imgHeight*0.5)
240
                    text_rect = transform.mapRect(text_rect)
241
                            
242
                text_info = ti.TextInfo(text, startPoint[0] + text_rect.left(), startPoint[1] + text_rect.top(), text_rect.width(), text_rect.height(), angle)
243
                textInfoList.append(text_info)
244

  
279 245
        return textInfoList
280 246
    except Exception as ex:
281 247
        from App import App

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)