Masec
2018-12-12 08:02
采纳率: 100%
浏览 5.9k

PyQt5中如何在lable中加载的图片上绘制矩形框呢?

我的程序是通过摄像头采集一帧图像,点击开始标记按钮后显示在按钮左侧的lable中(灰色框)。
我希望可以在lable中加载的一帧图像中通过点击鼠标左键开始绘制矩形,按住左键拖动时改变矩形的形状,松开左键后完成绘制。(和lableImg软件类似)。
请问如何实现上述功能呢?我目前找到的方法是使用QPainter来实现,但是在我的代码中该如何实现呢?万分感谢!

图片说明

附上我的完整代码如下:

# -*- coding: utf-8 -*-
import sys
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

# ~ class MyLabel(QLabel):
    # ~ def paintEvent(self, event):
        # ~ width = self.pos2[0]-self.pos1[0]
        # ~ height = self.pos2[1] - self.pos1[1]     
        # ~ qp = QPainter()
        # ~ qp.begin(self)           
        # ~ qp.drawRect(self.pos1[0], self.pos1[1], width, height)        
        # ~ qp.end()

    # ~ def mousePressEvent(self, event):
        # ~ self.pos1[0], self.pos1[1] = event.pos().x(), event.pos().y()
        # ~ print("clicked")

    # ~ def mouseReleaseEvent(self, event):
        # ~ self.pos2[0], self.pos2[1] = event.pos().x(), event.pos().y()
        # ~ print("released")
        # ~ self.update()

class Ui_train_window(QtWidgets.QWidget):
    def setupUi(self, train_window):
        train_window.setObjectName("train_window")
        train_window.resize(690, 600)
        train_window.setMaximumSize(QtCore.QSize(690, 600))
        self.horizontalLayoutWidget = QtWidgets.QWidget(train_window)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 10, 681, 80))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.lab_enter_name = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.lab_enter_name.setObjectName("lab_enter_name")
        self.horizontalLayout.addWidget(self.lab_enter_name)
        self.le_username = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
        self.le_username.setObjectName("le_username")
        self.horizontalLayout.addWidget(self.le_username)
        self.lab_enter_pw = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.lab_enter_pw.setObjectName("lab_enter_pw")
        self.horizontalLayout.addWidget(self.lab_enter_pw)
        self.le_userpw = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
        self.le_userpw.setObjectName("le_userpw")
        self.le_userpw.setEchoMode(QLineEdit.Password)
        self.horizontalLayout.addWidget(self.le_userpw)
        self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.le_ipadr = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
        self.le_ipadr.setObjectName("le_ipadr")
        self.horizontalLayout.addWidget(self.le_ipadr)
        self.btn_openIPcam = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.btn_openIPcam.setMinimumSize(QtCore.QSize(50, 10))
        self.btn_openIPcam.setObjectName("btn_openIPcam")
        self.horizontalLayout.addWidget(self.btn_openIPcam)

        self.show = QtWidgets.QLabel(train_window)
        self.show.setGeometry(QtCore.QRect(15, 110, 531, 371))
        self.show.setObjectName("show")
        self.show.setStyleSheet(("border:2px solid lightgray"))

        self.verticalLayoutWidget = QtWidgets.QWidget(train_window)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(560, 100, 111, 381))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.btn_start = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.btn_start.setObjectName("btn_start")
        self.verticalLayout.addWidget(self.btn_start)
        self.combo_label = QtWidgets.QComboBox(self.verticalLayoutWidget)
        self.combo_label.setObjectName("combo_label")
        self.combo_label.addItem("")
        self.combo_label.addItem("")
        self.combo_label.addItem("")
        self.combo_label.addItem("")
        self.combo_label.addItem("")
        self.combo_label.addItem("")
        self.verticalLayout.addWidget(self.combo_label)
        self.btn_save = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.btn_save.setObjectName("btn_save")
        self.verticalLayout.addWidget(self.btn_save)
        self.btn_finish = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.btn_finish.setObjectName("btn_finish")
        self.verticalLayout.addWidget(self.btn_finish)
        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(train_window)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(0, 500, 681, 71))
        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.lab_stepnum = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
        self.lab_stepnum.setObjectName("lab_stepnum")
        self.horizontalLayout_2.addWidget(self.lab_stepnum)
        self.le_stepnm = QtWidgets.QLineEdit(self.horizontalLayoutWidget_2)
        self.le_stepnm.setObjectName("le_stepnm")
        self.horizontalLayout_2.addWidget(self.le_stepnm)
        self.lab_batchsize = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
        self.lab_batchsize.setObjectName("lab_batchsize")
        self.horizontalLayout_2.addWidget(self.lab_batchsize)
        self.le_batchsize = QtWidgets.QLineEdit(self.horizontalLayoutWidget_2)
        self.le_batchsize.setObjectName("le_batchsize")
        self.horizontalLayout_2.addWidget(self.le_batchsize)
        self.btn_pretrain = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
        self.btn_pretrain.setObjectName("btn_pretrain")
        self.horizontalLayout_2.addWidget(self.btn_pretrain)
        self.btn_retrain = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
        self.btn_retrain.setObjectName("btn_retrain")
        self.horizontalLayout_2.addWidget(self.btn_retrain)
        self.show.raise_()
        self.horizontalLayoutWidget.raise_()
        self.verticalLayoutWidget.raise_()
        self.horizontalLayoutWidget_2.raise_()

        self.retranslateUi(train_window)
        self.btn_openIPcam.clicked.connect(self.IPinfo)
        self.btn_start.clicked.connect(self.labledata)
        QtCore.QMetaObject.connectSlotsByName(train_window)

    def retranslateUi(self, train_window):
        _translate = QtCore.QCoreApplication.translate
        train_window.setWindowTitle(_translate("train_window", "训练工具"))
        self.lab_enter_name.setText(_translate("train_window", "输入IP摄像头用户名:"))
        self.lab_enter_pw.setText(_translate("train_window", "输入IP摄像头密码:"))
        self.label.setText(_translate("train_window", "IP地址"))
        self.btn_openIPcam.setText(_translate("train_window", "连接IP摄像头"))
        self.show.setText(_translate("train_window", ""))
        self.btn_start.setText(_translate("train_window", "开始标记"))
        self.combo_label.setItemText(0, _translate("train_window", "a"))
        self.combo_label.setItemText(1, _translate("train_window", "b"))
        self.combo_label.setItemText(2, _translate("train_window", "c"))
        self.combo_label.setItemText(3, _translate("train_window", "d"))
        self.combo_label.setItemText(4, _translate("train_window", "e"))
        self.combo_label.setItemText(5, _translate("train_window", "f"))
        self.btn_save.setText(_translate("train_window", "保存"))
        self.btn_finish.setText(_translate("train_window", "标记结束"))
        self.lab_stepnum.setText(_translate("train_window", "迭代次数:"))
        self.lab_batchsize.setText(_translate("train_window", "batchsize:"))
        self.btn_pretrain.setText(_translate("train_window", "预训练"))
        self.btn_retrain.setText(_translate("train_window", "重新训练"))

    def IPinfo(self):
        username = self.le_username.text()
        password = self.le_userpw.text()
        ipaddress = self.le_ipadr.text()
        #cam_rtsp_addr = "rtsp://" + username + ":" + password + "@" + ipaddress + "/h264/ch33/main/av_stream"
        #self.camcapture = cv2.VideoCapture(cam_rtsp_addr)
        self.camcapture = cv2.VideoCapture(0)
        self.timer = QtCore.QTimer()
        self.timer.start()
        self.timer.setInterval(0.3) 
        self.timer.timeout.connect(self.camshow)

    def camshow(self):
        global showImage
        _ , camimg = self.camcapture.read()
        camimg = cv2.cvtColor(camimg, cv2.COLOR_BGR2RGB)
        showImage = QtGui.QImage(camimg.data, camimg.shape[1], camimg.shape[0], QtGui.QImage.Format_RGB888)

    def labledata(self):
        self.show.setPixmap(QtGui.QPixmap.fromImage(showImage))

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    Window = QtWidgets.QWidget()
    ui = Ui_train_window()
    ui.setupUi(Window)
    Window.show()
    sys.exit(app.exec_())

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Masec 2018-12-13 07:19
    已采纳

    emmmm……解决了……手动笑哭脸。
    定义了一个QLable类,在这个类中定义鼠标事件。 然后将self.show(show这个名字起的很不好……改成了showimg)这个lable改为我们定义的lable。
    然后只显示train-window界面即可。

    点赞 打赏 评论

相关推荐 更多相似问题