Repository: open-mmlab/mmocr

Issue:
IndexError when running model_inference with empty list
Thanks for your error report and we appreciate it a lot.

**Checklist**

1. I have searched related issues but cannot get the expected help.
2. The bug has not been fixed in the latest version.

**Bug description**
I am using the mmocr/utils/ocr.py script. I create a MMOCR object with both detector and recognition. However, when running the readtext method ,there are some images where I get the following error:
```python
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    result = ocr.readtext(data, print_result=True, imshow=False, batch_mode=True, merge=True)
  File "/home/mpena/mmocr/mmocr/utils/ocr.py", line 426, in readtext
    self.detect_model, self.recog_model, kie_model=self.kie_model)
  File "/home/mpena/mmocr/mmocr/utils/ocr.py", line 574, in det_recog_kie_inference
    recog_model, box_imgs, True, self.args.recog_batch_size)
  File "/home/mpena/mmocr/mmocr/utils/ocr.py", line 628, in single_inference
    result = model_inference(model, arrays, batch_mode=True)
  File "/home/mpena/mmocr/mmocr/apis/inference.py", line 101, in model_inference
    if not isinstance(imgs[0], (np.ndarray, str)):
IndexError: list index out of range
```
This happens because there are some images where the detector returns an empty list:  'boundary_result' from det_result at https://github.com/open-mmlab/mmocr/blob/main/mmocr/utils/ocr.py#L522

And this breaks at https://github.com/open-mmlab/mmocr/blob/main/mmocr/apis/inference.py#L101
```python
if isinstance(imgs, (list, tuple)):
        is_batch = True
        if not isinstance(imgs[0], (np.ndarray, str)):
            raise AssertionError('imgs must be strings or numpy arrays')

```
because imgs[0] doesn't exist.

**Reproduction**
The error can be reproduced with the following script, called from the mmocr directory

```python
from mmocr.utils.ocr import MMOCR
ocr = MMOCR() 
det_result = []
results = ocr.readtext(det_result, batch_mode=True, merge=True, print_result=True, imshow=False)
```

**Environment**

1. Please run `python mmocr/utils/collect_env.py` to collect necessary environment information and paste it here.
```bash
sys.platform: linux
Python: 3.7.11 (default, Jul 27 2021, 14:32:16) [GCC 7.5.0]
CUDA available: True
GPU 0: GeForce GTX 1080 Ti
CUDA_HOME: /usr
NVCC: Cuda compilation tools, release 10.1, V10.1.243
GCC: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
PyTorch: 1.6.0
PyTorch compiling details: PyTorch built with:
  - GCC 7.3
  - C++ Version: 201402
  - Intel(R) oneAPI Math Kernel Library Version 2021.4-Product Build 20210904 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v1.5.0 (Git Hash e2ac1fac44c5078ca927cb9b90e1b3066a0b2ed0)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - CUDA Runtime 10.1
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_37,code=compute_37
  - CuDNN 7.6.3
  - Magma 2.5.2
  - Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_VULKAN_WRAPPER -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF, 

TorchVision: 0.7.0
OpenCV: 4.5.4
MMCV: 1.3.18
MMCV Compiler: GCC 7.3
MMCV CUDA Compiler: 10.1
MMOCR: 0.3.0+3188e53
```
2. You may add addition that may be helpful for locating the problem, such as
    - How you installed PyTorch [e.g., pip, conda, source]
    - Other environment variables that may be related (such as `$PATH`, `$LD_LIBRARY_PATH`, `$PYTHONPATH`, etc.)

**Error traceback**
```bash
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    result = ocr.readtext(data, print_result=True, imshow=False, batch_mode=True, merge=True)
  File "/home/mpena/mmocr/mmocr/utils/ocr.py", line 426, in readtext
    self.detect_model, self.recog_model, kie_model=self.kie_model)
  File "/home/mpena/mmocr/mmocr/utils/ocr.py", line 574, in det_recog_kie_inference
    recog_model, box_imgs, True, self.args.recog_batch_size)
  File "/home/mpena/mmocr/mmocr/utils/ocr.py", line 628, in single_inference
    result = model_inference(model, arrays, batch_mode=True)
  File "/home/mpena/mmocr/mmocr/apis/inference.py", line 101, in model_inference
    if not isinstance(imgs[0], (np.ndarray, str)):
IndexError: list index out of range
```



**Bug fix**
It is necessary to check input size to ensure input image list is not empy, I am wiling to send a PR to fix this



Discussion:
Thanks for reporting that, and sure, your contribution is welcomed :)

Resolving patch:
diff --git a/mmocr/apis/inference.py b/mmocr/apis/inference.py
--- a/mmocr/apis/inference.py
+++ b/mmocr/apis/inference.py
@@ -98,6 +98,8 @@ def model_inference(model,
 
     if isinstance(imgs, (list, tuple)):
         is_batch = True
+        if len(imgs) == 0:
+            raise Exception('empty imgs provided, please check and try again')
         if not isinstance(imgs[0], (np.ndarray, str)):
             raise AssertionError('imgs must be strings or numpy arrays')
 


Test patch:
diff --git a/tests/test_apis/test_model_inference.py b/tests/test_apis/test_model_inference.py
--- a/tests/test_apis/test_model_inference.py
+++ b/tests/test_apis/test_model_inference.py
@@ -102,3 +102,20 @@ def test_model_batch_inference_recog(cfg_file):
     results = model_inference(model, [img, img], batch_mode=True)
 
     assert len(results) == 2
+
+
+@pytest.mark.parametrize(
+    'cfg_file',
+    ['../configs/textdet/psenet/psenet_r50_fpnf_600e_icdar2017.py'])
+def test_model_batch_inference_empty_detection(cfg_file):
+    tmp_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
+    config_file = os.path.join(tmp_dir, cfg_file)
+    model = build_model(config_file)
+
+    empty_detection = []
+
+    with pytest.raises(
+            Exception,
+            match='empty imgs provided, please check and try again'):
+
+        model_inference(model, empty_detection, batch_mode=True)