I tried using QVideoProbe to capture and send my camera video frames for processing (object detection) in another python process. The problem is that the conversion of the probed QVideoFrame into QImage fails.
Conversion to byte array using the suggestions from here fails because frame.bits() is always 0 and PixelFormat is Format_Invalid for every frame.
Im running my code on raspberry pi 4 with Rpi Os Buster and Qt 5.11.3.
Is there a better way for sending the video frames to an external process?
UPDATE: The conversion works fine after upgrading RPI OS from Buster to Bullseye. I`m still wondering if this is the recommended method for sending the video frames to an external process.
import sys
from PySide2 import QtCore, QtMultimedia
from PySide2.QtMultimedia import *
from PySide2.QtMultimediaWidgets import *
from PySide2.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.available_cameras = QCameraInfo.availableCameras()
if not self.available_cameras:
print("No camera found.")
sys.exit()
self.camera = QCamera(self.available_cameras[0])
self.camera.setCaptureMode(QCamera.CaptureViewfinder)
self.current_camera_name = self.available_cameras[0].description()
self.capture = QCameraImageCapture(self.camera)
self.capture.setCaptureDestination(QCameraImageCapture.CaptureToBuffer)
self.probe = QtMultimedia.QVideoProbe(self)
self.probe.videoFrameProbed.connect(self.processFrame)
self.probe.setSource(self.camera)
self.viewfinder = QCameraViewfinder()
self.viewfinder.show()
self.setCentralWidget(self.viewfinder)
self.camera.setViewfinder(self.viewfinder)
self.setGeometry(100, 100, 800, 600)
self.show()
self.camera.start()
def processFrame(self, frame):
QApplication.processEvents()
if frame.isValid():
frame.map(QAbstractVideoBuffer.MapMode.ReadOnly)
buffer = QtCore.QBuffer()
buffer.open(QtCore.QBuffer.ReadWrite)
frame.image().save(buffer, "JPEG")
frame.unmap()
def closeEvent(self, event):
if self.probe.isActive():
self.probe.videoFrameProbed.disconnect(self.processFrame)
self.probe.deleteLater()
self.camera.stop()
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())