I am trying to plot live data that is streamed from a CMOS camera in python. As a little side note: I want to use this to stabilize the pointing of a laser in the lab.
I am using ImageItem from the pyqtgraph module and my script is basically a modification of the ImageItem example (see below).
However when I execute the script I immediately get a delay of about 3 seconds and the RAM memory increases until the process gets killed. The resolution of the camera is 3672x5496 pixel which is quiet high and it seems like the pictures are queuing up until it is out of memory (my interpretation of the problem).
I tried to clear the ImageItem module every time when it gets updated but it does not change anything.
What am I doing wrong? Is it even possible to get this working live and without running out of memory with this resolution? I am already happy with about 1-2 frames per seconds.
## Add path to library (just for examples; you do not need this)
#import initExample
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import pyqtgraph as pg
import pyqtgraph.ptime as ptime
import gxipy as gx
import sys
## Check for connected devices
device_manager = gx.DeviceManager()
dev_num, dev_info_list = device_manager.update_device_list()
if dev_num == 0:
sys.exit(1)
## Connect to the camera
strSN = dev_info_list[0].get("sn")
cam = device_manager.open_device_by_sn(strSN)
## start the data stream
cam.stream_on()
app = QtGui.QApplication([])
## Create window with GraphicsView widget
win = pg.GraphicsLayoutWidget()
win.show() ## show widget alone in its own window
win.setWindowTitle('pyqtgraph example: ImageItem')
view = win.addViewBox()
## lock the aspect ratio so pixels are always square
view.setAspectLocked(True)
## Create image item
img = pg.ImageItem(border='w')
view.addItem(img)
## Set initial view bounds
view.setRange(QtCore.QRectF(0, 0, 3672, 5496))
## Create random image
updateTime = ptime.time()
def updateData():
global img, i, updateTime
## Get a new image from the camera
raw_image = cam.data_stream[0].get_image()
data = raw_image.get_numpy_array()
if data is None:
sys.exit(1)
## Display the image
img.setImage(data)
QtCore.QTimer.singleShot(1, updateData)
updateData()
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
cam.stream_off()
cam.close_device()
Related
I'm trying to figure out how to use Signal to send custom messages/commands between widgets. I've been up and down all kinds of sources and I still can't figure it out.
The base purpose is that, I want to set a picture in GUI (custom button) that when I click on it, it changes the index number of a stacked widget. But to simplify, I figured click on image, it just prints a message, like 'hi' --> But even better would be if that message could be a predefined string (ala, the index number that I want to connect my image to)
I have so many setups but I'm going to try and write here what I think is my best.
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
import sys
from PIL import Image, ImageQt
def just_go():
app.exit()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
screen = app.primaryScreen()
size = screen.size()
# <Code that sizes the window based on screensize>
program_window = window.size()
if program_window.width() > 200 and program_window.height() > 100:
s_exit = QtWidgets.QPushButton(text="Quit", parent=window)
s_exit.setGeometry(program_window.width() - 140, program_window.height() - 50, 120, 30)
s_exit.clicked.connect(just_go)
image = ImageQt(Image.open('file'))
clicker = QLabel(window)
clicker.setPixmap(image)
b_buffer = QWidget(window)
foreground = QStackedLayout()
page_0 = QWidget()
page_1 = QWidget()
page_2 = QWidget()
foreground.addWidget(page_0)
foreground.addWidget(page_1)
foreground.addWidget(page_2)
b_buffer.setLayout(foreground)
window.show()
sys.exit(app.exec_())
and then:
def switch_tabs(page):
foreground.setCurrentIndex(page)
Now I'm trying to make my project modular, so the actual file is in multiple python files but how would I make a Signal in one widget that emits to another? And then have a function run whenever that signal is sent?
I am trying to move from matplotlib to plotting with pyqtgraph because of its touted capabilities to render and save images faster. In my attempts to do this on a cluster with multiprocessors, I run into the following trouble:
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to
'/tmp/runtime-user' qt.qpa.screen: QXcbConnection: Could not connect
to display Could not connect to any X display.
How do I obviate displaying a plot, and save it directly to file? Here's my attempt at the code:
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import pyqtgraph.exporters
#app = QtGui.QApplication([])
#view = pg.GraphicsView()
l = pg.GraphicsWindow()
#view.setCentralItem(l)
#view.show()
#view.setWindowTitle('GraphicsLayout')
#view.resize(1000,1600)
def plotlayout(lines):
p_res={}
p_data={}
for rows in rows:
p_res[row]={}
p_data[row]={}
for col in cols:
l2=l.addLayout()
p_res[row][col]=l2.addPlot()
p_res[row][col].hideAxis('bottom')
l2.nextRow()
p_data[row][col]=l2.addPlot()
l.nextColumn()
l.nextRow()
return p_res, p_data
pl = plotlayout(lines)
pl[0].plot([1,3,5,9,7,8],[2,3,3,5,6,8])
pl[1].plot([1,3,5,9,7,8],[2,22,3,45,6,8])
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
QtGui.QApplication.exit()
QtGui.QApplication.quit()
QtGui.QApplication.quitOnLastWindowClosed()
QtGui.QApplication.closeAllWindows()
exporter = pg.exporters.ImageExporter(l.scene())
exporter.export('fits.ps')
I have tested this on my personal laptop and it works fine.
If you want to run a GUI without using desktop environment/window manager a possible solution is to use Xvfb.
How can I use PyQt5/PySide or any other Python library to display a desired image on a secondary monitor in full-screen mode? In the past, I used a framebuffer image viewer (Fbi and Fbi improved). However, this approach requires me to use Linux. I prefer to work in Windows and preferably find a solution using Python.
Motivation/Context
I am working on a DLP projection based 3D printing process. When I connect a DLP projector to my Windows PC using HDMI, it shows up as a second monitor. I want to dedicate this secondary monitor (DLP) only to display my desired patterns images (png, bmp, or svg) for the 3D printing process. I would like to programmatically control using Python which image is being displayed.
This is a followup question to https://3dprinting.stackexchange.com/questions/1217/how-to-display-images-on-dlp-using-hdmi-for-3d-printing
Partial solution and issues
Below code is one possible solution, however I am unsure if its the correct or the most efficient approach. I found two approaches using PyQt5: 1) using splash screen, and 2) using QLabel. I am facing the following issues with my code:
Cursor is hidden as expected, however if I accidentally click mouse on secondary screen, the splash screen closes.
If I use the QLabel approach, I see a white screen appear and then my image gets loaded. There is a distinct delay of ~ 0.5-1s from the time white screen appears to when the actual image is displayed.
If the images are displayed in high frequency (ex: every 1 sec), this code doesn't work well. For example, in the code change the total_loops=1 to total_loops=25. When using splash screen method, I see the splash screen appear on the main screen then it moves to the secondary screen. When using the QLabel method, all I see is a white screen appear, and only the last iteration the image is displayed. In addition, the window of the QLabel becomes active on the main screen and is visible in the Task bar.
How do I handle a situation if I want to display a video instead of an image?
For 3D printing application, the solution needs to meet the following requirement:
Secondary screen is the DLP projector, and it should NOT contain any OS related windows/taskbars/etc...
No cursor/mouse or other applications should appear on the the secondary screen
Images/videos need to be displayed in fullscreen mode
When displaying or updating images on the secondary screen, there should be no disturbance on the primary screen. For example, the image window in secondary screen shouldn't take focus away from currently active window in the primary screen
import time
start_time = time.time()
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QSplashScreen
from PyQt5.QtGui import QPixmap, QCursor
from PyQt5.QtCore import Qt
import os
app = QApplication(sys.argv)
total_loops = 1
for i in range(total_loops):
# https://doc.qt.io/qtforpython/index.html
# https://www.riverbankcomputing.com/static/Docs/PyQt5/module_index.html
s = app.screens()[1] # Get the secondary screen
# Display info about secondary screen
print('Screen Name: {} Size: {}x{} Available geometry {}x{} '.format(s.name(), s.size().width(), s.size().height(), s.availableGeometry().width(), s.availableGeometry().height()))
# Hide cursor from appearing on screen
app.setOverrideCursor(QCursor(Qt.BlankCursor)) # https://forum.qt.io/topic/49877/hide-cursor
# Select desired image to be displayed
pixmap = QPixmap('test.png')
# Splash screen approach
# https://doc.qt.io/qtforpython/PySide2/QtWidgets/QSplashScreen.html?highlight=windowflags
splash = QSplashScreen(pixmap) # Set the splash screen to desired image
splash.show() # Show the splash screen
splash.windowHandle().setScreen(s) # Set splash screen to secondary monitor https://stackoverflow.com/a/30597458/4988010
splash.showFullScreen() # Show in splash screen in full screen mode
# # Qlabel apporach
# l = QLabel()
# l.setPixmap(pixmap)
# l.move(1920,0)
# l.show()
# l.windowHandle().setScreen(s) # https://stackoverflow.com/a/30597458/4988010
# l.showFullScreen()
time.sleep(0.5)
end_time = time.time()
print('Execution time: ', end_time-start_time )
sys.exit(app.exec_())
The code below is one possible solution to my question. My solution assumes that Qt is only used to display the images in full-screen and not for the remaining logic. Therefore, I had to run the QT app in a secondary thread. This is because the moment I run the function app.exec_(), Qt will continuously run an event loop thus blocking the rest of my Python logic which does NOT rely on Qt. It is my understanding running QApplication outside of a main thread is not recommended, therefore I would welcome a more experienced user to post a better approach.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QObject, pyqtSignal
import sys
import time
import threading
def main():
print('Step 1')
print(' Some logic here without QT')
print('Step 2')
print(' Launch QT app to run in background')
myapp = myImageDisplayApp()
print('Step 3')
print(' Continue some logic while QT running in background')
time.sleep(2)
print('Step 4')
print(' Update the displayed image in the QT app running in background')
myapp.emit_image_update('qt_test_static_1.png')
time.sleep(2)
print('Step 5')
print(' Update displayed image again')
myapp.emit_image_update('qt_test_static_2.png')
time.sleep(2)
class myImageDisplayApp (QObject):
# Define the custom signal
# https://www.riverbankcomputing.com/static/Docs/PyQt5/signals_slots.html#the-pyqtslot-decorator
signal_update_image = pyqtSignal(str)
def __init__ (self):
super().__init__()
# Setup the seperate thread
# https://stackoverflow.com/a/37694109/4988010
self.thread = threading.Thread(target=self.run_app_widget_in_background)
self.thread.daemon = True
self.thread.start()
def run_app_widget_in_background(self):
self.app = QApplication(sys.argv)
self.my_bg_qt_app = qtAppWidget(main_thread_object=self)
self.app.exec_()
def emit_image_update(self, pattern_file=None):
print('emit_image_update signal')
self.signal_update_image.emit(pattern_file)
class qtAppWidget (QLabel):
def __init__ (self, main_thread_object):
super().__init__()
# Connect the singal to slot
main_thread_object.signal_update_image.connect(self.updateImage)
self.setupGUI()
def setupGUI(self):
self.app = QApplication.instance()
# Get avaliable screens/monitors
# https://doc.qt.io/qt-5/qscreen.html
# Get info on selected screen
self.selected_screen = 0 # Select the desired monitor/screen
self.screens_available = self.app.screens()
self.screen = self.screens_available[self.selected_screen]
self.screen_width = self.screen.size().width()
self.screen_height = self.screen.size().height()
# Create a black image for init
self.pixmap = QPixmap(self.screen_width, self.screen_height)
self.pixmap.fill(QColor('black'))
# Create QLabel object
self.app_widget = QLabel()
# Varioius flags that can be applied to make displayed window frameless, fullscreen, etc...
# https://doc.qt.io/qt-5/qt.html#WindowType-enum
# https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum
self.app_widget.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowDoesNotAcceptFocus | Qt.WindowStaysOnTopHint)
# Hide mouse cursor
self.app_widget.setCursor(Qt.BlankCursor)
self.app_widget.setGeometry(0, 0, self.screen_width, self.screen_height) # Set the size of Qlabel to size of the screen
self.app_widget.setWindowTitle('myImageDisplayApp')
self.app_widget.setAlignment(Qt.AlignLeft | Qt.AlignTop) #https://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum
self.app_widget.setPixmap(self.pixmap)
self.app_widget.show()
# Set the screen on which widget is on
self.app_widget.windowHandle().setScreen(self.screen)
# Make full screen
self.app_widget.showFullScreen()
def updateImage(self, pattern_file=None):
print('Pattern file given: ', pattern_file)
self.app_widget.clear() # Clear all existing content of the QLabel
self.pixmap = QPixmap(pattern_file) # Update pixmap with desired image
self.app_widget.setPixmap(self.pixmap) # Show desired image on Qlabel
if __name__ == "__main__":
main()
I would also like to thank #ekhumoro for pointing me to QWidget attributes/flags.
You should not run the GUI in other than the main thread since Qt does not guarantee that it works correctly as indicated by the docs. Instead of executing the GUI in another thread, you must execute the other heavy tasks in another thread.
You have to change your approach to classical sequential logic but you must use event-oriented programming where actions are taken before an event, in the case of Qt through signals.
Considering the above, the solution is:
import sys
import time
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, Qt, QThread, QTimer
from PyQt5.QtGui import QColor, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
class TaskManager(QObject):
task3Finished = pyqtSignal()
task4Finished = pyqtSignal()
#pyqtSlot()
def task3(self):
print("Step 3")
print(" Continue some logic while QT running in background")
time.sleep(2)
self.task3Finished.emit()
#pyqtSlot()
def task4(self):
print("Step 4")
print(" Update the displayed image in the QT app running in background")
time.sleep(2)
self.task4Finished.emit()
class qtAppWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setupGUI()
def setupGUI(self):
self.app = QApplication.instance()
# Get avaliable screens/monitors
# https://doc.qt.io/qt-5/qscreen.html
# Get info on selected screen
self.selected_screen = 0 # Select the desired monitor/screen
self.screens_available = self.app.screens()
self.screen = self.screens_available[self.selected_screen]
self.screen_width = self.screen.size().width()
self.screen_height = self.screen.size().height()
# Create a black image for init
self.pixmap = QPixmap(self.screen_width, self.screen_height)
self.pixmap.fill(QColor("black"))
# Create QLabel object
self.app_widget = QLabel()
# Varioius flags that can be applied to make displayed window frameless, fullscreen, etc...
# https://doc.qt.io/qt-5/qt.html#WindowType-enum
# https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum
self.app_widget.setWindowFlags(
Qt.FramelessWindowHint
| Qt.WindowDoesNotAcceptFocus
| Qt.WindowStaysOnTopHint
)
# Hide mouse cursor
self.app_widget.setCursor(Qt.BlankCursor)
self.app_widget.setGeometry(
0, 0, self.screen_width, self.screen_height
) # Set the size of Qlabel to size of the screen
self.app_widget.setWindowTitle("myImageDisplayApp")
self.app_widget.setAlignment(
Qt.AlignLeft | Qt.AlignTop
) # https://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum
self.app_widget.setPixmap(self.pixmap)
self.app_widget.show()
# Set the screen on which widget is on
self.app_widget.windowHandle().setScreen(self.screen)
# Make full screen
self.app_widget.show()
#pyqtSlot()
def on_task3_finished(self):
pixmap = QPixmap("qt_test_static_1.png")
self.app_widget.setPixmap(pixmap)
#pyqtSlot()
def on_task4_finished(self):
pixmap = QPixmap("qt_test_static_2.png")
self.app_widget.setPixmap(pixmap)
# quit application after to 2 secons
QTimer.singleShot(2 * 1000, QApplication.quit)
def main(args):
print("Step 1")
print(" Some logic here without QT")
print("Step 2")
print(" Launch QT app to run")
app = QApplication(args)
myapp = qtAppWidget()
thread = QThread()
thread.start()
manager = TaskManager()
# move the QObject to the other thread
manager.moveToThread(thread)
manager.task3Finished.connect(myapp.on_task3_finished)
manager.task3Finished.connect(manager.task4)
manager.task4Finished.connect(myapp.on_task4_finished)
# start task
QTimer.singleShot(0, manager.task3)
ret = app.exec_()
thread.quit()
thread.wait()
del thread, app
return ret
if __name__ == "__main__":
sys.exit(main(sys.argv))
I am trying to move from matplotlib to plotting with pyqtgraph because of its touted capabilities to render and save images faster. In my attempts to do this on a cluster with multiprocessors, I run into the following trouble:
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to
'/tmp/runtime-user' qt.qpa.screen: QXcbConnection: Could not connect
to display Could not connect to any X display.
How do I obviate displaying a plot, and save it directly to file? Here's my attempt at the code:
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import pyqtgraph.exporters
#app = QtGui.QApplication([])
#view = pg.GraphicsView()
l = pg.GraphicsWindow()
#view.setCentralItem(l)
#view.show()
#view.setWindowTitle('GraphicsLayout')
#view.resize(1000,1600)
def plotlayout(lines):
p_res={}
p_data={}
for rows in rows:
p_res[row]={}
p_data[row]={}
for col in cols:
l2=l.addLayout()
p_res[row][col]=l2.addPlot()
p_res[row][col].hideAxis('bottom')
l2.nextRow()
p_data[row][col]=l2.addPlot()
l.nextColumn()
l.nextRow()
return p_res, p_data
pl = plotlayout(lines)
pl[0].plot([1,3,5,9,7,8],[2,3,3,5,6,8])
pl[1].plot([1,3,5,9,7,8],[2,22,3,45,6,8])
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
QtGui.QApplication.exit()
QtGui.QApplication.quit()
QtGui.QApplication.quitOnLastWindowClosed()
QtGui.QApplication.closeAllWindows()
exporter = pg.exporters.ImageExporter(l.scene())
exporter.export('fits.ps')
I have tested this on my personal laptop and it works fine.
If you want to run a GUI without using desktop environment/window manager a possible solution is to use Xvfb.
i am working with CGKIT in my project, i came across a situation that i must use only render.py file and change the background of the picture.
i know how to use viewer.py and change the picture but implementing that i.e. changing background in GLOBALS it is not changing the background as per render.py
so please any one help me out
# Use the previously rendered texture map
from time import sleep
import Image
from cgkit.cgtypes import vec3, mat4
from cgkit.cmds import load, worldObject
from cgkit.glmaterial import GLMaterial, GLTexture
from cgkit.scene import getScene
from cgkit.sceneglobals import Globals
from OpenGL.GL import glReadPixels
from pyglet.gl import GL_RGBA, GL_UNSIGNED_BYTE, GL_DECAL
Globals(
resolution=(512,512),
up = (0,1,0),
background=(1,1,1,1),
output = "kishoreGoodBoy.png",
)
load("singleSofa.obj")
model = worldObject("small_sofa_dark_grey")
mat = GLMaterial(
diffuse = (0,1,0),
texture = GLTexture(
"final1.png",
mode = GL_DECAL,
transform = mat4().scaling(vec3(1,-1,1))
)
)
model.setMaterial(mat)
as per code i had textre file and so that i am directing it to a .PNG file and it is saving. but it is coming with a black background so i need to change the background clour