银行卡内容识别 opencv显示中文乱码解决

import cv2
from cnstd import CnStd
from cnocr import CnOcr
import numpy as np
import re
from PIL import Image, ImageDraw, ImageFont
def check_string(content):
    # Check if the content has Chinese characters, alphabets and numbers.
    if re.search('[\u4e00-\u9fa5]', content) and re.search('[a-zA-Z]', content) and re.search('[0-9]', content):
        return True
    return False
def put_chinese_text(img, text, left_top):
    # 转换 cv2 img 为 PIL Image
    img_PIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    draw = ImageDraw.Draw(img_PIL)
    font = ImageFont.truetype('simhei.ttf', 180, encoding="utf-8")

    # 黄色文字
    fillColor = (255,255,0)

    position = left_top
    draw.text(position, text, font=font, fill=fillColor)

    # 转换回 OpenCV 格式
    img_out = cv2.cvtColor(np.asarray(img_PIL),cv2.COLOR_RGB2BGR)

    return img_out
# Define the filename.
filename = r"D:\sun\output\1.txt"

# Open the file.
file = open(filename, "w")


std = CnStd()
cn_ocr = CnOcr()
img = cv2.imread(r"D:\sun\img\1.jpg")
box_infos = std.detect(np.array(img))
oriimg = img.copy()
for text_info in box_infos['detected_texts']:
    # Extract bbox.
    bbox = text_info['box']
    bbox = bbox.astype(int)

    # Cropped image for OCR.
    cropped_img = text_info['cropped_img']

    # OCR result.
    ocr_res = cn_ocr.ocr_for_single_line(cropped_img)
    txt = ocr_res['text']

    print(txt)
    if check_string(txt):
        # If text contains Chinese characters, number, and alphabets
        # Draw rectangle on the img.
        cv2.rectangle(img, (bbox[0][0], bbox[0][1]), (bbox[2][0], bbox[2][1]), (0, 255, 255), 20)
        file.write(txt + "\n")
        text_position = (bbox[2][0] -500, bbox[2][1])
        img = put_chinese_text(img, txt, text_position)
        # Put text annotation next to bounding box.
        #cv2.putText(img, txt, (bbox[2][0], bbox[2][1]-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
display_width = 600
ratio = img.shape[1] / display_width

# Resize the image for display.
display_img = cv2.resize(img, (display_width, int(img.shape[0]/ratio)))
# Display the image.
cv2.imshow('image with boxes', display_img)
#cv2.imwrite(r"D:\sun\output\car2.jpg", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值