IBM / vision-tools

IBM Maximo Visual Inspection CLI Tools and APIs
Apache License 2.0
23 stars 25 forks source link

AttributeError when executing `vision deployed-models infer --annotatefile FILE` #40

Closed nogayama closed 3 years ago

nogayama commented 3 years ago

Symptom

AttributeError when executing vision deployed-models infer --annotatefile FILE

AttributeError: 'NoneType' object has no attribute 'lower'

reproduce

$ vision deployed-models infer /tmp/hoge.JPG  --modelid=e1a90034-7fa0-469a-b81e-9e7b6f6fc082 --annotatefile /tmp/hoge.a.JPG
Traceback (most recent call last):
  File "/Users/nogayama1/.pyenv/versions/3.9.1_vision_tool2/bin/vision", line 7, in <module>
    exec(compile(f.read(), __file__, 'exec'))
  File "/Users/nogayama1/Data/project/vag/workspace/vision-tools/cli/vision", line 104, in <module>
    pkg.main(argv, cmd_flags=args)
  File "/Users/nogayama1/Data/project/vag/workspace/vision-tools/lib/vapi_cli/deployed-models.py", line 349, in main
    args.operation(args.op_params)
  File "/Users/nogayama1/Data/project/vag/workspace/vision-tools/lib/vapi_cli/deployed-models.py", line 203, in infer
    drawAnnotationsOnFile(params, filepath, server.json(), annotateFile)
  File "/Users/nogayama1/Data/project/vag/workspace/vision-tools/lib/vapi_cli/deployed-models.py", line 214, in drawAnnotationsOnFile
    determineMarkInfo(params)
  File "/Users/nogayama1/Data/project/vag/workspace/vision-tools/lib/vapi_cli/deployed-models.py", line 260, in determineMarkInfo
    incolor = params.get("--color", "red").lower()
AttributeError: 'NoneType' object has no attribute 'lower'
nogayama commented 3 years ago

If we store None as value of dict, we face this error.

$ ipython 
Python 3.9.1 (default, Feb 12 2021, 17:20:20) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: d = {"a": 10, "b": None}

In [2]: d.get("a", "red")
Out[2]: 10

In [3]: d.get("b", "red") is None
Out[3]: True

In [4]: d.get("c", "red")
Out[4]: 'red'

In [5]: d.get("b", "red").lower()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-5a428f4cd3b2> in <module>
----> 1 d.get("b", "red").lower()
nogayama commented 3 years ago

The workaround is

nogayama commented 3 years ago

But unfortunately there are too many params.get(key, default_value) in this tool. Fixing all lines by the workaround above is not realistic.

https://github.com/IBM/vision-tools/search?q=params.get%28

nogayama commented 3 years ago

All params variables are instance of docopt.ParsedOptions(dict). So we may be able to fix this issue by hooking this class.

nogayama commented 3 years ago

I found a fundamental solution for this issue.

We can check the return value by overriding get method of ParsedOptions.

class ParsedOptions(dict):
    ...
    def get(self, key, default_value=None):
        if key in self:
            value = super().get(key)
            if value is not None:
                return value
        return default_value
nogayama commented 3 years ago

I opened a pull request with my solution #41. Please merge it.

nogayama commented 3 years ago

@bcarld Thanks!