Skip to content

Commit

Permalink
Fix: desktop.py;
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeYoung committed Dec 10, 2020
1 parent a170d69 commit 9365924
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
/output_dup/
/output_fail/
/output_suc/
/pick_stu_number.py
/desktop.spec
/build/
/dist/
14 changes: 9 additions & 5 deletions desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,18 @@ def main():
print('请在处理完毕后再关闭本窗口\n')
print('-' * 30)

PickStuNumber(values.get(img_folder.get('key')), values.get(show_img.get('key'))).write_out(
values.get(output.get('key')), values.get(output_suc.get('key')),
values.get(output_dup.get('key')),
values.get(output_fail.get('key')))
PickStuNumber(
values.get(img_folder.get('key')),
sg.user_settings_get_entry(show_img.get('key'), show_img.get('default'))) \
.write_out(
sg.user_settings_get_entry(output.get('key'), output.get('default')),
sg.user_settings_get_entry(output_suc.get('key'), output_suc.get('default')),
sg.user_settings_get_entry(output_dup.get('key'), output_dup.get('default')),
sg.user_settings_get_entry(output_fail.get('key'), output_fail.get('default')))

print()
print('-' * 30)
print('处理完毕')
print('-' * 30)

# 启用关闭
window.DisableClose = False
Expand Down
44 changes: 28 additions & 16 deletions pick_stu_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class PickStuNumber:
def __init__(self, path: str, show_img: bool = False):
self.__ext = {'jpg', 'jpeg', 'png'}
self.__ext = {'jpg', 'jpeg'}
self.__ocr = CnOcr(model_name='densenet-lite-gru', cand_alphabet=string.digits, name=path)
self.__std = CnStd(name=path)
self.__info_dict = {}
Expand All @@ -31,8 +31,9 @@ def __init__(self, path: str, show_img: bool = False):
# 根据传入的路径判断操作
if os.path.isdir(path) or os.path.isfile(path):
files = [self.__format_path(os.path.join(path, f)) for f in os.listdir(path) if
os.path.isfile(os.path.join(path, f)) and self.__is_image(f)] \
if os.path.isdir(path) else [path]
(os.path.isfile(os.path.join(path, f)) and self.__is_image(f))] \
if os.path.isdir(path) \
else [path]
for file in tqdm(files):
self.__handle_info(file,
self.__ocr_number(self.__std_number(self.__cutter(file, show_img))))
Expand Down Expand Up @@ -63,11 +64,18 @@ def __cutter(path: str, show_img: bool = False) -> numpy.ndarray:
:param show_img: 是否需要展示图片
:return: 图片对应的 ndarray
"""
print(path)

# 以灰度模式读取图片
origin_img = cv2.imread(path, 0)

if show_img:
# 自由拉伸窗口
# cv2.namedWindow('bin img', 0)
cv2.imshow('origin img', origin_img)

# 切出一部分,取值是经验值
origin_img = origin_img[:origin_img.shape[0] // 3 * 2]
origin_img = origin_img[:origin_img.shape[0] // 2]

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

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

img = origin_img[left_top_y:right_down_y, left_top_x:right_down_x]
else:
img = origin_img[160:origin_img.shape[0] // 2]
img = origin_img[120:]
else:
img = origin_img[160:origin_img.shape[0] // 2]
img = origin_img[120:]

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

@staticmethod
def __handle_result_list(result_list: List[List[str]]) -> [str, bool]:
Expand All @@ -147,11 +155,15 @@ def __handle_result_list(result_list: List[List[str]]) -> [str, bool]:
:return: 结果,是否有效
"""
result = result_list[0]
if len(result) >= 12:
result = ''.join(result[:12])
return result, re.match(r'\d{12}', result) is not None
else:
return None, False

if len(result) < 12 and len(result_list) > 1:
for i in result_list:
if len(i) >= 12:
result = i

result = ''.join(result[:12] if len(result) >= 12 else result)
print(result, re.match(r'\d{12}', result) is not None)
return result, re.match(r'\d{12}', result) is not None

def __handle_dup_name(self, name, path):
dup_keys = self.__dup_name_dict.get(name)
Expand Down Expand Up @@ -233,10 +245,10 @@ def write_out(self,
elif value.get('legal') is True and value.get('dup') is True:
index = self.__dup_name_dict[value.get("name")].index(key)
copyfile(key,
os.path.join(dup,
f'{value.get("name")}.{index}.{value.get("suffix")}'))
os.path.join(dup, f'{value.get("name")}.{index}.{value.get("suffix")}'))
else:
copyfile(key, os.path.join(fail, os.path.split(key)[1]))
copyfile(key,
os.path.join(fail, f'{value.get("name")}.{value.get("suffix")}' or os.path.split(key)[1]))
else:
print(f'“{path}” 并非一个合法的路径!')

Expand Down

0 comments on commit 9365924

Please sign in to comment.