How to crop and save image obtained by inference_detector() using CascadeTabNet (openCV)?

424 Views Asked by At

My goal is to detect tables in images and transform such tables into CSV files.

I am using CascadeTabNet to detect the location of tables. Their example works great (https://colab.research.google.com/drive/1lzjbBQsF4X2C2WZhxBJz0wFEQor7F-fv?usp=sharing), the problem is that it does not show at the end how to save the detected table as another image (nor how to actually make that table into a CSV file, but for that, I can use other code).

This is what they had:

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)

I tried to add the following to their code:

model.show_result(img, result, out_file='result.jpg')

And I get the following error:

TypeError: show_result() got an unexpected keyword argument 'out_file'

I also tried adding the "out_file" inside the "show_result_pyplot":

show_result_pyplot(img, result,('Bordered', 'cell', 'Borderless'), score_thr=0.85, out_file='result.jpg')

And I get the same error: show_result() got an unexpected keyword argument 'out_file'

Finally, I tried saving the image using pyplot from matplotlib, but it did not save the cropped area (the table area of the page) but all of it:

plt.savefig('foo.png')

I have been looking for solutions (How to crop an image in OpenCV using Python, How to detect symbols on a image and save it?, How to save cropped image using openCV?, How to save the detected object into a new image ...), and they all include cv2, but that did not work for me either.

In short, how to crop the area of the table and save it as a new image after using "inference_detector()"?

1

There are 1 best solutions below

1
Santiago Ballesteros On

Use this code, for plotting the result on the image:

#Save Image with result
model.show_result(img, result, show=False,out_file='temp_img.jpg')