我正在设计用于面部识别的Orange Widget。我想通过使用cv2.setMouseCallback设置鼠标事件来围绕视频中捕获的面部绘制一个矩形。我可以使用cv2.VideoCapture(0)在图像窗口(cv2.namedWindow(“ image”))中捕获视频,但无法在图像窗口中调用MouseCallBack函数。我是Orange控件开发的新手,不确定cv2.setMouseCallback函数是否可以在OrangeWidget类中使用。任何建议/帮助将不胜感激。这是代码:
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):
super().__init__()
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.namedWindow("image")
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)
cv2.waitKey(1)