def picResultVisualization(imageName, r):
img = cv2.imread(imageName)
out_img = os.path.join(outImagePath, os.path.split(imageName)[-1])
cv2.imwrite(out_img, img)
img_OpenCV = cv2.imread(out_img)
for i in range(len(r)):
x1=r[i][2][0]-r[i][2][2]/2
y1=r[i][2][1]-r[i][2][3]/2
x2=r[i][2][0]+r[i][2][2]/2
y2=r[i][2][1]+r[i][2][3]/2
label = r[i][0]
seore = r[i][1]
# 画框
if label == '印章':
cv2.rectangle(img_OpenCV,(int(x1),int(y1)),(int(x2),int(y2)),(0,255,0),1)
# 画一个矩形填充区域
cv2.rectangle(img_OpenCV, (int(x1), int(y1-0.20*r[i][2][3])-2), (int(x2), int(y1)), (20, 147, 255), -1)
elif label == '国徽':
# 画一个矩形填充区域
cv2.rectangle(img_OpenCV, (int(x1), int(y1-0.20*r[i][2][3])-2), (int(x2), int(y1)), (150, 0, 180), -1)
cv2.rectangle(img_OpenCV,(int(x1),int(y1)),(int(x2),int(y2)),(255,0,0),1)
# 图像从OpenCV格式转换成PIL格式
img_PIL = Image.fromarray(cv2.cvtColor(img_OpenCV, cv2.COLOR_BGR2RGB))
# 字体 ubuntu系统字体*.ttc的存放路径一般是: /usr/share/fonts/opentype/noto/ 查找指令locate *.ttc
font = ImageFont.truetype('/home/monk/snap/notepad-plus-plus/common/.wine/drive_c/windows/Fonts/NotoSerifCJK-ExtraLight.ttc', int(0.13*r[i][2][2]))
# 字体颜色
fillColor = (255,255,255)
# 文字输出位置
position = (x1+3, y1-0.20*r[i][2][3])
# 输出内容
info = label + '_'+ str(round(seore,6))
# 需要先把输出的中文字符转换成Unicode编码形式
if not isinstance(info, unicode):
info = info.decode('utf8')
draw = ImageDraw.Draw(img_PIL)
draw.text(position, info, font=font, fill=fillColor)
# 转换回OpenCV格式
img_OpenCV = cv2.cvtColor(np.asarray(img_PIL),cv2.COLOR_RGB2BGR)
# 输出图片
cv2.imwrite(out_img, img_OpenCV)