我如何才能让该程序自行触发cv.waitKey()以便退出此循环?

时间:2019-07-11 06:03:07

标签: python opencv pynput

问题::我希望代码在绘制矩形后退出循环,但无法正常工作!我有一个cv.waitKey()等待被按下的循环。如何从某些外部启动器退出循环?我是从别人的代码开始的。

我尝试使用pynput,但是由于某种原因它不起作用...这是代码,一旦在图像上绘制了矩形,我想退出while循环。

import numpy as np
import cv2 as cv
from pynput.keyboard import Key, Controller
from time import sleep



class Interface:

    def __init__(self, image):

        self.ix = 0
        self.iy = 0
        self.ix2 = 0
        self.iy2 = 0
        self.drawing = False
        self.before_rect = image.copy()
        self.image = image.copy()
        self.result_img = image.copy()

    # Adding Function Attached To Mouse Callback
    def draw(self,event,x,y,flags,params):

        # Left Mouse Button Down Pressed
        if(event==1):
            self.drawing = True
            self.ix = x
            self.iy = y
        # Moving mouse
        if(event==0):
            self.ix2 = x
            self.iy2 = y
            if self.drawing == True:
                self.image = (self.before_rect).copy()
                cv.rectangle(self.image, pt1=(self.ix, self.iy), pt2=(x, y), color=(255, 255, 255), thickness=3)
        # Left Mouse Button Up
        if(event==4):
            if self.drawing == True:
                self.ix2 = x
                self.iy2 = y
                # For Drawing Rectangle
                cv.rectangle(self.result_img, pt1=(self.ix, self.iy), pt2=(x, y), color=(255, 255, 255), thickness=3)
                self.image = (self.result_img).copy()


                '''Here is the issue!!! How do I solve it?'''

                # Doesn't do anything to exit the loop like I wanted :(
                print('pressed') # (output to terminal) -> pressed 
                keyboard = Controller()
                keyboard.press(Key.esc)
                sleep(.01)
                keyboard.release(Key.esc)

            self.drawing = False

    def get_boxes(self):

        cv.namedWindow("Window")

        # Adding Mouse CallBack Event
        cv.setMouseCallback("Window", self.draw)

        # Starting The Loop So Image Can Be Shown
        while (True):
            cv.imshow("Window", self.image)
            if cv.waitKey(5) & 0xFF == 27:
                break
        cv.destroyAllWindows()

        return


# Making The Blank Image
orig = np.zeros((512,512,3))

# Start drawing boxes!
obj = Interface(orig)
obj.get_boxes()

-它正在执行pynput按键(我知道,因为它会打印“已按下”),但是它不会对代码产生任何影响。伤心。

谢谢您的帮助!〜(^ \ _`_ / ^)〜

1 个答案:

答案 0 :(得分:0)

使用布尔标志

我认为使用 flags (即告诉您何时运行或中断循环的布尔变量)不是更好地使用Python语言,而不是通过关键命令来隐式中断循环。这对您来说特别容易,因为您正在使用实例化的对象,该实例是在其方法之间(通过self参数共享相同范围的实例)。

例如,在您的__init__()函数中,您可以初始化变量self.loop_running = True,该变量是在实例化时定义的。

随后,在您要触发循环中断的draw()方法中,将此标志设置为False,例如:

def drawing(*args):
    # some code
    if self.drawing:
        self.ix2 = x
        self.iy2 = y
        # For Drawing Rectangle
        cv.rectangle(self.result_img, pt1=(self.ix, self.iy), pt2=(x, y), color=(255, 255, 255), thickness=3)
        self.image = (self.result_img).copy()
        # set flag to false ->
        self.loop_running = False

然后在最终函数中调用主循环,将while True更改为while self.loop_running

def get_boxes(self):

    cv.namedWindow("Window")

    # Adding Mouse CallBack Event
    cv.setMouseCallback("Window", self.draw)

    # Starting The Loop So Image Can Be Shown
    while self.loop_running:
        cv.imshow("Window", self.image)
    cv.destroyAllWindows()

    return