Can you pass a singleton into a QML register Type?
or rather import a singleton so that this python file can access the continuously changing data from another python file or class instance. For example "from main import myTestClasse" so far the data is not updating to match the singleton myTestClasse and was wondering if there is an extra step that needs to be taken?
From the example listed above with modifications :...
"""
import numpy as np
import threading
#from PySide6 import Qt
import cv2
"""
from PySide6 import QtCore, QtGui, QtQml
from PySide6.QtCore import QObject, Signal, Slot, Property
from main import myTestClasse
def max_rgb_filter(image):
# split the image into its BGR components
(B, G, R) = cv2.split(image)
# find the maximum pixel intensity values for each
# (x, y)-coordinate,, then set all pixel values less
# than M to zero
M = np.maximum(np.maximum(R, G), B)
R[R < M] = 0
G[G < M] = 0
B[B < M] = 0
# merge the channels back together and return the image
return cv2.merge([B, G, R])
gray_color_table = [QtGui.qRgb(i, i, i) for i in range(256)]
class CVCapture(QtCore.QObject):
#once the camera caputre is started from completion of the QML , send signal that the image capture has started
started = Signal()
imageReady = Signal()
indexChanged = Signal()
def __init__(self,parent=None):
super(CVCapture, self).__init__(parent)
self._image = QtGui.QImage()
self._index = 0
#self.image_handler = image_handler
#self.m_videoCapture = cv2.VideoCapture()
self.m_timer = QtCore.QBasicTimer()
#self.m_filters = []
self.m_busy = False
#self.testImage = None
#self.frame = None
@Slot()
@Slot(int)
def start(self, *args):
print('start the image display')
if args:
self.setIndex(args[0])
self.m_timer.start(50, self)
self.started.emit()
@Slot()
def stop(self):
self.m_timer.stop()
def timerEvent(self, e):
#print(f'image handler data is {image_handler.oneSecondCounter}')
if e.timerId() != self.m_timer.timerId(): return
#print('timerEvent Happening')
#grabbedImage = image_handler.thumbnailImage.copy()
#ret, frame = self.m_videoCapture.read()
#ret = False
#print(f'testimage size{testImage.shape}')
if myTestClasse.image is not None:
self.testImage = myTestClasse.image.copy()
#print(f'thumbnail image :{self.testImage}')
#self.frame = self.testImage.copy()
#ret = True
#cv2.imwrite('test3.png',self.testImage)
else:
return
#if not ret:
#print('timerEvent Stopping')
#self.m_timer.stop()
#return
if self.m_busy == False:
#print('start thread show image')
#cv2.imwrite('test4.png',self.testImage)
if self.testImage is not None:
#localTest = self.testImage.copy()
#print('start thread show image2')
#cv2.imwrite('test4to5.png',self.testImage)
#threading.Thread(target=self.process_image, args=(np.copy(self.testImage),)).start()
self.process_image(self.testImage.copy())
@Slot(np.ndarray)
def process_image(self, frame):
#print('process image')
cv2.imwrite('test5.png',frame)
self.m_busy = True
#print(f'flag is{self.m_busy}')
#for f in self.m_filters:
# frame = f.process_image(frame)
image = CVCapture.ToQImage(frame)
#if self._image == image:
# self.m_busy = False
# return
self._image = image
self.m_busy = False
self.setImage()
#QtCore.QMetaObject.invokeMethod(self,"setImage",QtCore.Qt.QueuedConnection)
@staticmethod
def ToQImage(im):
if im is None:
return QtGui.QImage()
if im.dtype == np.uint8:
if len(im.shape) == 2:
qim = QtGui.QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QtGui.QImage.Format_Indexed8)
qim.setColorTable(gray_color_table)
return qim.copy()
elif len(im.shape) == 3:
if im.shape[2] == 3:
w, h, _ = im.shape
rgb_image = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
flip_image = cv2.flip(rgb_image, 1)
qim = QtGui.QImage(flip_image.data, h, w, QtGui.QImage.Format_RGB888)
return qim.copy()
return QtGui.QImage()
def getImage(self):
return self._image
@Slot()
def setImage(self):
self.imageReady.emit()
def index(self):
return self._index
def setIndex(self, index):
if self._index == index: return
self._index = index
self.indexChanged.emit()
image = Property(QtGui.QImage, fget=getImage, notify=imageReady)
index = Property(int, fget=index, fset=setIndex, notify=indexChanged)
"""