@@ -61,11 +61,12 @@ def _cleanse_roi(self, raw_text):
61
61
selection_length = len (item )
62
62
break
63
63
64
- new_list = [item for item in input_list if len (item ) >= selection_length ]
65
-
66
- output_text = '\n ' .join (new_list )
67
-
68
- return output_text
64
+ if selection_length is None :
65
+ return ''
66
+ else :
67
+ new_list = [item for item in input_list if len (item ) >= selection_length ]
68
+ output_text = '\n ' .join (new_list )
69
+ return output_text
69
70
70
71
def _get_final_check_digit (self , input_string , input_type ):
71
72
if input_type == 'TD3' :
@@ -128,6 +129,16 @@ def get_mrz(self, image_path):
128
129
else :
129
130
return {'status' : 'FAILURE' , 'message' : file_status }
130
131
132
+ def _get_date_of_birth (self , date_of_birth_str , date_of_expiry_str ):
133
+ birth_year = int (date_of_birth_str [:4 ])
134
+ expiry_year = int (date_of_expiry_str [:4 ])
135
+
136
+ if expiry_year <= birth_year :
137
+ adjusted_year = birth_year - 100
138
+ return f"{ adjusted_year } -{ date_of_birth_str [5 :]} "
139
+ else :
140
+ return date_of_birth_str
141
+
131
142
def _parse_mrz (self , mrz_text ):
132
143
mrz_lines = mrz_text .strip ().split ('\n ' )
133
144
if len (mrz_lines ) not in [2 , 3 ]:
@@ -140,6 +151,8 @@ def _parse_mrz(self, mrz_text):
140
151
# Line 1
141
152
mrz_code_dict ['document_type' ] = mrz_lines [0 ][:2 ].strip ('<' )
142
153
mrz_code_dict ['country_code' ] = mrz_lines [0 ][2 :5 ]
154
+ if not mrz_code_dict ['country_code' ].isalpha ():
155
+ return {'status' : 'FAILURE' , 'message' : 'Invalid MRZ format' }
143
156
names = mrz_lines [0 ][5 :].split ('<<' )
144
157
mrz_code_dict ['surname' ] = names [0 ].replace ('<' , ' ' )
145
158
mrz_code_dict ['given_name' ] = names [1 ].replace ('<' , ' ' )
@@ -149,6 +162,8 @@ def _parse_mrz(self, mrz_text):
149
162
if self ._get_check_digit (mrz_code_dict ['document_number' ]) != mrz_lines [1 ][9 ]:
150
163
return {'status' : 'FAILURE' , 'message' : 'document number checksum is not matching' }
151
164
mrz_code_dict ['nationality' ] = mrz_lines [1 ][10 :13 ]
165
+ if not mrz_code_dict ['nationality' ].isalpha ():
166
+ return {'status' : 'FAILURE' , 'message' : 'Invalid MRZ format' }
152
167
mrz_code_dict ['date_of_birth' ] = mrz_lines [1 ][13 :19 ]
153
168
if self ._get_check_digit (mrz_code_dict ['date_of_birth' ]) != mrz_lines [1 ][19 ]:
154
169
return {'status' : 'FAILURE' , 'message' : 'date of birth checksum is not matching' }
@@ -158,6 +173,8 @@ def _parse_mrz(self, mrz_text):
158
173
if self ._get_check_digit (mrz_code_dict ['date_of_expiry' ]) != mrz_lines [1 ][27 ]:
159
174
return {'status' : 'FAILURE' , 'message' : 'date of expiry checksum is not matching' }
160
175
mrz_code_dict ['date_of_expiry' ] = self ._format_date (mrz_code_dict ['date_of_expiry' ])
176
+ mrz_code_dict ['date_of_birth' ] = self ._get_date_of_birth (mrz_code_dict ['date_of_birth' ],
177
+ mrz_code_dict ['date_of_expiry' ])
161
178
if mrz_code_dict ['mrz_type' ] == 'TD3' :
162
179
mrz_code_dict ['optional_data' ] = mrz_lines [1 ][28 :35 ].strip ('<' )
163
180
@@ -173,6 +190,8 @@ def _parse_mrz(self, mrz_text):
173
190
# Line 1
174
191
mrz_code_dict ['document_type' ] = mrz_lines [0 ][:2 ].strip ('<' )
175
192
mrz_code_dict ['country_code' ] = mrz_lines [0 ][2 :5 ]
193
+ if not mrz_code_dict ['country_code' ].isalpha ():
194
+ return {'status' : 'FAILURE' , 'message' : 'Invalid MRZ format' }
176
195
mrz_code_dict ['document_number' ] = mrz_lines [0 ][5 :14 ]
177
196
if self ._get_check_digit (mrz_code_dict ['document_number' ]) != mrz_lines [0 ][14 ]:
178
197
return {'status' : 'FAILURE' , 'message' : 'document number checksum is not matching' }
@@ -188,7 +207,11 @@ def _parse_mrz(self, mrz_text):
188
207
if self ._get_check_digit (mrz_code_dict ['date_of_expiry' ]) != mrz_lines [1 ][14 ]:
189
208
return {'status' : 'FAILURE' , 'message' : 'date of expiry checksum is not matching' }
190
209
mrz_code_dict ['date_of_expiry' ] = self ._format_date (mrz_code_dict ['date_of_expiry' ])
210
+ mrz_code_dict ['date_of_birth' ] = self ._get_date_of_birth (mrz_code_dict ['date_of_birth' ],
211
+ mrz_code_dict ['date_of_expiry' ])
191
212
mrz_code_dict ['nationality' ] = mrz_lines [1 ][15 :18 ]
213
+ if not mrz_code_dict ['nationality' ].isalpha ():
214
+ return {'status' : 'FAILURE' , 'message' : 'Invalid MRZ format' }
192
215
mrz_code_dict ['optional_data_2' ] = mrz_lines [0 ][18 :29 ].strip ('<' )
193
216
if mrz_lines [1 ][- 1 ] != self ._get_final_check_digit (mrz_lines , mrz_code_dict ['mrz_type' ]):
194
217
return {'status' : 'FAILURE' , 'message' : 'final checksum is not matching' }
0 commit comments