mmocr/demo/batch_image_demo.py
Sergio Bugallo Enjamio 18c54afbdc
Add support for batch inference (#86)
* Add support for numpy arrays in model_inference

* Add test for numpy ndarray inference

* Fix linting problems

* Add support for batch inference

* Add batch inference demo script

* Fix comment

* Test batch inference with paths and arrays

* lint code

* Update model_inference docstring

* Refactor model inference tests

* Change inference function to make text detectors and recognizers use the same input data types

* Change single state text detector model to support batch inference

* Lint code

* simplify inference tests

* Remove psenet from batch inference test cases to prevent the pytest being killed

* Update batch_image_demo.py

* fix bug when test with dataset 

fix bug when test with dataset, for example, `./tools/dist_test.sh configs/textrecog/sar/sar_r31_parallel_decoder_academic.py <checkpoint> 1 --eval acc`

Co-authored-by: Hongbin Sun <hongbin306@gmail.com>
2021-05-12 02:04:01 +00:00

56 lines
1.8 KiB
Python

from argparse import ArgumentParser
from pathlib import Path
import mmcv
from mmdet.apis import init_detector
from mmocr.apis.inference import model_inference
from mmocr.datasets import build_dataset # noqa: F401
from mmocr.models import build_detector # noqa: F401
def main():
parser = ArgumentParser()
parser.add_argument('config', help='Config file.')
parser.add_argument('checkpoint', help='Checkpoint file.')
parser.add_argument('save_path', help='Folder to save visualized images.')
parser.add_argument(
'--images',
nargs='+',
help='Image files to be predicted with batch mode, '
'separated by space, like "image_1.jpg image2.jpg". '
'If algorithm use augmentation test, only one '
'image file can be given.')
parser.add_argument(
'--device', default='cuda:0', help='Device used for inference.')
parser.add_argument(
'--imshow',
action='store_true',
help='Whether show image with OpenCV.')
args = parser.parse_args()
# build the model from a config file and a checkpoint file
model = init_detector(args.config, args.checkpoint, device=args.device)
if model.cfg.data.test['type'] == 'ConcatDataset':
model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][
0].pipeline
# test multiple images
results = model_inference(model, args.images)
print(f'results: {results}')
save_path = Path(args.save_path)
for img_path, result in zip(args.images, results):
out_file = save_path / f'result_{Path(img_path).stem}.png'
# show the results
img = model.show_result(
img_path, result, out_file=str(out_file), show=False)
if args.imshow:
mmcv.imshow(img, f'predicted results ({img_path})')
if __name__ == '__main__':
main()