直接上效果图:
原来效果:
model.py中核心代码:
def cv2ImgAddText(img,result, text, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
for one in result:
draw.text((int(one['cx']), int(one['cy'])), one['text'], textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
def text_predict():
# img = cv2.imread(imgpath)
img = cv2.imread('./test_imgs/train-demo.jpg')
preds, boxes_list, rects_re, t = text_handle.predict(img, long_size=pse_long_size)
img2 = draw_bbox(img, boxes_list, color=(0, 255, 0))
cv2.imwrite("./draw.png", img2)
#cv2.show(img2)
result = crnnRec(np.array(img), rects_re)
# 调用cv.putText()添加文字
AddText = img.copy()
AddText.fill(255)
AddText=cv2ImgAddText(AddText,result,(0, 0 , 139), 40)
# 将原图片和添加文字后的图片拼接起来
res = np.hstack([img, AddText])
cv2.imwrite("./result.png", res)
# 显示拼接后的图片
#cv2.imshow('text', res)
#cv2.waitKey()
#cv2.destroyAllWindows()
return result