Dear all, I am new in Python and maybe it seems that this question has been answered in other ways before but I could not figure it out after 2 days. I have a qtgraph plot and I want to show it in a QLabel, Qwidget or Graphicsview in a gui that I made in Qtdesigner but I can not find a direct way that shows that graph. I am streaming a video from a camera and I could easily show it in my GUI but I am struggling with this one. Thank you in advance for your help and comments.
the problem is in showing -- You received this message because you are subscribed to the Google Groups "pyqtgraph" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/2a5cb0ac-c498-476c-8349-af25d0809967%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
VideoStream.ui
Description: Binary data
#These are the libraries which have been used in the app.
import sys
import numpy as np
import cv2
import pyqtgraph as pg
import datetime
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication , QMainWindow
from PyQt5.uic import loadUi
from PyQt5 import QtGui
#this is the main class for this app and get the visual GUI from QtDesigner
class VideoStream(QMainWindow):
def __init__(self):
super(VideoStream , self).__init__()
loadUi('VideoStream.ui',self)
self.image= None
#self.l_values is the place that we collect the intensity values for each frame
self.l_values = []
#This is a button to set the geometry of our mask for the specific zone of lightness
self.Apply_Button.clicked.connect(self.Geometry)
#this button save the maximum values of the specific zone in a text file
#self.Save_Button.clicked.connect(self.save_data)
self.startButton.clicked.connect(self.start_webcam)
self.stopButton.clicked.connect(self.stop_webcam)
#these two variables are the height and width of our camera.
#With different cameras this part should be changed.
self.cameraHeight=480
self.cameraWidth=640
#Plot a seperate window for saving the figure of lightness at any moment.
self.mask = np.zeros((self.cameraHeight,self.cameraWidth),np.uint8)
#Initial values of the mask
self.radius=100
self.x1=0
self.y1=0
self.i=""
self.plt = pg.PlotWindow()
self.bufferSize = 1000
self.data = np.zeros(self.bufferSize)
self.curve = self.plt.plot()
self.line = self.plt.addLine(x=0)
self.k=1
self.l = 0
self.n=1
def start_webcam(self):
self.now = datetime.datetime.now()
cap=self.capture=cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FPS, 60)
fps = int(cap.get(5))
print("fps:", fps)
self.timer=QtCore.QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(1)
Ex_date="ex"+str(self.now.year) +str(self.now.month)+str(self.now.day)+str(self.now.hour)+str(self.now.minute)
#
self.f= open(Ex_date+".txt","w+")
#This function get the updated frame and operate the mask on it for the displayImage and displayHist parts
def update_frame(self):
ret,self.image=self.capture.read()
self.mask = np.zeros((self.cameraHeight,self.cameraWidth),np.uint8)
cv2.circle(self.mask,(self.cameraWidth//2+self.x1,self.cameraHeight//2+self.y1), self.radius, 255, -1)
self.image=cv2.circle(self.image,(self.cameraWidth//2+self.x1,self.cameraHeight//2+self.y1), self.radius+1, (0,255,0), thickness=2)
#convert the video stream from a BRG color space to gray scale for make the calculation faster for the program
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
self.displayImage(self.image,1)
#Operate the mask after showing the video stream to collect the lightness in the specific zone
self.image = cv2.bitwise_and(self.image,self.image,mask = self.mask)
self.displayHist(self.image, 1)
#This function stops the video stream
def stop_webcam(self):
self.timer.stop()
self.f.close()
#This function shows the stream in the GUI we created by qtdesigner
def displayImage(self,img,window=1):
qformat=QtGui.QImage.Format_Grayscale8
outImage=QtGui.QImage(img, img.shape[1],img.shape[0],qformat)
self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))
self.imgLabel.setScaledContents(True)
#for the seperate canvas you should click on the move button in that window once
def displayHist(self,img, window=1):
self.avr = int(np.average(self.image)*25)
self.avrage=np.array([self.avr])
if self.l<=self.bufferSize:
self.plt.setRange(xRange=[max(self.l- 100, 0), self.l])
self.data[self.l:self.l+self.n] = self.avrage
self.curve.setData(self.data)
print(self.l)
self.l = (self.l+self.n)
if self.l%100==0:
if self.l>=100:
print("y ",self.k,np.max(self.data[self.l-100:self.l]))
self.k=self.k+1
elif self.l==0:
print("- ",self.k,np.max(self.data[-(100-self.l):]))
else:
print("n ",self.k,max(np.max(self.data[-(100-self.l):]),np.max(self.data[:self.l])))
self.k=self.k+1
self.line.setValue(self.l)
if self.l>self.bufferSize:
self.plt.setRange(xRange=[max(self.bufferSize- 100, 0), self.bufferSize])
for j in range(self.bufferSize-1):
self.data[j]=self.data[j+1]
self.data[-1]=self.avrage
self.curve.setData(self.data)
self.l = (self.l+self.n)
self.line.setValue(self.bufferSize)
##########this is the place that I don't have any Idea to do
#This function set the Geometry for implementing the mask
def Geometry(self):
self.radius = int(self.Radius_edit.toPlainText())
self.x1 = int(self.X_edit.toPlainText())
self.y1 = -int(self.Y_edit.toPlainText())
if __name__=='__main__':
app= QApplication(sys.argv)
window=VideoStream()
window.setWindowTitle('Streaming video of the experiment')
window.show()
sys.exit(app.exec_())
