cvhciKIT / sloth

Sloth is a tool for labeling image and video data for computer vision research.
Other
608 stars 198 forks source link

sloth.items.RectItem.__call__() operator seems broken #124

Open PuchatekwSzortach opened 7 years ago

PuchatekwSzortach commented 7 years ago

Documentation states that RectItem and friends have a __call()__ method implemented so that one can use a modified RectItem instance as a value corresponding to "item" key in LABELS portion of config file. This is to allow use like in following snipped taken from documentation:

RedRectItem = items.RectItem()
RedRectItem.setColor(Qt.red)
GreenRectItem = items.RectItem()
GreenRectItem.setColor(Qt.green)

ITEMS = {
    "rect" : RedRectItem,
    "head" : GreenRectItem,
}

However trying to put an instance of RectItem into "item" portion of LABELS causes an exception. Here's a relevant portion of a sample config:

LABELS = (

{"attributes": {"type":  "rect",
                "class": "face",
                },
 "item":     sloth.items.RectItem(),
 "inserter": "sloth.items.RectItemInserter",
 "text":     "Face",
},

And here's the error message that occurs when trying to add a label:

Traceback (most recent call last):
  File "/Users/k.jakub/anaconda3/envs/qt4/lib/python3.5/site-packages/sloth/gui/annotationscene.py", line 337, in rowsInserted
    self.insertItems(first, last)
  File "/Users/k.jakub/anaconda3/envs/qt4/lib/python3.5/site-packages/sloth/gui/annotationscene.py", line 111, in insertItems
    item = self._itemfactory.create(label_class, child)
  File "/Users/k.jakub/anaconda3/envs/qt4/lib/python3.5/site-packages/sloth/items/factory.py", line 85, in create
    return item(*args, **kwargs)
  File "/Users/k.jakub/anaconda3/envs/qt4/lib/python3.5/site-packages/sloth/items/items.py", line 366, in __call__
    item = RectItem(model_item, parent)
  File "/Users/k.jakub/anaconda3/envs/qt4/lib/python3.5/site-packages/sloth/items/items.py", line 361, in __init__
    self._updateRect(self._dataToRect(self._model_item))
  File "/Users/k.jakub/anaconda3/envs/qt4/lib/python3.5/site-packages/sloth/items/items.py", line 376, in _dataToRect
    return QRectF(float(model_item[self.prefix() + 'x']),
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Using class, or "item": sloth.items.RectItem, rather than instance, or "item": sloth.items.RectItem() works.

I'm using Sloth installed from source from commit 10c9f59.

nilsonholger commented 7 years ago

So, are you saying the documentation is wrong in that regard and the examples should be something like...

RedRectItem = items.RectItem      # no ()
RedRectItem.setColor(Qt.red)
GreenRectItem = items.RectItem    # no ()
GreenRectItem.setColor(Qt.green)

ITEMS = {
    "rect" : RedRectItem,
    "head" : GreenRectItem,
}
PuchatekwSzortach commented 7 years ago

@nilsonholger Yes to lines 1 and 3, but I doubt that RedRectItem.setColor(Qt.red) and GreenRectItem.setColor(Qt.green) will work if RedRectItem and GreenRectItem are classes, not instances. How I implemented it in my code was by subclassing items.RectItem and making appropriate calls to self.setColor() in subclasses' constructors.

nilsonholger commented 7 years ago

Of course, but could you please show me the snippet then as you would see it work? Thanks!

PuchatekwSzortach commented 7 years ago

Here is my config.py for a very similar use-case:

from PyQt4.QtGui import QPen
from PyQt4.Qt import Qt
import sloth.items

class GreenItem(sloth.items.RectItem):

    def __init__(self, *args, **kwargs):

        sloth.items.RectItem.__init__(self, *args, **kwargs)
        self.setColor(Qt.green)

class RedItem(sloth.items.RectItem):

    def __init__(self, *args, **kwargs):

        sloth.items.RectItem.__init__(self, *args, **kwargs)
        self.setColor(Qt.red)

LABELS = (

    {"attributes": {"type":  "rect",
                    "class": "Face",
                    },
     "item":     GreenItem,
     "text":     "Face"
    },

    {"attributes": {"type":  "rect",
                    "class": "Not-Face",
                    },
     "item":     RedItem,
     "text":     "Not-Face"
    },
)