개정판 9e64682b
DTI_PID 중 drawFoundSymbols 내 Tesseract 사용 구문을 tesseract_ocr_module에 병합 / tesseract_ocr_module의 각 함수의 conf를 전역 변수 사용하여 통일화
DTI_PID/DTI_PID/DTI_PID.py | ||
---|---|---|
552 | 552 |
|
553 | 553 |
''' |
554 | 554 |
@history 2018.04.27 Jeongwoo Remove Tesseract Log on listWidget |
555 |
2018.05.04 Jeongwoo Change method to OCR with tesseract_ocr_module.py |
|
555 | 556 |
''' |
556 | 557 |
def drawFoundSymbols(symbol, listWidget): |
557 | 558 |
global src |
... | ... | |
599 | 600 |
#kernel2 = np.ones((1, 1), np.uint8) |
600 | 601 |
#bitImg = cv2.erode(bitImg, kernel2) |
601 | 602 |
|
602 |
bitImg = cv2.resize(bitImg, None, fx = 2.0, fy = 2.0) |
|
603 |
|
|
604 |
im = Image.fromarray(bitImg) |
|
605 |
|
|
606 |
ocrData = pytesseract.image_to_boxes(im, config='-c tessedit_char_whitelist="-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" -psm 6') |
|
607 |
|
|
608 |
### For image_to_boxes() |
|
609 |
if ocrData: |
|
610 |
splitOcrData = ocrData.split('\n') |
|
611 |
tList = [] |
|
612 |
lastCoord = (-1, -1) # Top-Right Coord |
|
613 |
tText = [] |
|
614 |
ftSp = (-1, -1) |
|
603 |
try: |
|
615 | 604 |
threadLock.acquire() |
616 |
for data in splitOcrData: |
|
617 |
sData = data.split(' ') |
|
618 |
text = sData[0] |
|
619 |
tsx = int(sData[1]) // 2 |
|
620 |
tsy = int(sData[2]) // 2 |
|
621 |
tex = int(sData[3]) // 2 |
|
622 |
tey = int(sData[4]) // 2 |
|
623 |
tw = tex - tsx |
|
624 |
th = tey - tsy |
|
625 |
|
|
626 |
global MIN_TEXT_SIZE |
|
627 |
if WHITE_LIST_CHARS.find(text) >= 0: |
|
628 |
if tw >= MIN_TEXT_SIZE or th >= MIN_TEXT_SIZE: |
|
629 |
realTextSp = (-1, -1) |
|
630 |
if symbolOcrOption == SymbolBase.OCR_OPTION_HALF_AND_HALF: |
|
631 |
realTextSp = (symbolSp[0]+inSqX+tsx, symbolSp[1]+((h//2) - tsy + inSqY)) |
|
632 |
else: |
|
633 |
realTextSp = (symbolSp[0]+inSqX+tsx, symbolSp[1]+tsy) |
|
634 |
#cv2.rectangle(src, realTextSp, (realTextSp[0] + tw, realTextSp[1] + th), (0, 255, 255), 3) |
|
635 |
removeText(srcGray, text, realTextSp[0], realTextSp[1], tw, th) |
|
636 |
if lastCoord == (-1, -1): |
|
637 |
tText.append(text) |
|
638 |
ftSp = (tsx, tsy) |
|
639 |
else: |
|
640 |
COORD_ADJUSTMENT = 15 |
|
641 |
if (abs(lastCoord[1] - tsy) <= COORD_ADJUSTMENT and lastCoord[0] >= tsx - COORD_ADJUSTMENT and lastCoord[0] <= tsx + COORD_ADJUSTMENT) or (abs(lastCoord[0] - tsx) <= COORD_ADJUSTMENT and lastCoord[1] >= tsy - COORD_ADJUSTMENT and lastCoord[1] <= tsy + COORD_ADJUSTMENT): |
|
642 |
tText.append(text) |
|
643 |
else: |
|
644 |
if symbolOcrOption == SymbolBase.OCR_OPTION_ALL_FIND or symbolOcrOption == SymbolBase.OCR_OPTION_HALF_AND_HALF: |
|
645 |
tText.append(',') |
|
646 |
tText.append(text) |
|
647 |
|
|
648 |
lastCoord = (tex, tsy) # Top-Right Coord |
|
649 |
|
|
650 |
realSp = (symbolSp[0]+inSqX+ftSp[0], symbolSp[1]+inSqY+ftSp[1]) |
|
651 |
realEp = (symbolSp[0]+inSqX+lastCoord[0], symbolSp[1]+inSqY+lastCoord[1]) |
|
652 |
resultText = ''.join(tText) |
|
653 |
cv2.putText(canvas, resultText, (realSp[0], realSp[1]+th), 2, 1.0, (0, 0, 0)) # cv2.FONT_HERSHEY_SIMPLEX |
|
654 |
|
|
655 |
#textInfoList.append(ti.TextInfo(resultText, str(realSp[0]), str(realSp[1]), str(realEp[0]), str(realEp[1]))) |
|
656 |
|
|
657 |
# text value in symbol object update |
|
658 |
index = [i for i, item in enumerate(searchedSymbolList) if item.getSp() == symbolSp] |
|
659 |
if len(index) > 0: |
|
660 |
searchedSymbolList[index[0]].setText(resultText) |
|
605 |
im = Image.fromarray(bitImg) |
|
606 |
sp = (0, 0) |
|
607 |
if symbolOcrOption == SymbolBase.OCR_OPTION_HALF_AND_HALF: |
|
608 |
sp = (symbolSp[0]+inSqX, symbolSp[1]+ inSqY) |
|
609 |
else: |
|
610 |
sp = (symbolSp[0]+inSqX, symbolSp[1] + inSqY) |
|
611 |
tList = TOCR.getTextInfoInSymbol(bitImg, sp) |
|
612 |
|
|
613 |
resultText = '' |
|
614 |
if tList is not None: |
|
615 |
for index in range(len(tList)): |
|
616 |
textInfo = tList[index] |
|
617 |
if index != 0: |
|
618 |
resultText = resultText + "," |
|
619 |
resultText = resultText + textInfo.getText() |
|
620 |
cv2.putText(canvas, textInfo.getText(), (textInfo.getX(), textInfo.getY()), 2, 1.0, (0, 0, 0)) # cv2.FONT_HERSHEY_SIMPLEX |
|
621 |
|
|
622 |
# text value in symbol object update |
|
623 |
index = [i for i, item in enumerate(searchedSymbolList) if item.getSp() == symbolSp] |
|
624 |
if len(index) > 0: |
|
625 |
searchedSymbolList[index[0]].setText(resultText) |
|
626 |
except Exception as ex: |
|
627 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
|
628 |
finally: |
|
661 | 629 |
threadLock.release() |
662 | 630 |
|
663 | 631 |
|
내보내기 Unified diff