'How to draw rectangle in this embedded window in PyQt5?

I make this GUI in python: enter image description here

Note: Chinese-->English: 导入视频-->load video, 框选屏幕-->The ROI screen, 框选完成-->set ROI.

I use this open_video function to import video from a directory, and get the first frame from video to display in right screen in the above picture.

    def open_video(self):
        self.video_name = QFileDialog.getOpenFileName(self, '选择视频', r'G:\dataset', '*.mp4')
        print(self.video_name[0])
        self.cap = cv2.VideoCapture(self.video_name[0])
        ret, self.first_frame = self.cap.read() 
        first_frame = cv2.cvtColor(self.first_frame, cv2.COLOR_BGR2RGB) 
        first_frame = cv2.resize(first_frame, (self.rpn_width, self.rpn_height))
        self.rpn_window.setPixmap(QPixmap.fromImage(QImage(first_frame.data,
                                                           first_frame.shape[1], first_frame.shape[0],
                                                           QImage.Format_RGB888)))  

Also, I write a function to select ROI from the first frame first_frame.

...
button2.clicked.connect(self.setROI_video)
...
def setROI_video(self):
    self.rect = draw_rectangle(self.first_frame, self.rpn_width, self.rpn_height) 
    print(self.rect)
def draw_rectangle(first_frame, width, height):
    window_name = "Select ROI, press 'q' to quit"
    cv2.namedWindow(window_name)
    color = (0, 0, 0)  # black
    start_point = (0, 0)  # (xs, ys)
    end_point = (0, 0)  # (xe, ye)
    thickness = 4
    is_down = False  # flag to check if left button was pressed before mouse move
    first_frame = cv2.resize(first_frame, (width, height))
    param = [first_frame, color, window_name, start_point, end_point, is_down, thickness]
    cv2.setMouseCallback(window_name, mouse_callback, param)
    cv2.imshow(window_name, first_frame)
    while True:
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    cv2.destroyAllWindows()
    x1 = param[3][0]
    y1 = param[3][1]
    x2 = param[4][0]
    y2 = param[4][1]
    return [min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1)]
def mouse_callback(event, x, y, flags, param):
    img = param[0].copy()
    if event == cv2.EVENT_LBUTTONDOWN:
        param[3] = (x, y)
        param[5] = True
    if event == cv2.EVENT_MOUSEMOVE:
        if param[5]:
            cv2.rectangle(img, param[3], (x, y), param[1], param[6])
    if event == cv2.EVENT_LBUTTONUP:
        param[4] = (x, y)
        cv2.rectangle(img, param[3], param[4], param[1], param[6])
        param[5] = False
    if param[5]:
        cv2.imshow(param[2], img)

But it will jump a new window from the QMainWindow, like this: enter image description here

The question is : How to select ROI from the embedded screen instead of jumping window. I want to draw rectangle from the embedded QLabel.

Thanks in advance! And I need your help please!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source