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()
05-21
1560
06-28
2549
10-27
2356