Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Camera in pyqt

Can we display a camera feed in Pyqt? I can only display a simple view through the opencv window in python. I want to add some more options while displaying the pyqt window.

like image 722
Shahir Ansari Avatar asked Mar 13 '26 05:03

Shahir Ansari


2 Answers

Yes, you can do it using QLabel and setting the label's QPixmap. Something vaguely along the lines of:

label = QtGui.QLabel()
image = QtGui.QImage(
    frame,
    frame.shape[1],
    frame.shape[0],
    frame.shape[1] * 3,
    QtGui.QImage.Format_RGB888
)
label.setPixmap(QtGui.QPixmap.fromImage(image))

where frame is your camera frame data.

like image 92
101 Avatar answered Mar 14 '26 19:03

101


In that case, consider using a QGraphicsView. Its not only a way to display your camera image, but you can draw additional lines, or put text on it if you want. First you initiate it:

# Create scene
self.image_item = QGraphicsPixmapItem()
scene = QGraphicsScene(self)
scene.addItem(self.image_item)

# Create GraphicView display
self.view = QGraphicsView(scene, self)
# Adding right click menus
self.view.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
self.zoomout_action = QAction("Fit canvas", self)
self.view.addAction(self.zoomout_action)

And later you put the camera image in it for display:

image = QImage(camera_image, w, h, w, QImage.Format_Grayscale8)
self.image_item.setPixmap(QPixmap.fromImage(image))
self.view.fitInView(self.image_item)
like image 22
kalzso Avatar answered Mar 14 '26 19:03

kalzso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!