Zhang Li il y a 2 ans
Parent
commit
d98a3fb4ad
12 fichiers modifiés avec 45 ajouts et 59 suppressions
  1. 11 0
      Makefile
  2. 13 8
      core/idcrad.py
  3. BIN
      idcard.zip
  4. 0 0
      images/back.jpeg
  5. 0 0
      images/front-180.png
  6. 0 0
      images/front-270-1.png
  7. 0 0
      images/front-270.png
  8. 0 0
      images/front-90-1.png
  9. 0 0
      images/front-90.png
  10. 0 0
      images/test.jpeg
  11. 8 28
      main.py
  12. 13 23
      main2.py

+ 11 - 0
Makefile

@@ -0,0 +1,11 @@
+NAME=idcard
+VERSION=latest
+BUILD_TIME      := $(shell date "+%F %T")
+COMMIT_SHA1     := $(shell git rev-parse HEAD)
+AUTHOR          := $(shell git show -s --format='%an')
+
+
+.PHONY: local
+local:
+	@docker build -t registry.cn-hangzhou.aliyuncs.com/sxtest/$(NAME):$(VERSION) .
+	@docker push registry.cn-hangzhou.aliyuncs.com/sxtest/$(NAME):$(VERSION)

+ 13 - 8
core/idcrad.py

@@ -56,8 +56,7 @@ class FrontParser(Parser):
                 # txt = txt.replace("出生", "")
                 txt = txt.split('生')[-1]
                 addString.append(txt.strip())
-                self.res["Birth"].text = "".join(addString)
-                self.res["Birth"].confidence = self.confs[i]
+                self.res["Birth"] = RecItem("".join(addString),  self.confs[i])
                 break
 
     def card_no(self):
@@ -103,13 +102,11 @@ class FrontParser(Parser):
         for i in range(len(self.result)):
             txt = self.result[i]
             if "男" in txt:
-                self.res["Gender"].text = "男"
-                self.res["Gender"].confidence = self.confs[i]
+                self.res["Gender"] = RecItem("男", self.confs[i])
                 break
 
             if "女" in txt:
-                self.res["Gender"].text = "女"
-                self.res["Gender"].confidence = self.confs[i]
+                self.res["Gender"] = RecItem("女", self.confs[i])
                 break
 
     def national(self):
@@ -119,8 +116,7 @@ class FrontParser(Parser):
             res = re.findall(".*民族[\u4e00-\u9fa5]+", txt)
 
             if len(res) > 0:
-                self.res["Nationality"].text = res[0].split("族")[-1]
-                self.res["Nationality"].confidence = self.confs[i]
+                self.res["Nationality"] = RecItem(res[0].split("族")[-1], self.confs[i])
                 break
 
     def address(self):
@@ -181,6 +177,14 @@ class FrontParser(Parser):
             self.res["address_region"] = RecItem(region, conf)
             self.res["address_detail"] = RecItem(detail, conf)
 
+    def expire_date(self):
+        for txt, conf in zip(self.result, self.confs):
+            print(txt)
+            res = re.findall('\d{4}\.\d{2}\.\d{2}\-\d{4}\.\d{2}\.\d{2}', txt)
+            print(res)
+            if res:
+                self.res["expire_date"] = RecItem(res[0], conf)
+
 
     def predict_name(self):
         """
@@ -218,6 +222,7 @@ class FrontParser(Parser):
         # self.predict_name()
         self.birth()
         self.gender()
+        self.expire_date()
         return self.res
 
 

BIN
idcard.zip


+ 0 - 0
back.jpeg → images/back.jpeg


+ 0 - 0
front-180.png → images/front-180.png


+ 0 - 0
front-270-1.png → images/front-270-1.png


+ 0 - 0
front-270.png → images/front-270.png


+ 0 - 0
front-90-1.png → images/front-90-1.png


+ 0 - 0
front-90.png → images/front-90.png


+ 0 - 0
test.jpeg → images/test.jpeg


+ 8 - 28
main.py

@@ -2,6 +2,7 @@ import cv2
 from paddleocr import PaddleOCR
 from core.idcrad import FrontParser
 from core.direction import *
+from core.ocr import IdCardOcr
 
 # 初始化ocr模型和后处理模型
 ocr = PaddleOCR(use_angle_cls=True, rec_model_dir="./idcard_rec_infer/",
@@ -11,34 +12,13 @@ ocr = PaddleOCR(use_angle_cls=True, rec_model_dir="./idcard_rec_infer/",
                 rec_char_dict_path="./ppocr_keys_v1.txt", lang="ch", use_gpu=False)
 
 # 定义文件路径
-img_path = "front-270.png"
+img_path = "images/front-270.png"
 image = cv2.imread(img_path)
-# 逆时针
-angle = detect_angle(image)
-print(angle)
+image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
-if angle == 180:
-    image = cv2.rotate(image, cv2.ROTATE_180)
-if angle == 90:
-    image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
-    cv2.imwrite('front-90-1.png', image)
-if angle == 270:
-    image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
-    cv2.imwrite('front-270-1.png', image)
+# 正面
+image_type = '0'
 
-
-# 获取模型检测结果
-result = ocr.ocr(image, cls=True)
-print("------------------")
-print(result)
-# 将检测到的文字放到一个列表中
-txts = [line[1][0] for line in result]
-confs = [line[1][1] for line in result]
-print("......................................")
-print(txts)
-print(confs)
-print("......................................")
-# 将结果送入到后处理模型中
-postprocessing = FrontParser(txts, confs)
-parse_result = postprocessing.parse()
-print(parse_result)
+m = IdCardOcr(ocr, image, image_type)
+res = m .predict()
+print(res)

+ 13 - 23
main2.py

@@ -1,35 +1,25 @@
-import os
 from paddleocr import PaddleOCR
-from core.idcrad import BackParser
+from core.ocr import IdCardOcr
 from core.direction import *
-import json
 
 # 初始化ocr模型和后处理模型
-ocr = PaddleOCR(use_angle_cls=True, rec_model_dir="./idcard_rec_infer/",
-                det_model_dir="./idcard_det_infer/", cls_model_dir="idcard_cls_infer",
+ocr = PaddleOCR(use_angle_cls=True,
+                rec_model_dir="./idcard_rec_infer/",
+                det_model_dir="./idcard_det_infer/",
+                cls_model_dir="idcard_cls_infer",
                 rec_algorithm='CRNN',
                 ocr_version='PP-OCRv2',
-                rec_char_dict_path="./ppocr_keys_v1.txt", lang="ch", use_gpu=False)
+                rec_char_dict_path="./ppocr_keys_v1.txt",
+                lang="ch", use_gpu=False)
 
 # 定义文件路径
-img_path = "back.jpeg"
+img_path = "images/back.jpeg"
 image = cv2.imread(img_path)
-angle = detect_angle(image)
-print("------------------")
-print(angle)
-print("------------------")
+image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
-# 获取模型检测结果
-result = ocr.ocr(img_path, cls=True)
-confs = [line[1][1] for line in result]
-
-# 将检测到的文字放到一个列表中
-txts = [line[1][0] for line in result]
-print("......................................")
-print(txts)
-print("......................................")
-# 将结果送入到后处理模型中
-parser = BackParser(txts, confs)
-res = parser.parse()
+# 反面
+image_type = '1'
 
+m = IdCardOcr(ocr, image, image_type)
+res = m .predict()
 print(res)