安装好torch环境以后,直接装官方的库
bash
pip install -U openmim
mim install mmengine
mim install mmcv
mim install mmdet
mim install mmocr
检测代码
py
from mmocr.apis import MMOCRInferencer
import cv2
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
infer = MMOCRInferencer(det='textsnake', rec='svtr-small')
img_path = 'test_ocr.jpg'
img_bgr = cv2.imread(img_path)
# 进行推理
result = infer(img_path, save_vis=True, return_vis=True)
# 查看检测到的结果
res = result['predictions'][0]['rec_texts']
print(res)
plt.imshow(result['visualization'][0])
# 保存图像到文件
plt.savefig('result_image.png', bbox_inches='tight', pad_inches=0,dpi=500)
plt.show()