diff --git a/OCRPython/extractor/identitycard_extractor.py b/OCRPython/extractor/identitycard_extractor.py index 0c72452..352e289 100644 --- a/OCRPython/extractor/identitycard_extractor.py +++ b/OCRPython/extractor/identitycard_extractor.py @@ -32,40 +32,116 @@ class IdentityCardExtractor(Extractor): logger.error(e) return {} + # def extract_textbyPaddle(self, text:str)->dict: + # try: + # patterns = { + # "issuingAuthority": r"签发机关\n*(.+?)\n", + # "validTime": r"有效期限\n*(.+?)\n", + # "name": r"姓名(.*?)\n", ##### + # "gender": r"(\S)民族", + # "ethnicity": r"民族(\S+)", + # "dateOfBirth": r"(\d+年\d+月\d+日)", + # "address": r"(住址|佳址)\s*(.*?)公民身份号码", + # "idNumber": r"(\d{18}|\d{17}[Xx])" + # } + # + # tempText = self.remove_blank_lines(text) + # # 提取信息 + # info = {} + # for key, pattern in patterns.items(): + # match = re.search(pattern, tempText,re.DOTALL) + # if match: + # if "address" == key: + # tempStr = match.group(2).strip() + # else: + # tempStr = match.group(1).strip() + # info[key] = tempStr.replace("\n", "") + # return info + # except Exception as e: + # print(e) + # logger.error(e) + # return {} + def extract_textbyPaddle(self, text:str)->dict: try: - patterns = { - "issuingAuthority": r"签发机关\n*(.+?)\n", - "validTime": r"有效期限\n*(.+?)\n", - "name": r"姓名(.*?)\n", ##### - "gender": r"(\S)民族", - "ethnicity": r"民族(\S+)", - "dateOfBirth": r"(\d+年\d+月\d+日)", - "address": r"(住址|佳址)\s*(.*?)公民身份号码", - "idNumber": r"(\d{18}|\d{17}[Xx])" - } + result = {} + # 提取签发机关 + issuing_authority = re.search(r"签发机关\n*(.+?)\n", text, re.DOTALL) + if issuing_authority: + result["issuingAuthority"] = issuing_authority.group(1).strip() - tempText = self.remove_blank_lines(text) - # 提取信息 - info = {} - for key, pattern in patterns.items(): - match = re.search(pattern, tempText,re.DOTALL) - if match: - if "address" == key: - tempStr = match.group(2).strip() + # 提取有效期限 + valid_time = re.search(r"有效期限\n*(\d{4}\.\d{2}\.\d{2}-\S+)", text, re.DOTALL) + if valid_time: + result["validTime"] = valid_time.group(1).strip() + + # 提取姓名 + name = re.search(r"姓名\s*(.*?)\n", text,re.DOTALL) + if name: + tempName = name.group(1).strip() + if tempName in "性别男" or tempName in "性别女": + name = re.search(r"(.*?)\s*姓名", text, re.DOTALL) + result["name"] = name.group(1).strip() + else: + result["name"] = name.group(1).strip() + + else: + name = re.search(r"米名(\S*)址\s*(\S+)", text, re.DOTALL) + if name: + result["name"] = name.group(2).strip() + else: + name = re.search(r"名\s*(\S+)\s*姓\s*", text, re.DOTALL) + if name: + result["name"] = name.group(1).strip() else: - tempStr = match.group(1).strip() - info[key] = tempStr.replace("\n", "") - return info + name = re.search(r"(\S+)\s*(男|女|性别)", text, re.DOTALL) + if name: + result["name"] = name.group(1).strip() + + # 提取民族 + ethnicity = re.search(r"民\s*族\s*(\S+)", text, re.DOTALL) + if ethnicity: + result["ethnicity"] = ethnicity.group(1).strip() + + # 提取地址 + address = re.search(r"(住址|佳址)(.*?)公民身份号码", text, re.DOTALL) + if address: + result["address"] = address.group(2).strip().replace("\n", "") + else: + address = re.search(r"(\S+省)(.*?)公民身份号码", text, re.DOTALL) + if address: + result["address"] = address.group(1).strip().replace("\n", "") + address.group(2).strip().replace("\n", "") + else: + address = re.search(r"(\S+市)(.*?)公民身份号码", text, re.DOTALL) + if address: + result["address"] = address.group(1).strip().replace("\n", "") + address.group(2).strip().replace("\n", "") + else: + address = re.search(r"(\S+县)(.*?)公民身份号码", text, re.DOTALL) + if address: + 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"]) + + # 提取身份证号码 + id_number = re.search(r"(\d{18}|\d{17}[Xx])", text, re.DOTALL) + if id_number: + result["idNumber"] = id_number.group(1).strip() + + if result["idNumber"]: + # 提取出生日期 + result["dateOfBirth"] = self.extract_birthday_from_id(result["idNumber"]) + # 提取性别 + result["gender"] = self.get_gender_from_id(result["idNumber"]) + + return result except Exception as e: print(e) logger.error(e) return {} - -class InvoiceExtractor(Extractor): - def extract_text(self,text:str)->dict: - pass + class InvoiceExtractor(Extractor): + def extract_text(self,text:str)->dict: + pass # text = """中华人民共和国 @@ -96,7 +172,145 @@ class InvoiceExtractor(Extractor): # 公民身份号码 # 34220119941017327X # """ -# extractor = IdentityCardExtractor() + +# text = """中华人民共和国 +# 居民身份证 +# 签发机关 +# 盐源县公安局 +# 出 +# 有效期限 +# 2013.06.18-2033.06.18 +# 坐姓5性中出住 +# 米名年别到生委址 +# 江六斤 +# 男民族彝 +# 半口 +# 1980 +# 年4月20日 +# 四川省盐源县盖租乡阿石 +# 村6组4号 +# 公民身份号码 +# 513423198004203995 +# """ # +# text = """姓名韩邀宇 +# 性别男 +# 民族汉 +# 出生 +# 1999年12月15日 +# 住址 +# 安徽省太和县税镇镇十里 +# 沟村委会中韩村16号 +# 公民身份号码 +# 34122219991215183X""" +# +# +# text = """中华人民共和国 +# 居民身份证 +# 签发机关 +# 木里县公安局 +# 有效期限 +# 2016.12.05-2026.12.05 +# 姓性出住 +# 马加加 +# 女 +# 民族彝 +# 1998年1月2日 +# 址 +# 四川省木里藏族自治县博 +# 科乡八科村麻窝地组18号 +# 公民身份号码 +# 513422199801023821""" + +# text = """ +# 毛阿卡 +# 姓名 +# 性别男 +# 民族彝 +# 3 +# 出生 +# 1976年3月3日 +# 住址 +# 四川省木里藏族自治县耗 +# 牛坪乡泥珠村下泥珠组55 +# 号 +# 中国LHINA +# 公民身份号码 +# 513423197603033997 +# 中华人民共和国 +# 居民身份证 +# 签发机关 +# 木里县公安局 +# 有效期限 +# 2017.06.06-2037.06.06 +# """ +# text = """中华人民共和国 +# 居民身份证 +# 国 +# 签发机关 +# 西昌市公安局 +# 有效期限 +# 2008.09.01-2028.09.01 +# 逆姓5性中出日住 +# 米名丰别刺生更址 +# 祝九根惹 +# 心 +# 民 +# 族彝 +# 男 +# E +# 1978 +# 四川省西昌市磨盘乡大厂 +# 村4组27号 +# 公民身份号码 +# 513401197807087411 +# """ +# text = """中华人民共和国 +# 居民身份证 +# 签发机关 +# 木里县公安局 +# 有效期限 +# 2020.03.16-2025.03.16 +# 名 +# 蒋子古 +# 姓 +# 男 +# 民族彝 +# 出生 +# 2005年1月4日 +# 住址 +# 四川省木里藏族自治县耗 +# 牛坪乡泥珠村5组29号 +# 公民身份号码 +# 513422200501044415 +# """ +# extractor = IdentityCardExtractor() # jsonstring = extractor.extract_textbyPaddle(text) -# print(jsonstring) \ No newline at end of file +# print(jsonstring) + + + + +# text = """ +# 姓名张三 +# 性别男 +# 民族汉 +# # """ +# text = """ +# 江六斤 +# 女民族彝 +# """ +# # 尝试使用 r"性别(\S)" 匹配性别 +# gender_match = re.search(r'性别(\S)|(\S)民族', text) +# if gender_match: +# gender = gender_match.group(0) +# print("性别是:", gender) +# # else: +# # # 如果匹配不到,尝试使用 r"(\S)民族" 匹配 +# # gender_match = re.search(r'(\S)民族', text) +# # if gender_match: +# # gender = gender_match.group(1) +# # print("性别是:", gender) +# else: +# print("未找到性别信息") +