19
19
20
20
class PickStuNumber :
21
21
def __init__ (self , path : str , show_img : bool = False ):
22
- self .__ext = {'jpg' , 'jpeg' , 'png' }
22
+ self .__ext = {'jpg' , 'jpeg' }
23
23
self .__ocr = CnOcr (model_name = 'densenet-lite-gru' , cand_alphabet = string .digits , name = path )
24
24
self .__std = CnStd (name = path )
25
25
self .__info_dict = {}
@@ -31,8 +31,9 @@ def __init__(self, path: str, show_img: bool = False):
31
31
# 根据传入的路径判断操作
32
32
if os .path .isdir (path ) or os .path .isfile (path ):
33
33
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 ]
36
37
for file in tqdm (files ):
37
38
self .__handle_info (file ,
38
39
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:
63
64
:param show_img: 是否需要展示图片
64
65
:return: 图片对应的 ndarray
65
66
"""
67
+ print (path )
68
+
66
69
# 以灰度模式读取图片
67
70
origin_img = cv2 .imread (path , 0 )
68
71
72
+ if show_img :
73
+ # 自由拉伸窗口
74
+ # cv2.namedWindow('bin img', 0)
75
+ cv2 .imshow ('origin img' , origin_img )
76
+
69
77
# 切出一部分,取值是经验值
70
- origin_img = origin_img [:origin_img .shape [0 ] // 3 * 2 ]
78
+ origin_img = origin_img [:origin_img .shape [0 ] // 2 ]
71
79
72
80
# 二值化
73
81
_ , 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:
92
100
x , y , w , h = cv2 .boundingRect (contours [1 ])
93
101
94
102
# 目前所有的数值设定使用的是经验值
95
- if w * h > 10000 :
103
+ if w * h > 250000 :
96
104
# 需要识别的学号部分
97
105
# 左上角坐标
98
106
left_top_x = x
@@ -103,9 +111,9 @@ def __cutter(path: str, show_img: bool = False) -> numpy.ndarray:
103
111
104
112
img = origin_img [left_top_y :right_down_y , left_top_x :right_down_x ]
105
113
else :
106
- img = origin_img [160 : origin_img . shape [ 0 ] // 2 ]
114
+ img = origin_img [120 : ]
107
115
else :
108
- img = origin_img [160 : origin_img . shape [ 0 ] // 2 ]
116
+ img = origin_img [120 : ]
109
117
110
118
# 对切出的图片进行再次处理,以便图像识别
111
119
kernel = numpy .ones ((2 , 2 ), dtype = numpy .uint8 )
@@ -137,7 +145,7 @@ def __std_number(self, img: numpy.ndarray):
137
145
:param img:
138
146
:return:
139
147
"""
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 )]
141
149
142
150
@staticmethod
143
151
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]:
147
155
:return: 结果,是否有效
148
156
"""
149
157
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
155
167
156
168
def __handle_dup_name (self , name , path ):
157
169
dup_keys = self .__dup_name_dict .get (name )
@@ -233,10 +245,10 @@ def write_out(self,
233
245
elif value .get ('legal' ) is True and value .get ('dup' ) is True :
234
246
index = self .__dup_name_dict [value .get ("name" )].index (key )
235
247
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" )} ' ))
238
249
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 ]))
240
252
else :
241
253
print (f'“{ path } ” 并非一个合法的路径!' )
242
254
0 commit comments