我的目标是检测图像中的表格并将这些表格转换为 CSV 文件。
我正在使用 CascadeTabNet 来检测表的位置。 他们的示例效果很好(https://colab.research.google.com/drive/1lzjbBQsF4X2C2WZhxBJz0wFEQor7F-fv?usp=sharing),问题是它最后没有显示如何将检测到的表格保存为另一个图像(也不知道如何将该表实际制作成 CSV 文件,但为此,我可以使用其他代码)。
这就是他们所拥有的:
from mmdet.apis import init_detector, inference_detector, show_result_pyplot
import mmcv
# Load model
config_file = '/content/CascadeTabNet/Config/cascade_mask_rcnn_hrnetv2p_w32_20e.py'
checkpoint_file = '/content/epoch_36.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# Test a single image
img = "/content/CascadeTabNet/Demo/demo.png"
# Run Inference
result = inference_detector(model, img)
# Visualization results
show_result_pyplot(img, result,('Bordered', 'cell', 'Borderless'), score_thr=0.85)
我尝试将以下内容添加到他们的代码中:
model.show_result(img, result, out_file='result.jpg')
我收到以下错误:
TypeError:show_result() 获得意外的关键字参数“out_file”
我还尝试在“show_result_pyplot”中添加“out_file”:
show_result_pyplot(img, result,('Bordered', 'cell', 'Borderless'), score_thr=0.85, out_file='result.jpg')
我得到了同样的错误:show_result()得到了一个意外的关键字参数'out_file'
最后,我尝试使用 matplotlib 中的 pyplot 保存图像,但它没有保存裁剪区域(页面的表格区域),而是保存所有区域:
plt.savefig('foo.png')
我一直在寻找解决方案(如何使用Python在OpenCV中裁剪图像,如何检测图像上的符号并保存它?,如何使用openCV保存裁剪后的图像?,如何保存检测到的对象放入新图像中 ...),它们都包含 cv2,但这对我来说也不起作用。
简而言之,使用“inference_detector()”后如何裁剪表格区域并保存为新图像?
使用此代码,在图像上绘制结果:
#Save Image with result
model.show_result(img, result, show=False,out_file='temp_img.jpg')