Если по какой-либо причине нужно использовать цикл while, то посмотрите мой код. Я не специалист, написал как сумел, но код работает.
main
import sys
import cv2
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.uic import loadUi
class New(QDialog):
def __init__(self):
super(New, self).__init__()
loadUi("form_cam.ui", self)
self.SHOW.clicked.connect(self.camera)
def displayImage(self, img, window): # Эта функция необходима для отрисовки в виджете Label
qformat = QImage.Format_Indexed8
if len(img.shape) == 3:
if (img.shape[2]) == 4:
qformat = QImage.Format_RGBA8888
else:
qformat = QImage.Format_RGB888
img = QImage(img, img.shape[1], img.shape[0], qformat)
img = img.rgbSwapped()
self.imgLabel.setScaledContents(True)
self.imgLabel.setPixmap(QPixmap.fromImage(img))
def camera(self):
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
self.displayImage(frame, 1) # Это вместо привычного cv2.imshow()
cv2.waitKey(1)
# cv2.imshow('Video', frame)
def closeEvent(self, e):
super().closeEvent(e)
sys.exit() # добавил для остановки процессов в IDE
if __name__ == '__main__':
app = QApplication(sys.argv)
window = New()
window.show()
sys.exit(app.exec_())
form_cam.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>663</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="sizeGripEnabled">
<bool>false</bool>
</property>
<property name="modal">
<bool>false</bool>
</property>
<widget class="QGroupBox" name="groupBox_3">
<property name="geometry">
<rect>
<x>11</x>
<y>11</y>
<width>778</width>
<height>641</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(200, 191, 200);</string>
</property>
<property name="title">
<string>GroupBox</string>
</property>
<widget class="QLabel" name="imgLabel">
<property name="geometry">
<rect>
<x>70</x>
<y>90</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>640</width>
<height>480</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>640</width>
<height>480</height>
</size>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);</string>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<property name="lineWidth">
<number>7</number>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="wordWrap">
<bool>false</bool>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>80</x>
<y>40</y>
<width>291</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
</font>
</property>
<property name="text">
<string>Трансляция с вэб камеры:</string>
</property>
</widget>
<widget class="QPushButton" name="SHOW">
<property name="geometry">
<rect>
<x>350</x>
<y>600</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>пуск</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>