Skip to content

Commit 79c3841

Browse files
added dob fn
1 parent 195e42b commit 79c3841

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

fastmrz/fastmrz.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ def _cleanse_roi(self, raw_text):
6161
selection_length = len(item)
6262
break
6363

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
6970

7071
def _get_final_check_digit(self, input_string, input_type):
7172
if input_type == 'TD3':
@@ -128,6 +129,16 @@ def get_mrz(self, image_path):
128129
else:
129130
return {'status': 'FAILURE', 'message': file_status}
130131

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+
131142
def _parse_mrz(self, mrz_text):
132143
mrz_lines = mrz_text.strip().split('\n')
133144
if len(mrz_lines) not in [2, 3]:
@@ -140,6 +151,8 @@ def _parse_mrz(self, mrz_text):
140151
# Line 1
141152
mrz_code_dict['document_type'] = mrz_lines[0][:2].strip('<')
142153
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'}
143156
names = mrz_lines[0][5:].split('<<')
144157
mrz_code_dict['surname'] = names[0].replace('<', ' ')
145158
mrz_code_dict['given_name'] = names[1].replace('<', ' ')
@@ -149,6 +162,8 @@ def _parse_mrz(self, mrz_text):
149162
if self._get_check_digit(mrz_code_dict['document_number']) != mrz_lines[1][9]:
150163
return {'status': 'FAILURE', 'message': 'document number checksum is not matching'}
151164
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'}
152167
mrz_code_dict['date_of_birth'] = mrz_lines[1][13:19]
153168
if self._get_check_digit(mrz_code_dict['date_of_birth']) != mrz_lines[1][19]:
154169
return {'status': 'FAILURE', 'message': 'date of birth checksum is not matching'}
@@ -158,6 +173,8 @@ def _parse_mrz(self, mrz_text):
158173
if self._get_check_digit(mrz_code_dict['date_of_expiry']) != mrz_lines[1][27]:
159174
return {'status': 'FAILURE', 'message': 'date of expiry checksum is not matching'}
160175
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'])
161178
if mrz_code_dict['mrz_type'] == 'TD3':
162179
mrz_code_dict['optional_data'] = mrz_lines[1][28:35].strip('<')
163180

@@ -173,6 +190,8 @@ def _parse_mrz(self, mrz_text):
173190
# Line 1
174191
mrz_code_dict['document_type'] = mrz_lines[0][:2].strip('<')
175192
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'}
176195
mrz_code_dict['document_number'] = mrz_lines[0][5:14]
177196
if self._get_check_digit(mrz_code_dict['document_number']) != mrz_lines[0][14]:
178197
return {'status': 'FAILURE', 'message': 'document number checksum is not matching'}
@@ -188,7 +207,11 @@ def _parse_mrz(self, mrz_text):
188207
if self._get_check_digit(mrz_code_dict['date_of_expiry']) != mrz_lines[1][14]:
189208
return {'status': 'FAILURE', 'message': 'date of expiry checksum is not matching'}
190209
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'])
191212
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'}
192215
mrz_code_dict['optional_data_2'] = mrz_lines[0][18:29].strip('<')
193216
if mrz_lines[1][-1] != self._get_final_check_digit(mrz_lines, mrz_code_dict['mrz_type']):
194217
return {'status': 'FAILURE', 'message': 'final checksum is not matching'}

0 commit comments

Comments
 (0)