Я пытаюсь сделать некий виджет, в котором будет отображаться видео с вебкамеры используя модуль cv2. У меня получилось это сделать, но только форме квадрата, а я хочу в форме круга. Вот тут есть пример как сделать QPixmap скругленным, но мне не удалось применить к моему случаю.
Вот ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(231, 221)
self.display_webcam_label = QtWidgets.QLabel(Form)
self.display_webcam_label.setGeometry(QtCore.QRect(0, 0, 231, 221))
font = QtGui.QFont()
font.setPointSize(12)
self.display_webcam_label.setFont(font)
self.display_webcam_label.setStyleSheet("background: #111;\n"
"color: #fff;\n"
"border-radius: 0px;\n"
"border: 0;")
self.display_webcam_label.setText("")
self.display_webcam_label.setAlignment(QtCore.Qt.AlignCenter)
self.display_webcam_label.setObjectName("display_webcam_label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Вот stackoverflow.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from ui import Ui_Form
import cv2
class Thread1(QThread):
changePixmap = pyqtSignal(QImage)
def __init__(self, webcam_live, parent=None):
super().__init__()
self.webcam_live = webcam_live
def run(self):
cap1 = cv2.VideoCapture(0, cv2.CAP_DSHOW)
ret1, image1 = cap1.read()
try:
im1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
except Exception as e:
self.exit_thr_and_error.emit()
self.webcam_live.height1, self.webcam_live.width1, channel1 = im1.shape
cap1.set(3,480)
cap1.set(4,640)
cap1.set(5,30)
while True:
ret1, image1 = cap1.read()
if ret1:
im1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
self.webcam_live.height1, self.webcam_live.width1, channel1 = im1.shape
step1 = channel1 * self.webcam_live.width1
qImg1 = QImage(im1.data, self.webcam_live.width1, self.webcam_live.height1, step1, QImage.Format_RGB888)
self.changePixmap.emit(qImg1)
class Widget(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super().__init__()
self.setupUi(self)
self.dragPos = QtCore.QPoint()
self.move(0, 0)
self.height1 = 0
self.width1 = 0
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.th1 = Thread1(webcam_live=self)
self.th1.changePixmap.connect(self.setImage)
self.th1.start()
# Widget move event
def mouseMove(event):
if event.buttons() == QtCore.Qt.LeftButton:
self.move(self.pos() + event.globalPos() - self.dragPos)
self.dragPos = event.globalPos()
event.accept()
self.mouseMoveEvent = mouseMove
def mousePressEvent(self, event):
self.dragPos = event.globalPos()
#
@QtCore.pyqtSlot(QImage)
def setImage(self, qImg1):
pixmap = QPixmap.fromImage(qImg1)
self.display_webcam_label.setPixmap(pixmap.scaled(250, 250, Qt.KeepAspectRatioByExpanding))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())