Resizing a constant Video stream from OpenCV to PyQt5 with QPainter in a QWidget using a Raspberry Pi
-
Hello SGaist,
I am using Qt5 on Raspbian, but i also have Qt4. My project is building a functioning microscope that uses a raspberry pi. So i need the python file in autostart(which is no problem i guess). So the right resolution is full screen with room for 2 extra buttons for the whole program. Unfortunatly the camera only can stream in those resolutions 1920x1080, 1280x720, 640x480. Also it uses the AVI videoformat.
To be honest i read about Qt's multimedia module but didnt put much time into it. Is it a much easier option for me? I dont need to change the frames of my live stream so i wouldnt mind using something else.
I dont know for sure what size i get from the video stream but i guess its 640x480 because its quite small.Thanks for the fast answer and i hope you can give me some guidance.
EDIT: i searched for pyqt5 code in python but cant find much about it besides some links to the c++ equivalent
EDIT2: OK, i looked around for the multimedia module and it seems like i need to use QCamera. So what do i need to display the output of QCamera? QVideoWidget?
-
-
Hello again,
I worked with your answer in the right direction and am now at the next problem.
My Error:QObject::startTimer: Timers can only be used with threads started with QThread QObject::startTimer: Timers can only be used with threads started with QThread <built-in method description of QCameraInfo object at 0x72f56970> Traceback (most recent call last): File "qt.py", line 29, in <module> cam.iniCamera(qcam, qcaminfo) File "qt.py", line 20, in iniCamera cam = QCamera(caminfo.deviceName()) TypeError: arguments did not match any overloaded call: QCamera(parent: QObject = None): argument 1 has unexpected type 'str' QCamera(Union[QByteArray, bytes, bytearray], parent: QObject = None): argument 1 has unexpected type 'str' QCamera(QCameraInfo, parent: QObject = None): argument 1 has unexpected type 'str' QCamera(QCamera.Position, parent: QObject = None): argument 1 has unexpected type 'str'
and my code:
#import numpy as np import sys from PyQt5 import QtCore , QtWidgets, QtGui, QtMultimedia from PyQt5.QtMultimedia import QCamera, QCameraInfo, QMediaObject #from PyQt5.QtWidgets import QtVideoWidget #from PyQt5 import QtMediaObject from PyQt5.QtCore import pyqtSignal class Camera: def __init__(self): #super(Camera, self).__init__(parent) self.cam = QCamera() self.caminfo = QCameraInfo() def iniCamera(self, cam, caminfo): print (caminfo.description) if caminfo.isNull() != True: cam = QCamera(caminfo.deviceName()) print (caminfo.description) if __name__ == '__main__': cam = Camera() qcam = QCamera() qcaminfo = QCameraInfo(qcam) cam.iniCamera(qcam, qcaminfo)
I know i can use super(Camera, self).init(parent) and def init(self, parent) and the error disappears but another one occurs:
TypeError: __init__() missing 1 required positional argument: 'parent'
Do you have any idea what my problem is?
-
You Camera class is not based on QObject hence your "parent" error.
-
Hello @SGaist,
here i am again and i made quite some progress, or at least i think.
I get no real error but i still have some questions.
How can i connect QCameraViewFinder with QCamera and QVideoWidget?
How is my code looking?
Should i do something specific in a very special way that is maybe hard for a beginner?
Do you have some tips for me at this point of my project?
Thanks again for all the help you have given me :)#import numpy as np import sys from PyQt5 import QtCore , QtWidgets, QtGui, QtMultimedia, QtMultimediaWidgets from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtWidgets import QApplication from PyQt5.QtMultimedia import QCamera, QCameraInfo, QMediaObject from PyQt5.QtMultimediaWidgets import QVideoWidget, QCameraViewfinder class Camera(QObject): def __init__(self, parent): super(Camera, self).__init__(parent) self.obj = QObject() self.cam = QCamera(self.obj) self.caminfo = QCameraInfo(self.cam) def iniCamera(self, cam, caminfo): print (caminfo.description()) print(caminfo.availableCameras()) if caminfo.isNull() != caminfo.defaultCamera(): cam = QCamera(caminfo.defaultCamera()) cam.start() class VideoPlayaa(): def __init__(self): self.vid = QVideoWidget() self.camvfind = QCameraViewfinder(self.vid) def startVid(self,vid,camvfind,cam): if __name__ == '__main__': app = QtWidgets.QApplication([]) obj = QObject() cam = Camera(obj) qcam = QCamera() qcaminfo = QCameraInfo(qcam) vid = VideoPlayaa() cam.iniCamera(qcam, qcaminfo) app.exec_()
-
QCameraViewfinder is a QVideoWidget.
Call
camera.setViewfinder(viewfinder)
-
Ok i tried to implement this into my code but i am pretty sure i am still missing something for displaying the actual video. Also i am getting the Error: Segmentation fault.
Here is my code, i tried to clean it a little bit up:#import numpy as np import sys from PyQt5 import QtCore , QtWidgets, QtGui, QtMultimedia, QtMultimediaWidgets from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtWidgets import QApplication from PyQt5.QtMultimedia import QCamera, QCameraInfo, QMediaObject from PyQt5.QtMultimediaWidgets import QVideoWidget, QCameraViewfinder class Camera(QObject): def __init__(self, parent = QObject()): super(Camera, self).__init__(parent) self.obj = QObject() self.cam = QCamera(self.obj) self.caminfo = QCameraInfo(self.cam) self.cam.CaptureMode(0) def iniCamera(self): print (self.caminfo.description()) print(self.caminfo.availableCameras()) if self.caminfo.isNull() != self.caminfo.defaultCamera(): self.cam = QCamera(self.caminfo.defaultCamera()) self.cam.load() self.cam.start() class VideoPlayaa(QObject): def __init__(self, parent = QObject()): super(VideoPlayaa, self).__init__(parent) self.vid = QVideoWidget() self.camvfind = QCameraViewfinder(self.vid) self.cam = QCamera() def startVid(self): self.cam.setViewfinder(self.camvfind) self.camvfind.show() if __name__ == '__main__': app = QtWidgets.QApplication([]) cam = Camera() cam.iniCamera() vid = VideoPlayaa() vid.startVid() app.exec_()
Can you tell me how to progress with my project? How do i insert camera.setViewfinder(viewfinder) in a window? Should i maybe start a new thread because the original question is now rather unimportant?
-
With the code you show here, it seems you are getting lost.
You should take look at the QtMultimedia module samples before going further.
There's no need for QVideoWidget as QCameraViewfinder is already a QVideoWidget. You also don't need for two separated classes.
-
Ok, i looked at the multimedia samples and learned a bit from them. Sadly the code is all c++ and not python so i couldnt get that much out of it, besides that i should use (maybe) QMainWindow.
I put some tests in my code and the "Segmentation fault" error seems to be a big problem somewhere in the startVid() function. The CaptureMode(0) is sadly not supported and i have to use CaptureMode(2). Could this be a problem? Anyway, thanks for all the support from you, as always here is my code:import sys from PyQt5 import QtCore , QtWidgets, QtGui, QtMultimedia, QtMultimediaWidgets from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtWidgets import QApplication from PyQt5.QtMultimedia import QCamera, QCameraInfo, QMediaObject, QCameraViewfinderSettings from PyQt5.QtMultimediaWidgets import QCameraViewfinder class Camera(QObject): def __init__(self, parent = QObject()): super(Camera, self).__init__(parent) self.obj = QObject() self.cam = QCamera(self.obj) self.caminfo = QCameraInfo(self.cam) self.camvfind = QCameraViewfinder() self.camvfindset = QCameraViewfinderSettings() self.cammode = self.cam.CaptureMode(2) def iniCamera(self): print(self.caminfo.description()) print(self.caminfo.availableCameras()) if self.cam.isCaptureModeSupported(self.cammode): print("Capturemode supported") self.cam.setCaptureMode(self.cammode) if self.caminfo.isNull() != self.caminfo.defaultCamera(): self.cam = QCamera(self.caminfo.defaultCamera()) self.cam.load() self.cam.start() def startVid(self): self.cam.setViewfinder(self.camvfind) self.camvfindset.setResolution(1280,720) print("self.cam.supportedViewfinderFrameRateRanges(self.camvfind)") self.camvfindset.setMinimumFrameRate(15) self.camvfind.show() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) cam = Camera() cam.iniCamera() cam.startVid() sys.exit(app.exec_())
PS: i actually managed to open a window, but sadly its just black and doesnt show up consistent.
-
What is the use of
self.obj
?if self.caminfo.isNull() != self.caminfo.defaultCamera():
You are comparing a boolean with a QCameraInfo object.Then you are replace your current
cam
object with a new so your capture mode related code is useless.You should start by just translating the QCameraViewfinder example to python.
-
@SGaist Ok i did translate the example to python and it actually did alot. Now a window with the camera feed pops up. Unfortunatly its not very consistent since sometimes the ol' Segmentation fault error still shows and then no window opens. I think thats very strange.
Also when the camera feed opens the terminal output is:** (python3:1197): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files Venus USB2.0 Camera [<PyQt5.QtMultimedia.QCameraInfo object at 0x72fafa30>] Capturemode supported QWidget::paintEngine: Should no longer be called QWidget::paintEngine: Should no longer be called
Whats up with QWidget::paintEngine: Should no longer be called?
Do you know how i could add buttons to this window? Do i guess i need layers and a MainWindow, but where should i look for more information?
As always here is my code:import sys from PyQt5 import QtCore , QtWidgets, QtGui, QtMultimedia, QtMultimediaWidgets from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtWidgets import QApplication from PyQt5.QtMultimedia import QCamera, QCameraInfo, QMediaObject, QCameraViewfinderSettings, QCameraImageCapture from PyQt5.QtMultimediaWidgets import QCameraViewfinder class Camera(QObject): def __init__(self, parent = QObject()): super(Camera, self).__init__(parent) self.cam = QCamera() self.caminfo = QCameraInfo(self.cam) self.camvfind = QCameraViewfinder() self.camvfindset = QCameraViewfinderSettings() self.cammode = self.cam.CaptureMode(1) self.camimgcap = QCameraImageCapture(self.cam) def iniCamera(self): print(self.caminfo.description()) print(self.caminfo.availableCameras()) if self.cam.isCaptureModeSupported(self.cammode): print("Capturemode supported") def startVid(self): self.camvfind.show() self.cam.setViewfinder(self.camvfind) self.cam.load() self.camvfindset.setResolution(1280,720) #print(self.cam.supportedViewfinderFrameRateRanges(self.camvfind)) self.camvfindset.setMinimumFrameRate(15) self.cam.setCaptureMode(self.cammode) self.cam.start() #self.cam.unload() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) cam = Camera() cam.iniCamera() cam.startVid() del cam sys.exit(app.exec_())
-
Why are you calling
del cam
just beforesys.exit(app.exec_())
? -
Don't delete objects while you are using them ;)
Can you get a stack trace of the crash ?
-
Ok, it seems like its not possible to catch a Segmentation fault error with stack trace. I used "gdb" to get some information about the error. Here is the output:
(gdb) run qt.py Starting program: /usr/bin/python3 qt.py [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1". [New Thread 0x720a7470 (LWP 20003)] [New Thread 0x716ff470 (LWP 20004)] ** (python3:19975): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files [New Thread 0x6ff00470 (LWP 20007)] [New Thread 0x6f5ff470 (LWP 20008)] [New Thread 0x6e6ab470 (LWP 20022)] [Thread 0x6e6ab470 (LWP 20022) exited] [New Thread 0x6e6ab470 (LWP 20023)] [Thread 0x6e6ab470 (LWP 20023) exited] Venus USB2.0 Camera [<PyQt5.QtMultimedia.QCameraInfo object at 0x72fde030>] Capturemode supported Thread 1 "python3" received signal SIGSEGV, Segmentation fault. 0x7344b620 in platform_get_handle () from /opt/vc/lib/libEGL.so
I read many threads about this problem and it could be that i run out of memory. Since Geany-Output is:
Segmentation fault ------------------ (program exited with code: 139) Press return to continue
and code :139 means "attempt to access a virtual address which is not in your address space"
EDIT: the problem could also occur because some of the modules are written in C++. I read that some problems can happen because of this
here is more output of gdb that "should" give more info
(gdb) bt #0 0x733d4620 in platform_get_handle () from /opt/vc/lib/libEGL.so #1 0x733c9f2c in eglCreateWindowSurface () from /opt/vc/lib/libEGL.so #2 0x722e7004 in ?? () from /usr/lib/arm-linux-gnueabihf/qt5/plugins/xcbglintegrations/libqxcb-egl-integration.so Backtrace stopped: previous frame identical to this frame (corrupt stack?)
-
Ok, well i can give you some more info. It seems that the Segmentation fault is because of this line
self.camvfind.show()
or the line after that. I just tested this with putting print("1") after every line and looking how many lines of 1s get printed.Also someone from stackoverflow wrote this :"Most likely there is a race condition in some of the modules you load and someone accesses memory before someone else has set it up. It looks like the program is looking for a handle in libEGL and that function is triggering the segfault."
This person also suggested that i look at the memory load of the program but it doesnt seem to matter because the memory load is sometimes high and it works and sometimes its lower and it doesnt work -
Do you have several versions of Qt installed on your device ?
-
I have qt5 and qt4 but i deinstalled qt4 and some folders are still left. They are in: /usr/lib/arm-linux-gnueabihf/qt4
/usr/share/qt4
Also output ofqmake --version
is :QMake version 3.0 Using Qt version 5.7.1 in /usr/lib/arm-linux-gnueabihf
-
How did you install PyQt ?
-
I tried to install PyQt from source but it always gave me an error so i just used sudo apt-get install pyqt5-dev/pyqt5-dev-tools/python3-pyqt5.qtmultimedia.
I also wrote my question with Segmentation Fault on other forums and someone said: "There is a buffer overflow happening in the eglCreateWindowSurface function". To be honest i dont know exactly what to do with this or how this would help me with my problem. The eglCreateWindowSurface function has something with openGl to do, maybe there is a problem with it? You got an idea?