0

I have a list of coordinates [xmin, xmax, ymin, ymax] on a list: [[xmin_a, xmax_a, ymin_a, ymax_a], [xmin_b, xmax_b, ymin_b, ymax_b]] and I want to remove the best result. For example, the [xmin_b, xmax_b, ymin_b, ymax_b]. For this, I tried using the listname.index() function but it returns 0. Through some prints I can tell the element is there, what is wrong with my code?

screenshot of the print evidence

This is the code excert that is troubled:

#Loop for each ground truth object compare with all the detections
        for obj in groundtruth:
            print('\t',obj)
            
            max_iou = 0
            for det in detectedlines:
                print('\t>',det)
                iou = calc_iou(obj, det, img_width, img_height)
                if (iou > max_iou):
                    max_iou = iou
                    det_store = det
            
            #removes the pred box with the best IoU from list
            index_det = detectedlines.index(det_store)
            print(index_det)
            print(det_store)
            print(detectedlines)
            detectedlines.remove(index_det)
            print(detectedlines)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
diogofd8
  • 27
  • 6
  • 1
    Please try to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). In particular, what input value for `detectedlines` do you need in order to cause the problem? Does the `for obj in groundtruth:` outer loop actually matter? What does `calc_iou` do, and does it matter to causing the problem? – Karl Knechtel Oct 26 '21 at 22:46
  • Also, [please do not use screenshots](//meta.stackoverflow.com/q/285551) for textual console output. Instead, copy and paste your output, formatted as code. If you need to make clear what output corresponds to which debug `print` statement, then use the `print` arguments to make that clear (format in some text labels). – Karl Knechtel Oct 26 '21 at 22:48
  • Anyway, the error here is that `list.remove` **does not expect** an index, but instead a value - which it then *searches for*. You would know this if you read the [documentation](https://docs.python.org/3/library/stdtypes.html?highlight=list%20remove#mutable-sequence-types) - which is as simple as trying `help(list.remove)` at the interpreter prompt. The way to remove an item directly given its index is with the `del` statement: `del detectedlines[index_det]`, or with the `pop` method: `detectedlines.pop(index_det)`. – Karl Knechtel Oct 26 '21 at 22:53
  • Please also see https://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index . You can easily find this previous question using a search engine. Please keep in mind that [you are expected to do some research before asking](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Karl Knechtel Oct 26 '21 at 22:54

1 Answers1

0

You could use the enumerate() function which on each loop of the list will yield a tuple such that the 0th index is the index in the list and the 1st index is the element from the list detectedlines itself. You can store the index in the same way you did for max_iou. When you go to remove the element, you should use the pop method as remove tries to find an equal object in the list to remove.

Sorry if this is not exactly what you are looking for.

#Loop for each ground truth object compare with all the detections
        for obj in groundtruth:
            print('\t',obj)
            
            max_iou = 0
            index_det = None
            for elem in enumerate(detectedlines):
                det = elem[1]
                idx = elem[0]
                print('\t>',det)
                iou = calc_iou(obj, det, img_width, img_height)
                if (iou > max_iou):
                    max_iou = iou
                    det_store = det
                    index_det = idx
            
            #removes the pred box with the best IoU from list
            index_det = detectedlines.index(det_store)
            print(index_det)
            print(det_store)
            print(detectedlines)
            removed_element = detectedlines.pop(index_det)
            print(detectedlines)
Joshua Reisbord
  • 111
  • 2
  • 7