OpenCV waitkey() returning last key press after destroyallwindows()

Viewed 31

I'm creating a function to annotate an image and was hoping to cycle through images in a folder. Subsequent images after the first are closed almost instantly. I did some debugging and found that cv2.waitkey(-1) is returning the value from the previous key press (in this case 113 or q), even if there is no additional keypress. What should I be doing differently to "reset' waitKey()?

Here is (the stripped down) annotater for testing.

class Annotater(object):
    def __init__(self, image_path):
        self.original_image = cv2.imread(image_path)
        self.clone = self.original_image.copy()

        cv2.namedWindow('image')
        cv2.setMouseCallback('image', self.extract_coordinates)

        # Bounding box reference points
        self.image_coordinates = []

    def extract_coordinates(self, event, x, y, flags, parameters):
        # Record starting (x,y) coordinates on left mouse button click
        if event == cv2.EVENT_LBUTTONDOWN:
            self.image_coordinates.append((x,y))
            print(self.image_coordinates)

        # Clear drawing boxes on right mouse button click
        elif event == cv2.EVENT_RBUTTONDOWN:
            self.clone = self.original_image.copy()

    def show_image(self):
        return self.clone

if __name__ == '__main__':
    filename = 'test.jpg'
    cv2.destroyAllWindows()
    annotater_widget = Annotater(filename)
    while True:
        key = -1
        cv2.imshow('image', annotater_widget.show_image())
        key = cv2.waitKey(-1)

    # Close program with keyboard 'q'
        if key == ord('q'):
            cv2.destroyAllWindows()
            break
0 Answers
Related