217 |
217 |
|
218 |
218 |
# try to detect text if there is no result of detection or
|
219 |
219 |
# count of text info list not match with count of split text
|
220 |
|
if not self.textInfoList or (len(self.textInfoList) != len(splitText)):
|
|
220 |
if isSplit and self.textInfoList and (len(self.textInfoList) == len(splitText)):
|
|
221 |
for index in range(len(self.textInfoList)):
|
|
222 |
self.textInfoList[index].setText(splitText[index])
|
|
223 |
elif isSplit and not self.textInfoList:
|
221 |
224 |
self.detect_text()
|
|
225 |
if len(self.textInfoList) == len(splitText[index]):
|
|
226 |
for index in range(len(self.textInfoList)):
|
|
227 |
self.textInfoList[index].setText(splitText[index])
|
|
228 |
else:
|
|
229 |
self.textInfoList = self.getMergedTextInfo(text)
|
|
230 |
else:
|
|
231 |
self.textInfoList = self.getMergedTextInfo(text)
|
222 |
232 |
|
223 |
|
if not isSplit:
|
|
233 |
'''
|
224 |
234 |
minX, minY, maxX, maxY = sys.maxsize, sys.maxsize, 0, 0
|
225 |
235 |
for textInfo in self.textInfoList:
|
226 |
236 |
x, y, w, h = textInfo.getX(), textInfo.getY(), textInfo.getW(), textInfo.getH()
|
... | ... | |
230 |
240 |
maxY = max(y + h, maxY)
|
231 |
241 |
|
232 |
242 |
self.textInfoList = [TextInfo(text, minX, minY, maxX - minX, maxY - minY, 0)]
|
|
243 |
'''
|
233 |
244 |
|
|
245 |
'''
|
234 |
246 |
if self.textInfoList:
|
235 |
247 |
if self._text_item:
|
236 |
248 |
textInfo = self.textInfoList[0]
|
... | ... | |
264 |
276 |
self._text_item.update_shape()
|
265 |
277 |
|
266 |
278 |
self.textInfoList = self.textInfoList[1:]
|
|
279 |
'''
|
267 |
280 |
|
268 |
|
QDialog.accept(self)
|
|
281 |
QDialog.accept(self)
|
269 |
282 |
|
270 |
283 |
except Exception as ex:
|
271 |
284 |
from App import App
|
... | ... | |
273 |
286 |
sys.exc_info()[-1].tb_lineno)
|
274 |
287 |
App.mainWnd().addMessage.emit(MessageType.Error, message)
|
275 |
288 |
|
|
289 |
def getMergedTextInfo(self, text):
|
|
290 |
import cv2
|
|
291 |
from TextInfo import TextInfo
|
|
292 |
|
|
293 |
buffer = QBuffer()
|
|
294 |
buffer.open(QBuffer.ReadWrite)
|
|
295 |
self.image.save(buffer, "PNG")
|
|
296 |
pyImage = Image.open(io.BytesIO(buffer.data()))
|
|
297 |
img = np.array(pyImage)
|
|
298 |
|
|
299 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
300 |
imgNot = np.ones(img.shape, np.uint8)
|
|
301 |
cv2.bitwise_not(img, imgNot)
|
|
302 |
imgNot = cv2.dilate(imgNot, np.ones((8, 8), np.uint8))
|
|
303 |
|
|
304 |
contours, hierarchy = cv2.findContours(imgNot, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
305 |
minX, minY, maxX, maxY = sys.maxsize, sys.maxsize, 0, 0
|
|
306 |
if len(contours) is 0:
|
|
307 |
minX, minY, maxX, maxY = self.boundingBox.x(), self.boundingBox.y(), self.boundingBox.x() + self.image.width(), self.boundingBox.y() + self.image.height()
|
|
308 |
else:
|
|
309 |
minX, minY, maxX, maxY = sys.maxsize, sys.maxsize, 0, 0
|
|
310 |
for cnt in contours:
|
|
311 |
x, y, w, h = cv2.boundingRect(cnt)
|
|
312 |
minX = min(x, minX)
|
|
313 |
minY = min(y, minY)
|
|
314 |
maxX = max(x + w, maxX)
|
|
315 |
maxY = max(y + h, maxY)
|
|
316 |
minX, minY, maxX, maxY = minX + self.boundingBox.x(), minY + self.boundingBox.y(), maxX + self.boundingBox.x(), maxY + self.boundingBox.y()
|
|
317 |
|
|
318 |
return [TextInfo(text, minX, minY, maxX - minX, maxY - minY, 0)]
|
|
319 |
|
276 |
320 |
def reject(self):
|
277 |
321 |
self.isAccepted = False
|
278 |
322 |
self.textInfoList = None
|