'PyQt5 I want to show camera view to label
This is my frame:
I want to show camera view to label Label object name is camera But the program finished without showing anything What is the problem? Why I cant show? Code:
class Widget(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("mainwindow.ui",self)
self.v = QVBoxLayout()
self.v.addWidget(self.camera)
self.Worker1 = Worker1()
self.Worker1.start()
self.setLayout(self.v)
class Worker1(QThread):
ImageUpdate = pyqtSignal(QImage)
def run(self):
self.ThreadActive = True
Capture = cv2.VideoCapture(0)
while self.ThreadActive:
ret, frame = Capture.read()
if ret:
frame = cv2.flip(frame, 180)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (311, 221))
image = QImage(frame.data, frame.shape[1], frame.shape[0],frame.strides[0],
QImage.Format_RGB888)
self.camera.setPixmap(QPixmap.fromImage(image))
def stop(self):
self.ThreadActive = False
self.quit()
if __name__ == "__main__":
app = QApplication([])
window = Widget()
window.show()
sys.exit(app.exec_())
Solution 1:[1]
A little bit break and I solved my problem with this code:
from PyQt5.uic import loadUi
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import cv2
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
super(MainWindow, self).__init__()
loadUi("mainwindow.ui", self)
self.camera = realTimeVideo(self.camera)
self.camera.start()
class realTimeVideo(QThread):
def __init__(self, label):
super(realTimeVideo, self).__init__()
self.label = label
def run(self):
self.cap = cv2.VideoCapture("ig_At.mp4")
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
#self.out = cv2.VideoWriter('output.avi', fourcc, 25.0, (640, 480))
while (self.cap.isOpened()):
ret, frame = self.cap.read()
if ret == True:
frame = cv2.flip(frame, 180)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (311, 221))
image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888)
self.label.setPixmap(QPixmap.fromImage(image))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cv2.destroyAllWindows()
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
sys.exit(app.exec_())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

