Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.

Commit 9365924

Browse files
committed
Fix: desktop.py;
1 parent a170d69 commit 9365924

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@
66
/output_dup/
77
/output_fail/
88
/output_suc/
9-
/pick_stu_number.py
10-
/desktop.spec
119
/build/
1210
/dist/

desktop.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,18 @@ def main():
144144
print('请在处理完毕后再关闭本窗口\n')
145145
print('-' * 30)
146146

147-
PickStuNumber(values.get(img_folder.get('key')), values.get(show_img.get('key'))).write_out(
148-
values.get(output.get('key')), values.get(output_suc.get('key')),
149-
values.get(output_dup.get('key')),
150-
values.get(output_fail.get('key')))
147+
PickStuNumber(
148+
values.get(img_folder.get('key')),
149+
sg.user_settings_get_entry(show_img.get('key'), show_img.get('default'))) \
150+
.write_out(
151+
sg.user_settings_get_entry(output.get('key'), output.get('default')),
152+
sg.user_settings_get_entry(output_suc.get('key'), output_suc.get('default')),
153+
sg.user_settings_get_entry(output_dup.get('key'), output_dup.get('default')),
154+
sg.user_settings_get_entry(output_fail.get('key'), output_fail.get('default')))
151155

152156
print()
153-
print('-' * 30)
154157
print('处理完毕')
158+
print('-' * 30)
155159

156160
# 启用关闭
157161
window.DisableClose = False

pick_stu_number.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class PickStuNumber:
2121
def __init__(self, path: str, show_img: bool = False):
22-
self.__ext = {'jpg', 'jpeg', 'png'}
22+
self.__ext = {'jpg', 'jpeg'}
2323
self.__ocr = CnOcr(model_name='densenet-lite-gru', cand_alphabet=string.digits, name=path)
2424
self.__std = CnStd(name=path)
2525
self.__info_dict = {}
@@ -31,8 +31,9 @@ def __init__(self, path: str, show_img: bool = False):
3131
# 根据传入的路径判断操作
3232
if os.path.isdir(path) or os.path.isfile(path):
3333
files = [self.__format_path(os.path.join(path, f)) for f in os.listdir(path) if
34-
os.path.isfile(os.path.join(path, f)) and self.__is_image(f)] \
35-
if os.path.isdir(path) else [path]
34+
(os.path.isfile(os.path.join(path, f)) and self.__is_image(f))] \
35+
if os.path.isdir(path) \
36+
else [path]
3637
for file in tqdm(files):
3738
self.__handle_info(file,
3839
self.__ocr_number(self.__std_number(self.__cutter(file, show_img))))
@@ -63,11 +64,18 @@ def __cutter(path: str, show_img: bool = False) -> numpy.ndarray:
6364
:param show_img: 是否需要展示图片
6465
:return: 图片对应的 ndarray
6566
"""
67+
print(path)
68+
6669
# 以灰度模式读取图片
6770
origin_img = cv2.imread(path, 0)
6871

72+
if show_img:
73+
# 自由拉伸窗口
74+
# cv2.namedWindow('bin img', 0)
75+
cv2.imshow('origin img', origin_img)
76+
6977
# 切出一部分,取值是经验值
70-
origin_img = origin_img[:origin_img.shape[0] // 3 * 2]
78+
origin_img = origin_img[:origin_img.shape[0] // 2]
7179

7280
# 二值化
7381
_, origin_img = cv2.threshold(origin_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
@@ -92,7 +100,7 @@ def __cutter(path: str, show_img: bool = False) -> numpy.ndarray:
92100
x, y, w, h = cv2.boundingRect(contours[1])
93101

94102
# 目前所有的数值设定使用的是经验值
95-
if w * h > 10000:
103+
if w * h > 250000:
96104
# 需要识别的学号部分
97105
# 左上角坐标
98106
left_top_x = x
@@ -103,9 +111,9 @@ def __cutter(path: str, show_img: bool = False) -> numpy.ndarray:
103111

104112
img = origin_img[left_top_y:right_down_y, left_top_x:right_down_x]
105113
else:
106-
img = origin_img[160:origin_img.shape[0] // 2]
114+
img = origin_img[120:]
107115
else:
108-
img = origin_img[160:origin_img.shape[0] // 2]
116+
img = origin_img[120:]
109117

110118
# 对切出的图片进行再次处理,以便图像识别
111119
kernel = numpy.ones((2, 2), dtype=numpy.uint8)
@@ -137,7 +145,7 @@ def __std_number(self, img: numpy.ndarray):
137145
:param img:
138146
:return:
139147
"""
140-
return [i['cropped_img'] for i in self.__std.detect(img)][:2]
148+
return [i['cropped_img'] for i in self.__std.detect(img)]
141149

142150
@staticmethod
143151
def __handle_result_list(result_list: List[List[str]]) -> [str, bool]:
@@ -147,11 +155,15 @@ def __handle_result_list(result_list: List[List[str]]) -> [str, bool]:
147155
:return: 结果,是否有效
148156
"""
149157
result = result_list[0]
150-
if len(result) >= 12:
151-
result = ''.join(result[:12])
152-
return result, re.match(r'\d{12}', result) is not None
153-
else:
154-
return None, False
158+
159+
if len(result) < 12 and len(result_list) > 1:
160+
for i in result_list:
161+
if len(i) >= 12:
162+
result = i
163+
164+
result = ''.join(result[:12] if len(result) >= 12 else result)
165+
print(result, re.match(r'\d{12}', result) is not None)
166+
return result, re.match(r'\d{12}', result) is not None
155167

156168
def __handle_dup_name(self, name, path):
157169
dup_keys = self.__dup_name_dict.get(name)
@@ -233,10 +245,10 @@ def write_out(self,
233245
elif value.get('legal') is True and value.get('dup') is True:
234246
index = self.__dup_name_dict[value.get("name")].index(key)
235247
copyfile(key,
236-
os.path.join(dup,
237-
f'{value.get("name")}.{index}.{value.get("suffix")}'))
248+
os.path.join(dup, f'{value.get("name")}.{index}.{value.get("suffix")}'))
238249
else:
239-
copyfile(key, os.path.join(fail, os.path.split(key)[1]))
250+
copyfile(key,
251+
os.path.join(fail, f'{value.get("name")}.{value.get("suffix")}' or os.path.split(key)[1]))
240252
else:
241253
print(f'“{path}” 并非一个合法的路径!')
242254

0 commit comments

Comments
 (0)