Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Keep track of number of rectangles drawn over an image using Mouse Events



  • I am working with the following tool:
    https://github.com/tzutalin/labelImg

    I want to extend the functionality of the tool. I am almost halfway through but I would like to now know how to identify how many rectangles the user has drawn over the image.

    The script to draw rectangle is this: https://github.com/tzutalin/labelImg/blob/master/libs/canvas.py

    One of the ways I have been thinking about is keeping track of MousePress and MouseRelease events. But I am not sure whether this is the way to go.

    But I am not entirely sure how to do this. I would appreciate any help I can get.

    Thank You.


  • Lifetime Qt Champion

    Hi
    Are those rectangle not the Shapes the code talk about ?
    So they are store in self.shapes ?
    That seems like a list so can you ask the list about its size and that would how many rectangles user added ?



  • @mrjj Yep, thats true. But I am wondering, how to use this "self.shapes" from this script in the main script which is : https://github.com/tzutalin/labelImg/blob/master/labelImg.py

    I can see that the size of the list changes when the rectangle is drawn but I want to compare this in another script as I mentioned above (link).


  • Lifetime Qt Champion

    @duddal_

    Hi
    Maybe I miss something but in labelImg.py, a canvas object is made in MainWindow
    so self.canvas should give you access. (when in MainWindows scope)



  • @mrjj
    Sorry for the late reply. Yes, I forgot to mention it here. But as you said I can access the shapes using self.canvas.shapes.

    I actually have another doubt regarding the same script but wondering whether I should open a new issue or not. What do you think?


  • Lifetime Qt Champion

    @duddal_
    hi i think here is fine since there is ref to the script.



  • Thanks @mrjj

    So there is a variable called self.canvas.shapes which is a list.
    Every time a rectangle is drawn over the image the list gets updated. It contains the "Shape" object for each rectangle.
    For example: [<libs.shape.Shape object at 0x000002638F8FE448>, <libs.shape.Shape object at 0x000002638C89A848>]

    I would like to modify the code such that it outputs the following:
    [[<libs.shape.Shape object at 0x000002638F8FE448>], [<libs.shape.Shape object at 0x000002638F8FE448>, <libs.shape.Shape object at 0x000002638C89A848>]]

    A list of lists so that I can compare the length of the previous and the current.

    So in simple terms it should be: test = [[A], [A,B], [A,B,C]]
    So that I can now compare len(test[-1]) and len(test[-2])

    I have tried looking into canvas.py script but unable to find a solution.


  • Lifetime Qt Champion

    @duddal_
    Hi
    So you want a list of lists?

    Wont that upset the rest of the drawing code ?

    • So that I can now compare len(test[-1]) and len(test[-2])

    That will return the size of the lists ?



  • Hello @mrjj ,

    Yes, it will return the size of the lists.
    I am not sure whether it will upset the drawing code, to be honest.

    I have a button that changes the brightness of the image. After a rectangle is drawn on the image I push that button. And after every new rectangle is drawn I first have to save the image file (label file). So, for example, The user has drawn a bounding box, and then once the button is pushed a window appears which allows the user to save the image file. And without drawing any more rectangles the button should just change the brightness instead of asking to save the image file again. Therefore I thought if I have a list of lists of the rectangles drawn one after the other, I will be able to compare the size and then it is just the matter of comparing the previous and current size to determine whether a new rectangle has been drawn or not.

    I can't quite figure out if there is another way of doing this or not.


  • Lifetime Qt Champion

    @duddal_

    Hi
    If you keep your own list, i guess nothing bad will happen :)

    But if the goal is only to keep track of rectangles (shapes)
    im not sure i understand why you want to keep a list to track it.

    Say we have a new variable.
    ShapeCountBefore=0;

    Then when your SaveButton is pressed
    we compare
    ShapeCountBefore and Shapes.Size()
    if they are the same, no new rect was added.

    if ( ShapeCountBefore LESS than Shapes.Size() )
    then a new rect was added. and then we
    update ShapeCountBefore=Shapes.Size()

    if ( ShapeCountBefore BIGGER than Shapes.Size() ) it means
    rect was removed

    Maybe i dont 100% understand what you want as you say drawn for the rect and not added.


Log in to reply