-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-single.py
154 lines (118 loc) · 5.56 KB
/
main-single.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import sys
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QListWidget, QLabel, QFileDialog, QProgressDialog, QMessageBox, QSpinBox
from PySide6.QtGui import QPixmap
from PySide6.QtCore import Qt, QThread, Signal
from rapidocr_onnxruntime import RapidOCR
engine = RapidOCR()
class RecognitionThread(QThread):
progressUpdated = Signal(int)
finished = Signal()
def __init__(self, imageList, resultList):
super().__init__()
self.imageList = imageList
self.resultList = resultList
def run(self):
imageListSize = self.imageList.count()
for i in range(imageListSize):
imgPath = self.imageList.item(i).text()
result, elapse = engine(imgPath, use_det=True, use_cls=False, use_rec=True) # 检测+识别
texts = [item[1] for item in result]
target_text = '号码'
for text in texts:
if text.startswith(target_text):
# print(text) # 号码:01819689
numbers = ''.join(filter(str.isdigit, text))
print(numbers)
self.resultList.addItem(numbers)
progress = (i + 1) * 100 // imageListSize
self.progressUpdated.emit(progress)
self.finished.emit()
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 1000, 800)
# 左边布局
leftLayout = QVBoxLayout()
self.imageCountLabel = QLabel("图片数量: 0")
leftLayout.addWidget(self.imageCountLabel)
self.imageList = QListWidget()
leftLayout.addWidget(self.imageList)
# 中间布局
centerLayout = QVBoxLayout()
self.uploadButton = QPushButton("上传图片")
centerLayout.addWidget(self.uploadButton)
self.uploadButton.clicked.connect(self.uploadImage)
self.recognizeButton = QPushButton("识别")
centerLayout.addWidget(self.recognizeButton)
self.recognizeButton.clicked.connect(self.recognizeImage)
self.sbDiffValue = QSpinBox()
self.sbDiffValue.setRange(1, 5000)
self.sbDiffValue.setValue(100)
centerLayout.addWidget(self.sbDiffValue)
self.btnCheckResult = QPushButton("校验")
centerLayout.addWidget(self.btnCheckResult)
self.btnCheckResult.clicked.connect(self.checkResult)
self.btnClean = QPushButton("清除")
centerLayout.addWidget(self.btnClean)
self.btnClean.clicked.connect(self.cleanAll)
# 右边布局
rightLayout = QVBoxLayout()
self.resultCountLabel = QLabel("识别数量: 0")
rightLayout.addWidget(self.resultCountLabel)
self.resultList = QListWidget()
rightLayout.addWidget(self.resultList)
# 主布局
mainLayout = QHBoxLayout()
mainLayout.addLayout(leftLayout)
mainLayout.addLayout(centerLayout)
mainLayout.addLayout(rightLayout)
self.setLayout(mainLayout)
def uploadImage(self):
filePaths, _ = QFileDialog.getOpenFileNames(self, "选择图片", "", "图片文件 (*.jpg *.png *.bmp)")
if filePaths:
for filePath in filePaths:
self.imageList.addItem(filePath)
self.imageCountLabel.setText(f"图片数量: {self.imageList.count()}")
def recognizeImage(self):
self.loading_dialog = QProgressDialog("正在识别...", "取消", 0, 100, self)
self.loading_dialog.setWindowModality(Qt.WindowModal)
self.loading_dialog.show()
self.recognitionThread = RecognitionThread(self.imageList, self.resultList)
self.recognitionThread.progressUpdated.connect(self.updateProgressDialog)
self.recognitionThread.finished.connect(self.recognitionFinished)
self.recognitionThread.start()
def checkResult(self):
diffValue = self.sbDiffValue.value()
if diffValue<=0:
QMessageBox.warning(self, "异常", "请设定合理的临界值")
return
for i in range(self.resultList.count() - 1):
item1 = self.resultList.item(i).text()
item2 = self.resultList.item(i + 1).text()
# 这里假设item1和item2都是数字,你可以根据实际情况修改
if item1.isdigit() and item2.isdigit():
diff = abs(int(item1) - int(item2))
if diff < 100:
QMessageBox.warning(self, "错误", f"发现相邻号码差值小于100, 号码: {item1} 和 {item2}")
return
else:
QMessageBox.warning(self, f"识别结果不是数字: {item1} 或 {item2}")
QMessageBox.information(self, "校验通过", "未发现异常号码")
def updateProgressDialog(self, progress):
self.loading_dialog.setValue(progress)
def recognitionFinished(self):
self.loading_dialog.close()
self.resultCountLabel.setText(f"识别数量: {self.resultList.count()}")
self.resultList.sortItems()
def cleanAll(self):
self.imageList.clear()
self.resultList.clear()
self.imageCountLabel.setText(f"图片数量: {self.imageList.count()}")
self.resultCountLabel.setText(f"识别数量: {self.resultList.count()}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())