diff --git a/check_ie_result/__init__.py b/check_ie_result/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/check_ie_result/check_ie_result.py b/check_ie_result/check_ie_result.py new file mode 100644 index 0000000..6c6d580 --- /dev/null +++ b/check_ie_result/check_ie_result.py @@ -0,0 +1,55 @@ +import json +import os +import sys +from io import BytesIO +from itertools import groupby + +import requests +from PIL import ImageDraw, Image, ImageFont + +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from config.mysql import MysqlSession +from photo_review.entity.zx_ocr import ZxOcr +from photo_review.entity.zx_phrec import ZxPhrec +from ucloud import ucloud + + +def check_ie_result(pk_phhd): + session = MysqlSession() + phrecs = session.query(ZxPhrec.pk_phrec, ZxPhrec.pk_phhd, ZxPhrec.cRectype, ZxPhrec.cfjaddress).filter(ZxPhrec.pk_phhd == pk_phhd).all() + for phrec in phrecs: + img_name = phrec.cfjaddress + img_path = ucloud.get_private_url(img_name) + + response = requests.get(img_path) + image = Image.open(BytesIO(response.content)).convert("RGB") + size = image.width * image.height / 200000 + font = ImageFont.truetype("./font/simfang.ttf", size=size) + + ocr = session.query(ZxOcr.id, ZxOcr.content, ZxOcr.x_offset, ZxOcr.y_offset).filter(ZxOcr.pk_phrec == phrec.pk_phrec).all() + for id, group_results in groupby(ocr, key=lambda x: x.id): + draw = ImageDraw.Draw(image) + for ocr_item in group_results: + result = json.loads(ocr_item.content) + x_offset = ocr_item.x_offset + y_offset = ocr_item.y_offset + for key in result: + for value in result[key]: + box = value["bbox"][0] + if x_offset: + box[0] += x_offset + box[2] += x_offset + if y_offset: + box[1] += y_offset + box[3] += y_offset + + draw.rectangle(box, outline="red", width=2) # 绘制矩形 + draw.text((box[0], box[1] - size), key, fill="blue", font=font) # 在矩形上方绘制文本 + draw.text((box[0], box[3]), value["text"], fill="blue", font=font) # 在矩形下方绘制文本 + os.makedirs(f"./check_result/{pk_phhd}/{ocr_item.id}", exist_ok=True) + image.save(f"./check_result/{pk_phhd}/{ocr_item.id}/{img_name}") + session.close() + + +if __name__ == '__main__': + check_ie_result(3866770) diff --git a/check_ie_result/font/simfang.ttf b/check_ie_result/font/simfang.ttf new file mode 100644 index 0000000..2b59eae Binary files /dev/null and b/check_ie_result/font/simfang.ttf differ