-3

I have found some code which should work, but don't.. What am I doing wrong / is missing?

Source: How to find table like structure in image

def find_table_in_boxes(boxes, cell_threshold=10, min_columns=2):
    for box in boxes:
        (x, y, w, h) = box
        print(box)
        col_key = x // cell_threshold

if __name__ == '__main__':
    text_boxes = [
        {
            'x': 123,
            'y': 512,
            'w': 100,
            'h': 150
        },
        {
            'x': 500,
            'y': 512,
            'w': 100,
            'h': 150
        }
    ]
    cells = find_table_in_boxes(text_boxes)

error

# python test.py
{'x': 123, 'w': 100, 'y': 512, 'h': 150}
Traceback (most recent call last):
  File "test.py", line 22, in <module>
    cells = find_table_in_boxes(text_boxes)
  File "test.py", line 5, in find_table_in_boxes
    col_key = x // cell_threshold
TypeError: unsupported operand type(s) for //: 'str' and 'int'
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • What is the code suppose to do? – Dani Mesejo Jan 10 '19 at 19:09
  • 13
    Read the error -- it says `unsupported operand types(s) for //: 'str' and 'int'`. That means you're trying to divide a string and an integer. – Calvin Godfrey Jan 10 '19 at 19:09
  • 1
    do `print(x, y, w, h)` to see what you're doing in there. – castis Jan 10 '19 at 19:10
  • `(x, y, w, h) = box` assigns the keys of `box`, that is, strings `'x'`, `'y'`, etc., to the variables `x,y,w,h`. – DYZ Jan 10 '19 at 19:10
  • 1
    change `(x, y, w, h) = box` to `(x, y, w, h) = box["x"], box["y"], box["w"], box["h"]` – eyllanesc Jan 10 '19 at 19:10
  • @CalvinGodfrey isn't both vars int? They looks like integers to me when looking in the code – clarkk Jan 10 '19 at 19:10
  • 1
    @clarkk Obviosly not, as DYZ already explained. – glglgl Jan 10 '19 at 19:11
  • 2
    You are trying to floor division on an int and a string – Frontear Jan 10 '19 at 19:11
  • 2
    See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. "Looking at the code" is not a particularly detailed or accurate debugging tactic. Been there, done that, made a quilt from all the t-shirts ... – Prune Jan 10 '19 at 19:12
  • It's unclear why you thought that *would* work; what made you think a dictionary would be the same as a bounding rectangle? You need to *adapt* code you find for your specific needs (the change you *did* make, from assigning to a list of names to assigning to a tuple of names, doesn't make any difference). Also this question is generally of a very disappointing standard for a user with your rep. – jonrsharpe Jan 10 '19 at 19:12

1 Answers1

8

It's telling you x is a string. It comes from boxes which comes from text_boxes. You cannot unpack dictionary values with:

    (x, y, w, h) = box

Try this instead:

x = box["x"]
y = box["y"]
w = box["w"]
h = box["h"]

Another option is:

def _find_table_in_box(cell_threshold, x, y, w, h):
    print(x, y, w, h)
    col_key = x // cell_threshold


def find_table_in_boxes(boxes, cell_threshold=10, min_columns=2):
    for box in boxes:
        _find_table_in_box(cell_threshold, **box)
kichik
  • 33,220
  • 7
  • 94
  • 114