I am designing an Orange Widget for facial recognition.

I would like to draw a rectangle around faces captured in video by setting mouse events with cv2.setMouseCallback. I can capture the video in the image window (cv2.namedWindow("image")) with cv2.VideoCapture(0) but I am unable to call the MouseCallBack function in the image window.

I am new to Orange widget development and unsure if thecv2.setMouseCallback function can work inside of an OrangeWidget class. Any suggestions or help would be greatly appreciated.

Here is my code:

class MouseCallBackWidget(OWWidget):

    name = "MouseCallBack"
    # Short widget description
    description = "Draw rectangle"
    icon = "icons/DataSamplerA.svg"

    want_main_area = False
#   with a fixed non resizable geometry.
    resizing_enabled = False 

    def MouseCallBack(self,event, x, y, flags, param):
        #global boxRefPt, boxCX, boxCY, boxW, boxH, click, mode_learn, completed_rect, ix, iy 

        # check if the left mouse button is clicked.
        if event == cv2.EVENT_LBUTTONDOWN:              
            if self.completed_rect == False:                 
                self.boxRefPt = [(x, y)]                     # record the starting (x, y) coordinate of a new rectangle.
                self.ix, self.iy = x, y                           
                self.click = True                           

        # check if the left mouse button is released.        
        elif event == cv2.EVENT_LBUTTONUP:              
            self.boxRefPt.append((x, y))                     # record the ending (x, y) coordinate a new rectangle.
            self.click = False                             
            self.completed_rect = True                      
            self.mode_learn = True

            (x1, y1) = self.boxRefPt[0]
            (x2, y2) = self.boxRefPt[1]

            self.boxCX = min(x1, x2) 
            self.boxCY = min(y1, y2)
            self.boxW = abs(x1 - x2)
            self.boxH = abs(y1 - y2)

    def __init__(self):


        from AnyQt.QtGui import QIntValidator

        self.boxRefPt = []
        self.click = False
        self.mode_learn = False
        self.completed_rect = False

        self.ix = 0
        self.iy = 0
        self.boxCX = 0
        self.boxCY = 0
        self.boxW = 0
        self.boxH = 0

        self.cap = cv2.VideoCapture(0)

        cv2.setMouseCallback("image", self.MouseCallBack)    

        while (True):

            self.ret, self.frame = self.cap.read()

            if self.completed_rect == True:

                cv2.rectangle(self.frame, self.boxRefPt[0], self.boxRefPt[1], (0, 255, 0), 2)
                cv2.putText(self.frame,'LEARN AREA', (self.boxCX, (self.boxCY - 12)), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), lineType=cv2.LINE_AA)

            cv2.imshow("image", self.frame)

  • $\begingroup$ Please state a clearer main question, and elaborate on the kind of errors you get. $\endgroup$ – Leevo Jul 2 '19 at 8:51
  • $\begingroup$ And while Orange seems to be a data science suite, the programming question is really more fit for StackOverflow. $\endgroup$ – K3---rnc Jul 2 '19 at 16:06
  • $\begingroup$ There is no compilation error in the code. However, I don't see the rectangle drawn with mouse callback. The code works fine when run independently without OWWidget Class. $\endgroup$ – Vernica Jain Jul 4 '19 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.