识别结果可视化

This commit is contained in:
2024-06-24 09:13:24 +08:00
parent 2a0e9a2367
commit aa83b88536
3 changed files with 55 additions and 0 deletions

View File

View File

@@ -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)

Binary file not shown.