I've a PyQt code which keeps on crashes giving me error message QPixmap::fromImage: QPixmap cannot be created without a QGuiApplication QPixmap: Must construct a QGuiApplication before a QPixmap
It's a fairly simple application in which I read frames from one class named CameraWidget
and apply a function def transform_perspective(self, frame, points)
to get the transform perspective of that frame. I've divided my screen in two equal haves, the right half section displays the frame as seen by the camera and the left half displays the perspective transformation of it (for which I get the coordinates from another class named Canvas
).
There is one more issue: the left half doesn't occupy it's entire region. A lot of portion just appears black. Here is a pic for your reference
It's a fairly long program so below I'm including the class where I believe the problem lies.
class TopView(QtWidgets.QWidget):
def __init__(self, parent=None):
super(TopView, self).__init__(parent)
self.original_frame = CameraWidget('Abc.ts')
# Layouts and frames
self.frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.frame)
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
self.setLayout(layout)
# frame left
self.frame_left = QtWidgets.QFrame()
self.get_frame_thread = Thread(target=self.transform_frame, args=())
self.get_frame_thread.daemon = True
self.get_frame_thread.start()
self.top_view_label = QtWidgets.QLabel()
self.top_view_label.setScaledContents(True)
self.layout_left = QtWidgets.QVBoxLayout()
self.layout_left.addWidget(self.top_view_label)
self.layout_left.setContentsMargins(0,0,0,0)
self.layout_left.setSpacing(0)
self.frame_left.setLayout(self.layout_left)
# frame right
self.frame_right = QtWidgets.QFrame()
self.frame_right.setStyleSheet("background-color: rgb(153, 187, 255)")
self.video_frame_1 = self.original_frame
# Create camera widgets
print('Creating Camera Widgets...')
self.layout_right = QtWidgets.QVBoxLayout()
self.layout_right.addWidget(self.video_frame_1)
self.layout_right.setContentsMargins(5,5,5,5)
self.frame_right.setLayout(self.layout_right)
self.layout_inner = QtWidgets.QHBoxLayout()
self.layout_inner.addWidget(self.frame_left, 50)
self.layout_inner.addWidget(self.frame_right, 50)
self.layout_inner.setContentsMargins(0,0,0,0)
self.layout_inner.setSpacing(0)
self.frame.setLayout(self.layout_inner)
self.setLayout(layout)
sizeObject = QtWidgets.QDesktopWidget().screenGeometry(0)
self.screen_width = int(0.7*sizeObject.width())
self.screen_height = int(0.7*sizeObject.height())
def event(self, e):
if e.type() in (QtCore.QEvent.Show, QtCore.QEvent.Resize):
print('')
return QtWidgets.QWidget.event(self, e)
def transform_frame(self):
while True:
try:
self.top_view_frame = self.transform_perspective(self.original_frame.get_video_frame(), self.original_frame.canvas.mapped_list)
h, w, ch = self.top_view_frame.shape
bytesPerLine = ch * w
self.img = QtGui.QImage(self.top_view_frame, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
self.pix = QtGui.QPixmap.fromImage(self.img)
if not sip.isdeleted(self.top_view_label):
self.top_view_label.setPixmap(self.pix)
except Exception as e:
print(e)
def transform_perspective(self, frame, points):
points = np.float32(points)
p2 = np.float32([[0, 0], [600, 0], [0, 600], [600, 600]])
self.matrix = cv2.getPerspectiveTransform(points, p2)
frame = cv2.warpPerspective(frame, self.matrix, (400, 600))
return frame