Shahir Ansari
Shahir Ansari

Reputation: 1848

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.

Upvotes: 3

Views: 4418

Answers (2)

kalzso
kalzso

Reputation: 536

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)

Upvotes: 4

101
101

Reputation: 8999

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.

Upvotes: 4

Related Questions