prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Webcam demo script is not working properly
  **Checklist**
  
  1. I have searched related issues but cannot get the expected help: Yes
  2. The bug has not been fixed in the latest version: Yes
  
  **Describe the bug**
  
  The current model_inference function expects to receive a model and a path to an image as inputs but the webcam demo scripts tries to call it with a model and a numpy array (the return value from cv2.VideoCapture.read()). 
  
  This raises an assertion error due to type mismatch (np.ndarray vs str)
  
  **Reproduction**
  
  1. What command or script did you run?
  
  ```none
  python demo/webcam_demo.py
  ```
  
  2. Did you make any modifications on the code or config? Did you understand what you have modified?
  
  No.
  
  3. What dataset did you use?
  
  **Environment**
  
  1. Please run `python mmocr/utils/collect_env.py` to collect necessary environment information and paste it here.
  
  sys.platform: linux
  Python: 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 16:07:37) [GCC 9.3.0]
  CUDA available: True
  GPU 0: GeForce GTX 1050 Ti
  CUDA_HOME: /usr/local/cuda
  NVCC: Build cuda_11.1.TC455_06.29190527_0
  GCC: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
  PyTorch: 1.5.0
  PyTorch compiling details: PyTorch built with:
    - GCC 7.3
    - C++ Version: 201402
    - Intel(R) Math Kernel Library Version 2020.0.4 Product Build 20200917 for Intel(R) 64 architecture applications
    - Intel(R) MKL-DNN v0.21.1 (Git Hash 7d2fd500bc78936d1d648ca713b901012f470dbc)
    - 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 -fopenmp -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_INTERNAL_THREADPOOL_IMPL -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-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.6.0a0+82fd1c8
  OpenCV: 4.5.1
  MMCV: 1.2.7
  MMCV Compiler: GCC 9.3
  MMCV CUDA Compiler: not available
  MMOCR: 0.1.0+344cc9a
  
  2. You may add addition that may be helpful for locating the problem, such as
      - How you installed PyTorch: conda
  
  **Error traceback**
  
  ```none
  Use load_from_local loader
  Press "Esc", "q" or "Q" to exit.
  Traceback (most recent call last):
    File "demo/webcam_demo.py", line 52, in <module>
      main()
    File "demo/webcam_demo.py", line 41, in main
      result = model_inference(model, img)
    File "/home/sbugallo/Projects/mmocr/mmocr/apis/inference.py", line 18, in model_inference
      assert isinstance(img, str)
  AssertionError
  ```
  
  **Bug fix**
  
  The inference method should accept the following types as input image(s) (str/ndarray or list[str/ndarray] or tuple[str/ndarray]) like in MMDetection
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `mmocr/apis/inference.py`
  Content:
  ```
  1 import torch
  2 from mmcv.ops import RoIPool
  3 from mmcv.parallel import collate, scatter
  4 
  5 from mmdet.datasets.pipelines import Compose
  6 
  7 
  8 def model_inference(model, img):
  9     """Inference image(s) with the detector.
  10 
  11     Args:
  12         model (nn.Module): The loaded detector.
  13         imgs (str): Image files.
  14 
  15     Returns:
  16         result (dict): Detection results.
  17     """
  18     assert isinstance(img, str)
  19 
  20     cfg = model.cfg
  21     device = next(model.parameters()).device  # model device
  22     data = dict(img_info=dict(filename=img), img_prefix=None)
  23     # build the data pipeline
  24     test_pipeline = Compose(cfg.data.test.pipeline)
  25     data = test_pipeline(data)
  26     data = collate([data], samples_per_gpu=1)
  27 
  28     # process img_metas
  29     if isinstance(data['img_metas'], list):
  30         data['img_metas'] = data['img_metas'][0].data
  31     else:
  32         data['img_metas'] = data['img_metas'].data[0]
  33 
  34     if next(model.parameters()).is_cuda:
  35         # scatter to specified GPU
  36         data = scatter(data, [device])[0]
  37     else:
  38         for m in model.modules():
  39             assert not isinstance(
  40                 m, RoIPool
  41             ), 'CPU inference with RoIPool is not supported currently.'
  42 
  43     # forward the model
  44     with torch.no_grad():
  45         result = model(return_loss=False, rescale=True, **data)[0]
  46     return result
  47
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/mmocr/apis/inference.py b/mmocr/apis/inference.py
  --- a/mmocr/apis/inference.py
  +++ b/mmocr/apis/inference.py
  @@ -1,7 +1,9 @@
  +import numpy as np
   import torch
   from mmcv.ops import RoIPool
   from mmcv.parallel import collate, scatter
   
  +from mmdet.datasets import replace_ImageToTensor
   from mmdet.datasets.pipelines import Compose
   
   
  @@ -10,18 +12,33 @@
   
       Args:
           model (nn.Module): The loaded detector.
  -        imgs (str): Image files.
  +        imgs (str/ndarray): Image files.
   
       Returns:
           result (dict): Detection results.
       """
  -    assert isinstance(img, str)
  +
  +    assert isinstance(img, (str, np.ndarray))
   
       cfg = model.cfg
       device = next(model.parameters()).device  # model device
  -    data = dict(img_info=dict(filename=img), img_prefix=None)
  -    # build the data pipeline
  +
  +    if isinstance(img, np.ndarray):
  +        cfg = cfg.copy()
  +        # set loading pipeline type
  +        cfg.data.test.pipeline[0].type = 'LoadImageFromWebcam'
  +
  +    cfg.data.test.pipeline = replace_ImageToTensor(cfg.data.test.pipeline)
       test_pipeline = Compose(cfg.data.test.pipeline)
  +
  +    if isinstance(img, np.ndarray):
  +        # directly add img
  +        data = dict(img=img)
  +    else:
  +        # add information into dict
  +        data = dict(img_info=dict(filename=img), img_prefix=None)
  +
  +    # build the data pipeline
       data = test_pipeline(data)
       data = collate([data], samples_per_gpu=1)
  
verification_info: {"golden_diff": "diff --git a/mmocr/apis/inference.py b/mmocr/apis/inference.py\n--- a/mmocr/apis/inference.py\n+++ b/mmocr/apis/inference.py\n@@ -1,7 +1,9 @@\n+import numpy as np\n import torch\n from mmcv.ops import RoIPool\n from mmcv.parallel import collate, scatter\n \n+from mmdet.datasets import replace_ImageToTensor\n from mmdet.datasets.pipelines import Compose\n \n \n@@ -10,18 +12,33 @@\n \n     Args:\n         model (nn.Module): The loaded detector.\n-        imgs (str): Image files.\n+        imgs (str/ndarray): Image files.\n \n     Returns:\n         result (dict): Detection results.\n     \"\"\"\n-    assert isinstance(img, str)\n+\n+    assert isinstance(img, (str, np.ndarray))\n \n     cfg = model.cfg\n     device = next(model.parameters()).device  # model device\n-    data = dict(img_info=dict(filename=img), img_prefix=None)\n-    # build the data pipeline\n+\n+    if isinstance(img, np.ndarray):\n+        cfg = cfg.copy()\n+        # set loading pipeline type\n+        cfg.data.test.pipeline[0].type = 'LoadImageFromWebcam'\n+\n+    cfg.data.test.pipeline = replace_ImageToTensor(cfg.data.test.pipeline)\n     test_pipeline = Compose(cfg.data.test.pipeline)\n+\n+    if isinstance(img, np.ndarray):\n+        # directly add img\n+        data = dict(img=img)\n+    else:\n+        # add information into dict\n+        data = dict(img_info=dict(filename=img), img_prefix=None)\n+\n+    # build the data pipeline\n     data = test_pipeline(data)\n     data = collate([data], samples_per_gpu=1)\n", "issue": "Webcam demo script is not working properly\n**Checklist**\r\n\r\n1. I have searched related issues but cannot get the expected help: Yes\r\n2. The bug has not been fixed in the latest version: Yes\r\n\r\n**Describe the bug**\r\n\r\nThe current model_inference function expects to receive a model and a path to an image as inputs but the webcam demo scripts tries to call it with a model and a numpy array (the return value from cv2.VideoCapture.read()). \r\n\r\nThis raises an assertion error due to type mismatch (np.ndarray vs str)\r\n\r\n**Reproduction**\r\n\r\n1. What command or script did you run?\r\n\r\n```none\r\npython demo/webcam_demo.py\r\n```\r\n\r\n2. Did you make any modifications on the code or config? Did you understand what you have modified?\r\n\r\nNo.\r\n\r\n3. What dataset did you use?\r\n\r\n**Environment**\r\n\r\n1. Please run `python mmocr/utils/collect_env.py` to collect necessary environment information and paste it here.\r\n\r\nsys.platform: linux\r\nPython: 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 16:07:37) [GCC 9.3.0]\r\nCUDA available: True\r\nGPU 0: GeForce GTX 1050 Ti\r\nCUDA_HOME: /usr/local/cuda\r\nNVCC: Build cuda_11.1.TC455_06.29190527_0\r\nGCC: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0\r\nPyTorch: 1.5.0\r\nPyTorch compiling details: PyTorch built with:\r\n  - GCC 7.3\r\n  - C++ Version: 201402\r\n  - Intel(R) Math Kernel Library Version 2020.0.4 Product Build 20200917 for Intel(R) 64 architecture applications\r\n  - Intel(R) MKL-DNN v0.21.1 (Git Hash 7d2fd500bc78936d1d648ca713b901012f470dbc)\r\n  - OpenMP 201511 (a.k.a. OpenMP 4.5)\r\n  - NNPACK is enabled\r\n  - CPU capability usage: AVX2\r\n  - CUDA Runtime 10.1\r\n  - 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\r\n  - CuDNN 7.6.3\r\n  - Magma 2.5.2\r\n  - Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_INTERNAL_THREADPOOL_IMPL -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-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, \r\n\r\nTorchVision: 0.6.0a0+82fd1c8\r\nOpenCV: 4.5.1\r\nMMCV: 1.2.7\r\nMMCV Compiler: GCC 9.3\r\nMMCV CUDA Compiler: not available\r\nMMOCR: 0.1.0+344cc9a\r\n\r\n2. You may add addition that may be helpful for locating the problem, such as\r\n    - How you installed PyTorch: conda\r\n\r\n**Error traceback**\r\n\r\n```none\r\nUse load_from_local loader\r\nPress \"Esc\", \"q\" or \"Q\" to exit.\r\nTraceback (most recent call last):\r\n  File \"demo/webcam_demo.py\", line 52, in <module>\r\n    main()\r\n  File \"demo/webcam_demo.py\", line 41, in main\r\n    result = model_inference(model, img)\r\n  File \"/home/sbugallo/Projects/mmocr/mmocr/apis/inference.py\", line 18, in model_inference\r\n    assert isinstance(img, str)\r\nAssertionError\r\n```\r\n\r\n**Bug fix**\r\n\r\nThe inference method should accept the following types as input image(s) (str/ndarray or list[str/ndarray] or tuple[str/ndarray]) like in MMDetection\n", "before_files": [{"content": "import torch\nfrom mmcv.ops import RoIPool\nfrom mmcv.parallel import collate, scatter\n\nfrom mmdet.datasets.pipelines import Compose\n\n\ndef model_inference(model, img):\n    \"\"\"Inference image(s) with the detector.\n\n    Args:\n        model (nn.Module): The loaded detector.\n        imgs (str): Image files.\n\n    Returns:\n        result (dict): Detection results.\n    \"\"\"\n    assert isinstance(img, str)\n\n    cfg = model.cfg\n    device = next(model.parameters()).device  # model device\n    data = dict(img_info=dict(filename=img), img_prefix=None)\n    # build the data pipeline\n    test_pipeline = Compose(cfg.data.test.pipeline)\n    data = test_pipeline(data)\n    data = collate([data], samples_per_gpu=1)\n\n    # process img_metas\n    if isinstance(data['img_metas'], list):\n        data['img_metas'] = data['img_metas'][0].data\n    else:\n        data['img_metas'] = data['img_metas'].data[0]\n\n    if next(model.parameters()).is_cuda:\n        # scatter to specified GPU\n        data = scatter(data, [device])[0]\n    else:\n        for m in model.modules():\n            assert not isinstance(\n                m, RoIPool\n            ), 'CPU inference with RoIPool is not supported currently.'\n\n    # forward the model\n    with torch.no_grad():\n        result = model(return_loss=False, rescale=True, **data)[0]\n    return result\n", "path": "mmocr/apis/inference.py"}], "after_files": [{"content": "import numpy as np\nimport torch\nfrom mmcv.ops import RoIPool\nfrom mmcv.parallel import collate, scatter\n\nfrom mmdet.datasets import replace_ImageToTensor\nfrom mmdet.datasets.pipelines import Compose\n\n\ndef model_inference(model, img):\n    \"\"\"Inference image(s) with the detector.\n\n    Args:\n        model (nn.Module): The loaded detector.\n        imgs (str/ndarray): Image files.\n\n    Returns:\n        result (dict): Detection results.\n    \"\"\"\n\n    assert isinstance(img, (str, np.ndarray))\n\n    cfg = model.cfg\n    device = next(model.parameters()).device  # model device\n\n    if isinstance(img, np.ndarray):\n        cfg = cfg.copy()\n        # set loading pipeline type\n        cfg.data.test.pipeline[0].type = 'LoadImageFromWebcam'\n\n    cfg.data.test.pipeline = replace_ImageToTensor(cfg.data.test.pipeline)\n    test_pipeline = Compose(cfg.data.test.pipeline)\n\n    if isinstance(img, np.ndarray):\n        # directly add img\n        data = dict(img=img)\n    else:\n        # add information into dict\n        data = dict(img_info=dict(filename=img), img_prefix=None)\n\n    # build the data pipeline\n    data = test_pipeline(data)\n    data = collate([data], samples_per_gpu=1)\n\n    # process img_metas\n    if isinstance(data['img_metas'], list):\n        data['img_metas'] = data['img_metas'][0].data\n    else:\n        data['img_metas'] = data['img_metas'].data[0]\n\n    if next(model.parameters()).is_cuda:\n        # scatter to specified GPU\n        data = scatter(data, [device])[0]\n    else:\n        for m in model.modules():\n            assert not isinstance(\n                m, RoIPool\n            ), 'CPU inference with RoIPool is not supported currently.'\n\n    # forward the model\n    with torch.no_grad():\n        result = model(return_loss=False, rescale=True, **data)[0]\n    return result\n", "path": "mmocr/apis/inference.py"}]}