diff --git a/OCRPython/extractor/identitycard_extractor.py b/OCRPython/extractor/identitycard_extractor.py index 3bba09b..1f380a1 100644 --- a/OCRPython/extractor/identitycard_extractor.py +++ b/OCRPython/extractor/identitycard_extractor.py @@ -69,14 +69,20 @@ class IdentityCardExtractor(Extractor): "validTime": "", "name": "", "gender": "", - "ethnicity": "", + "ethnicity": "汉", "dateOfBirth": "", "address": "", "idNumber": ""} # 提取签发机关 issuing_authority = re.search(r"(签发机关|签发机美)\n*(.+?)\n", text, re.DOTALL) if issuing_authority: - result["issuingAuthority"] = issuing_authority.group(2).strip() + tempAuthority = issuing_authority.group(2).strip() + if "公安局" not in tempAuthority: + issuing_authority = re.search(r"(\S+)\n*(签发机关|签发机美)\n", text, re.DOTALL) + if issuing_authority: + result["issuingAuthority"] = issuing_authority.group(1).strip() + else: + result["issuingAuthority"] = issuing_authority.group(2).strip() # 提取有效期限 valid_time = re.search(r"有效期限\n*(\d{4}\.\d{2}\.\d{2}-\S+)", text, re.DOTALL) @@ -87,7 +93,7 @@ class IdentityCardExtractor(Extractor): name = re.search(r"姓名\s*(.*?)\n", text,re.DOTALL) if name: tempName = name.group(1).strip() - if tempName in "性别男" or tempName in "性别女" or "性别男" in tempName or "性别女" in tempName: + if tempName in "性别男" or tempName in "性别女" or "性别男" in tempName or "性别女" in tempName or tempName.isupper() or tempName.islower(): name = re.search(r"(\S+)\s*姓名", text, re.DOTALL) result["name"] = name.group(1).strip() else: @@ -105,6 +111,9 @@ class IdentityCardExtractor(Extractor): name = re.search(r"(\S+)\s*(男|女|性别)", text, re.DOTALL) if name: result["name"] = name.group(1).strip() + tempName = result["name"] + if tempName.startswith("名"): + result["name"] = text[len("名"):] # 提取民族 ethnicity = re.search(r"民\s*(族|旅)\s*(\S+)", text, re.DOTALL) @@ -129,9 +138,21 @@ class IdentityCardExtractor(Extractor): result["address"] = address.group(1).strip().replace("\n", "") + address.group(2).strip().replace("\n", "") if result["address"]: result["address"] = re.sub(r'[A-Z]', '', result["address"]) - + pattern = r"\d+[号组室房]" + # 匹配数字+号、数字+组、数字+室、数字+房 的最后一个匹配项 + tempAddress = result["address"] + matchAddress = None + for m in re.finditer(pattern, tempAddress): + matchAddress = m + if matchAddress: + index = matchAddress.end() # 获取匹配项的结束位置 + result["address"] = tempAddress[:index] # 截取匹配项及之前的字符串 + #去掉地址后面的 + else: + pattern = r"\d{18,}$" # 匹配1位以上的数字结尾 + result["address"] = re.sub(pattern, "", result["address"]) # 提取身份证号码 - id_number = re.search(r"(\d{18}|\d{17}[Xx])", text, re.DOTALL) + id_number = re.search(r"([123456]\d{17}|[123456]\d{16}[Xx])", text, re.DOTALL) if id_number: result["idNumber"] = id_number.group(1).strip() @@ -199,7 +220,7 @@ class IdentityCardExtractor(Extractor): # 村6组4号 # 公民身份号码 # 513423198004203995 -# """ +# # """ # # text = """姓名韩邀宇 # 性别男 @@ -211,7 +232,7 @@ class IdentityCardExtractor(Extractor): # 沟村委会中韩村16号 # 公民身份号码 # 34122219991215183X""" -# + # # text = """中华人民共和国 # 居民身份证 @@ -311,13 +332,63 @@ class IdentityCardExtractor(Extractor): # 坪镇全家坪村3组 # 公民身份号码 # 433030197508150820""" +# text = """姓5性乎出日住 +# 米名丰别生更址 +# 张亮 +# 男 +# 民族彝 +# 1975 +# 年11月27日 +# 四川省西昌市樟木箐乡中 +# 安村1组29号附1号 +# 公民身份号码 +# 513401197511274218 +# 中华人民共和国 +# 居民身份证 +# 国水 +# 西昌市公安局 +# 签发机关 +# 有效期限 +# 2017.08.21-2037.08.21""" +# text = """中华人民共和国 +# 居民身份证 +# 签发机关 +# 宁乡县公安局 +# 有效期限 +# 2008.10.28-2028.10.28 +# 姓名刘放华 +# 性别男民族汉 +# 出生1970年5月13日 +# 住址 +# 湖南省宁乡县流沙河镇荷 +# 林村10组 +# 8430124197005132917 +# 公民身份号码""" +# +# text = """中华人民共和国 +# 居民身份证 +# 签发机关醴陵市公安局 +# 有效期限2007.07.02-2027.07.02 +# 和 +# 姓名朱建明 +# 致签 +# 性别男民族汉 +# 出生1970年11月9日 +# 劳 +# 住址 +# 湖南省醴陵市西山办事处 +# 动 +# 滴水井村樟树组7号 +# 23 +# 公民身份号码 +# 430281197011090090""" + # extractor = IdentityCardExtractor() # jsonstring = extractor.extract_textbyPaddle(text) # print(jsonstring) - # text = """ # 姓名张三 # 性别男