原因:
cv2.putText在给图片中写中文时会出现乱码“???”
cv2.putText(image, “中文”, (x_min, y_min), cv2.FONT_HERSHEY_COMPLEX, 0.6, (255, 255, 255), 1)
这里改用调用其他包来实现
import cv2
import numpy as np
from xml.etree import ElementTree
from PIL import Image, ImageDraw, ImageFont
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
# 判断是否为opencv图片类型
if (isinstance(img, np.ndarray)):
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype('simsun.ttc', textSize, encoding="utf-8")
draw.text((left, top), text, textColor, font=fontText)
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
image = cv2.imread(img_all_path)
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 0, 0), 2)
image = cv2ImgAddText(image, “中文”, x_max, y_min, (0, 0, 0), 40)