autoDialog.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. try:
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtWidgets import *
  5. except ImportError:
  6. from PyQt4.QtGui import *
  7. from PyQt4.QtCore import *
  8. import json
  9. import cv2
  10. import numpy as np
  11. from libs.utils import newIcon
  12. BB = QDialogButtonBox
  13. class Worker(QThread):
  14. progressBarValue = pyqtSignal(int)
  15. listValue = pyqtSignal(str)
  16. endsignal = pyqtSignal(int, str)
  17. handle = 0
  18. def __init__(self, ocr, mImgList, mainThread, model):
  19. super(Worker, self).__init__()
  20. self.ocr = ocr
  21. self.mImgList = mImgList
  22. self.mainThread = mainThread
  23. self.model = model
  24. self.setStackSize(1024*1024)
  25. def run(self):
  26. try:
  27. findex = 0
  28. for Imgpath in self.mImgList:
  29. if self.handle == 0:
  30. self.listValue.emit(Imgpath)
  31. if self.model == 'paddle':
  32. h, w, _ = cv2.imdecode(np.fromfile(Imgpath, dtype=np.uint8), 1).shape
  33. if h > 32 and w > 32:
  34. self.result_dic = self.ocr.ocr(Imgpath, cls=True, det=True)
  35. else:
  36. print('The size of', Imgpath, 'is too small to be recognised')
  37. self.result_dic = None
  38. # 结果保存
  39. if self.result_dic is None or len(self.result_dic) == 0:
  40. print('Can not recognise file', Imgpath)
  41. pass
  42. else:
  43. strs = ''
  44. for res in self.result_dic:
  45. chars = res[1][0]
  46. cond = res[1][1]
  47. posi = res[0]
  48. strs += "Transcription: " + chars + " Probability: " + str(cond) + \
  49. " Location: " + json.dumps(posi) +'\n'
  50. # Sending large amounts of data repeatedly through pyqtSignal may affect the program efficiency
  51. self.listValue.emit(strs)
  52. self.mainThread.result_dic = self.result_dic
  53. self.mainThread.filePath = Imgpath
  54. # 保存
  55. self.mainThread.saveFile(mode='Auto')
  56. findex += 1
  57. self.progressBarValue.emit(findex)
  58. else:
  59. break
  60. self.endsignal.emit(0, "readAll")
  61. self.exec()
  62. except Exception as e:
  63. print(e)
  64. raise
  65. class AutoDialog(QDialog):
  66. def __init__(self, text="Enter object label", parent=None, ocr=None, mImgList=None, lenbar=0):
  67. super(AutoDialog, self).__init__(parent)
  68. self.setFixedWidth(1000)
  69. self.parent = parent
  70. self.ocr = ocr
  71. self.mImgList = mImgList
  72. self.pb = QProgressBar()
  73. self.pb.setRange(0, lenbar)
  74. self.pb.setValue(0)
  75. layout = QVBoxLayout()
  76. layout.addWidget(self.pb)
  77. self.model = 'paddle'
  78. self.listWidget = QListWidget(self)
  79. layout.addWidget(self.listWidget)
  80. self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
  81. bb.button(BB.Ok).setIcon(newIcon('done'))
  82. bb.button(BB.Cancel).setIcon(newIcon('undo'))
  83. bb.accepted.connect(self.validate)
  84. bb.rejected.connect(self.reject)
  85. layout.addWidget(bb)
  86. bb.button(BB.Ok).setEnabled(False)
  87. self.setLayout(layout)
  88. # self.setWindowTitle("自动标注中")
  89. self.setWindowModality(Qt.ApplicationModal)
  90. # self.setWindowFlags(Qt.WindowCloseButtonHint)
  91. self.thread_1 = Worker(self.ocr, self.mImgList, self.parent, 'paddle')
  92. self.thread_1.progressBarValue.connect(self.handleProgressBarSingal)
  93. self.thread_1.listValue.connect(self.handleListWidgetSingal)
  94. self.thread_1.endsignal.connect(self.handleEndsignalSignal)
  95. def handleProgressBarSingal(self, i):
  96. self.pb.setValue(i)
  97. def handleListWidgetSingal(self, i):
  98. self.listWidget.addItem(i)
  99. titem = self.listWidget.item(self.listWidget.count() - 1)
  100. self.listWidget.scrollToItem(titem)
  101. def handleEndsignalSignal(self, i, str):
  102. if i == 0 and str == "readAll":
  103. self.buttonBox.button(BB.Ok).setEnabled(True)
  104. self.buttonBox.button(BB.Cancel).setEnabled(False)
  105. def reject(self):
  106. print("reject")
  107. self.thread_1.handle = -1
  108. self.thread_1.quit()
  109. # del self.thread_1
  110. # if self.thread_1.isRunning():
  111. # self.thread_1.terminate()
  112. # self.thread_1.quit()
  113. # super(AutoDialog,self).reject()
  114. while not self.thread_1.isFinished():
  115. pass
  116. self.accept()
  117. def validate(self):
  118. self.accept()
  119. def postProcess(self):
  120. try:
  121. self.edit.setText(self.edit.text().trimmed())
  122. # print(self.edit.text())
  123. except AttributeError:
  124. # PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
  125. self.edit.setText(self.edit.text())
  126. print(self.edit.text())
  127. def popUp(self):
  128. self.thread_1.start()
  129. return 1 if self.exec_() else None
  130. def closeEvent(self, event):
  131. print("???")
  132. # if self.thread_1.isRunning():
  133. # self.thread_1.quit()
  134. #
  135. # # self._thread.terminate()
  136. # # del self.thread_1
  137. # super(AutoDialog, self).closeEvent(event)
  138. self.reject()