개정판 40460006
Azure OCR 시 이미지 회전(0, 90, 180, 270) 에 따른 영역좌표 개선
DTI_PID/DTI_PID/DTI_PID.py | ||
---|---|---|
108 | 108 |
roi = img[y:y+height, x:x+width] |
109 | 109 |
temp = roi.copy() |
110 | 110 |
tempBin = cv2.bitwise_not(temp) |
111 |
roi = cv2.bitwise_xor(roi, tempBin) |
|
111 |
img[y:y+height, x:x+width] = cv2.bitwise_xor(roi, tempBin) |
|
112 |
return img |
|
112 | 113 |
|
113 | 114 |
#Convert into Grayscale image |
114 | 115 |
def cvtGrayImage(img): |
... | ... | |
820 | 821 |
global MIN_TEXT_SIZE |
821 | 822 |
for textInfo in textInfoList: |
822 | 823 |
if textInfo.getW() >= MIN_TEXT_SIZE or textInfo.getH() >= MIN_TEXT_SIZE: |
823 |
removeText(ocrCompletedSrc, textInfo.getText(), textInfo.getX(), textInfo.getY(), textInfo.getW(), textInfo.getH()) |
|
824 |
ocrCompletedSrc = removeText(ocrCompletedSrc, textInfo.getText(), textInfo.getX(), textInfo.getY(), textInfo.getW(), textInfo.getH())
|
|
824 | 825 |
except Exception as ex: |
825 | 826 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
826 | 827 |
|
DTI_PID/DTI_PID/azure_ocr_module.py | ||
---|---|---|
12 | 12 |
from io import BytesIO |
13 | 13 |
import TextInfo as ti |
14 | 14 |
|
15 |
try: |
|
16 |
from PyQt5.QtCore import * |
|
17 |
from PyQt5.QtGui import * |
|
18 |
except: |
|
19 |
from PyQt4.QtCore import * |
|
20 |
from PyQt4.QtGui import * |
|
21 |
|
|
15 | 22 |
EXCEPTION_LIST = ['0', 'o', 'O' |
16 | 23 |
, '-0', '-o', '-O', '—0', '—o', '—O'] |
17 | 24 |
STANDARD_SIZE = 30 |
... | ... | |
30 | 37 |
angle = 0 |
31 | 38 |
return angle |
32 | 39 |
|
40 |
''' |
|
41 |
@history 18.04.25 Jeongwoo Calculate Left-Top Coord by QRect and QTransform |
|
42 |
''' |
|
33 | 43 |
def getCoordOnRotatedImage(rAngle, x, y, originImageWidth, originImageHeight, otw, oth): |
34 | 44 |
rx = None |
35 | 45 |
ry = None |
36 |
if (rAngle == "Up"): |
|
37 |
rx = x |
|
38 |
ry = y |
|
39 |
elif rAngle == "Left": |
|
40 |
rx = y |
|
41 |
ry = originImageHeight - x - otw |
|
42 |
elif rAngle == "Down": |
|
43 |
rx = originImageHeight - x - oth |
|
44 |
ry = originImageWidth - y - otw |
|
45 |
elif rAngle == "Right": |
|
46 |
rx = originImageWidth - y - oth |
|
47 |
ry = x |
|
48 |
else: # general case |
|
49 |
pass |
|
50 |
return (rx, ry) |
|
46 |
transform = QTransform() |
|
47 |
if rAngle == 90 or rAngle == 270: |
|
48 |
transform.translate(originImageHeight*0.5, originImageWidth*0.5) |
|
49 |
elif rAngle == 0 or rAngle == 360: |
|
50 |
transform.translate(originImageWidth*0.5, originImageHeight*0.5) |
|
51 |
transform.rotate(-abs(rAngle)) |
|
52 |
transform.translate(-originImageWidth*0.5, -originImageHeight*0.5) |
|
53 |
rect = QRect(x, y, otw, oth) |
|
54 |
rect = transform.mapRect(rect) |
|
55 |
rx = rect.x() |
|
56 |
ry = rect.y() |
|
57 |
rwidth = rect.width() |
|
58 |
rheight = rect.height() |
|
59 |
return (rx, ry, rwidth, rheight) |
|
51 | 60 |
|
52 | 61 |
def getRollbackRotateCodeByOrientation(orientation): |
53 | 62 |
rotatedAngle = None |
... | ... | |
121 | 130 |
#body = |
122 | 131 |
#"{'url':'https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png'}" |
123 | 132 |
#body = "{'url':'http://cfile9.uf.tistory.com/image/99AFE1505A322DBD114994'}" |
124 |
|
|
133 |
''' |
|
134 |
@history 18.04.25 Jeongwoo Init TextInfo Object with getCoordOnRotatedImage() |
|
135 |
''' |
|
125 | 136 |
def detectAndRemoveText(image, offsetX, offsetY): |
126 | 137 |
tmp = image.copy() |
127 | 138 |
img = np.array(tmp) |
... | ... | |
170 | 181 |
for cs in parsed['regions']: |
171 | 182 |
for re in cs['lines']: |
172 | 183 |
strArr = re['boundingBox'].split(',') |
173 |
width = int(strArr[2]) if orientation == "Up" or orientation == "Down" else int(strArr[3]) |
|
174 |
height = int(strArr[3]) if orientation == "Up" or orientation == "Down" else int(strArr[2]) |
|
175 |
pt = getCoordOnRotatedImage(orientation, int(strArr[0]), int(strArr[1]), w, h, int(strArr[2]), int(strArr[3])) |
|
184 |
(rx, ry, rwidth, rheight) = getCoordOnRotatedImage(getRotatedAngleByOrientation(orientation), int(strArr[0]), int(strArr[1]), w if orientation == 'Up' or orientation == 'Down' else h, h if orientation == 'Up' or orientation == 'Down' else w, int(strArr[2]), int(strArr[3])) |
|
176 | 185 |
text = "" |
177 | 186 |
for words in re['words']: |
178 | 187 |
print(words) |
179 | 188 |
text = text + " " + words['text'] |
180 |
#strArr = words['boundingBox'].split(',') |
|
181 |
#width = int(strArr[2]) if orientation == "Up" or orientation == "Down" else int(strArr[3]) |
|
182 |
#height = int(strArr[3]) if orientation == "Up" or orientation == "Down" else int(strArr[2]) |
|
183 |
#pt = getCoordOnRotatedImage(orientation, int(strArr[0]), int(strArr[1]), w, h, int(strArr[2]), int(strArr[3])) |
|
184 |
#pt = (int(strArr[0]), int(strArr[1])) |
|
185 |
#width = int(strArr[2]) |
|
186 |
#height = int(strArr[3]) |
|
187 |
|
|
188 |
#roi = img[pt[1]:pt[1]+height, pt[0]:pt[0]+width] |
|
189 |
#temp = roi.copy() |
|
190 |
#tempBin = cv2.bitwise_not(temp) |
|
191 |
#roi = cv2.bitwise_xor(roi, tempBin, roi) |
|
192 |
#tInfo.append(ti.TextInfo(text, strArr[0], strArr[1], strArr[2], strArr[3])) |
|
193 |
|
|
189 |
|
|
190 |
#print(text) |
|
194 | 191 |
if not(text in EXCEPTION_LIST and height < STANDARD_SIZE): |
195 |
tInfo.append(ti.TextInfo(text.strip(), pt[0] + offsetX, pt[1] + offsetY, width, height, getRotatedAngleByOrientation(orientation)))
|
|
192 |
tInfo.append(ti.TextInfo(text.strip(), rx + int(offsetX), ry + int(offsetY), rwidth, rheight, getRotatedAngleByOrientation(orientation)))
|
|
196 | 193 |
count = count + 1 |
197 | 194 |
|
198 | 195 |
print("OCR Result count : " + str(count)) |
... | ... | |
207 | 204 |
|
208 | 205 |
return (img, tInfo) |
209 | 206 |
|
210 |
def removeText(imagePath): |
|
211 |
image = Image.open(imagePath) |
|
212 |
ret = np.array(image) |
|
213 |
textInfoList = [] |
|
207 |
#def removeText(imagePath):
|
|
208 |
# image = Image.open(imagePath)
|
|
209 |
# ret = np.array(image)
|
|
210 |
# textInfoList = []
|
|
214 | 211 |
|
215 |
for i in range(1): |
|
216 |
(ret, tInfo) = detectAndRemoveText(Image.fromarray(ret)) |
|
217 |
#ret = cv2.rotate(ret, cv2.ROTATE_90_CLOCKWISE) |
|
218 |
textInfoList.extend(tInfo) |
|
212 |
# for i in range(1):
|
|
213 |
# (ret, tInfo) = detectAndRemoveText(Image.fromarray(ret))
|
|
214 |
# #ret = cv2.rotate(ret, cv2.ROTATE_90_CLOCKWISE)
|
|
215 |
# textInfoList.extend(tInfo)
|
|
219 | 216 |
|
220 |
image.close() |
|
221 |
return (ret, textInfoList) |
|
217 |
# image.close()
|
|
218 |
# return (ret, textInfoList)
|
|
222 | 219 |
|
223 | 220 |
def removeTextFromNpArray(img, offsetX, offsetY): |
224 | 221 |
ret = img.copy() |
225 | 222 |
textInfoList = [] |
226 |
|
|
223 |
imgW, imgH = ret.shape[::-1] |
|
227 | 224 |
for i in range(1): |
228 | 225 |
(ret, tInfo) = detectAndRemoveText(Image.fromarray(ret), offsetX, offsetY) |
229 | 226 |
#ret = cv2.rotate(ret, cv2.ROTATE_90_CLOCKWISE) |
... | ... | |
231 | 228 |
|
232 | 229 |
return (ret, textInfoList) |
233 | 230 |
|
231 |
## NOT USED |
|
232 |
def removeText(img, text, x, y, imgW, imgH, width, height, angle): |
|
233 |
roi = img[y:y+height, x:x+width] |
|
234 |
w, h = roi.shape[::-1] |
|
235 |
if w > 0 and h > 0: |
|
236 |
temp = roi.copy() |
|
237 |
tempBin = cv2.bitwise_not(temp) |
|
238 |
img[y:y+height, x:x+width] = cv2.bitwise_xor(roi, tempBin) |
|
239 |
return img |
|
240 |
|
|
234 | 241 |
def getTextInfoFromNpArray(image): |
235 | 242 |
try: |
236 | 243 |
tmp = Image.fromarray(image) |
내보내기 Unified diff