Implement video player with PYSIDE/PYQT and use Pyinstaller to pack and publish

Source: Internet
Author: User
Tags mutex pack

The first time with Pyqt/pyside, encountered a lot of problems. All the problems encountered in the video release this piece encountered, but finally succeeded, here to summarize the problems encountered.

Read the frame in the video, show it by frame, the format must be qimage, but the format of the frame read with CV2 is NumPy array, it must be converted to Qimage, so try:

1. Using pyqtgraph:

The NumPy type of frame through pyqtgraph conversion to qimage, and then through the Setpixmap display, the program in idle execution is correct, but in the release, encountered a lot of disgusting problems, because it is to help others do, I do not have so much time to solve the problem, Then a second approach was adopted.

                Frame,stat=pyqtgraph.makeargb (frame, lut=none, Levels=none, Scale=none, usergba=true)
                frame= Pyqtgraph.makeqimage (frame, alpha=none, Copy=true, Transpose=false) #
                
                Frame=qpixmap.fromimage (frame)
                Self.pictureLabel.setPixmap (frame)
2. Use PiL to save the image, and then read it through Qimage, and convert it to qimage format directly:

But with the new problems encountered, through the CV2. Videocapture ("001.avi") read the video file, through the conversion below, the color channel incredibly chaotic, I tried to use Numpy.roll () will channel flip, but the effect is not ideal, and slowed down the speed, really helpless ~, so there is a third way to try

                Frame=image.fromarray (frame)
                frame.save (' 2.jpg ') 
                self.image.load ("2.jpg")
                Self.pictureLabel.setPixmap (Qpixmap.fromimage (self.image))
3. Use Cv2.cv.fromarray () to convert the read NumPy frame, then save and then read ~

This success, through the Pyinstaller packaging after finally can be displayed normally.

                Frame=cv2.cv.fromarray (frame)
                cv2.cv.SaveImage (' 2.jpg ', frame)
                self.image.load ("2.jpg")
                Self.pictureLabel.setPixmap (Qpixmap.fromimage (self.image))

The above three methods do not have errors in idle, that is, when packaging the release encountered an error. And when it comes to containing pyqtgraph libraries, there are a lot of difficulties in packaging the release process, which eventually forced me to abandon the use of pyqtgraph.

In the use of Pyinstaller packaging, but also encountered some problems, this article is using the Pyinstaller 2.1 version.

First CD into Pyinstaller, Python setup.py install, then execute Python pyinstaller-f-W yourpy.py (-F is packaged as a file,-W is Windows edition, no small black window).

It is recommended that you perform a Python setup.py install before each package (so you will avoid a lot of trouble)

Put a picture on the background of the video window, set the background, if he did not show, in C:\Python27\Lib\site-packages\PyQt4\plugins find imageformats this folder, and EXE put in a piece, on the line, All right, paste the program:

The program has three buttons, each button press down, will play two consecutive videos, and then click Will pause. The final interface is as follows:


#-*-Coding:utf-8-*-from Pyside.qtgui Import * from pyside.qtcore Import * Import pyside import sys import CV2 impo                RT time from PIL import Image import NumPy class MainWindow (PySide.QtGui.QWidget): # # video_list=[\ # # R "D:\data\ single well CH4 diffusion video. mpeg", \ # # R "D:\data\ single well H2S diffusion video. mpeg", \ # # R "D:\data\ gas station CH4 diffusion video                . mpeg ", \ # # R" D:\data\ Gas collector station H2S diffusion video. mpeg ", \ # # R" D:\data\ gas pipeline CH4 diffused video. mpeg ", \ # # R "D:\data\ gas transmission line H2S diffusion video. mpeg", \ # #] def __init__ (self,parent = None): PySide.QtGui.QWidget. __init__ (self) self.setwindowtitle (U ' natural gas leak fluent numerical simulation video demo ') self.status_1= 0 # 0:init 1:play vedio 2:camer A self.status_2= 0 self.status_3= 0 # # Self.video_1_0=self.video_list[0] # # SELF.VI        DEO_1_1=SELF.VIDEO_LIST[1] # # Self.video_2_0=self.video_list[2] # # Self.video_2_1=self.video_list[3] # # Self.video_3_0=self.vIDEO_LIST[4] # # Self.video_3_1=self.video_list[5] # # SELF.VIDEO_1_0=R "D:\data\ single well CH4 diffuse video. MPEG" # # SEL F.video_1_1=r "D:\data\ single well H2S diffusion video. mpeg" # # SELF.VIDEO_2_0=R "D:\data\ gas station CH4 diffusion video. Mpeg" # # SELF.VIDEO_2_1=R "D:\da Ta\ Gas collector station H2S diffusion video. mpeg "# # SELF.VIDEO_3_0=R" D:\data\ gas pipeline CH4 diffusion video. Mpeg "# # SELF.VIDEO_3_1=R" D:\data\ gas pipeline H2S diffusion video. MP Eg "# # # Self.video_1_0=r" D:\data\001.mpeg "Self.video_1_1=r" D:\data\002.mpeg "Self.video_2_0=r" D: \data\003.mpeg "Self.video_2_1=r" D:\data\004.mpeg "Self.video_3_0=r" D:\data\005.mpeg "Self.video_3 _1=r "D:\data\006.mpeg" self.next_video_flag=false self.image = Qim Age () #set video File Self.playcapture_1_0 = Cv2. Videocapture (self.video_1_0) self.playcapture_1_1 = Cv2. Videocapture (self.video_1_1) self.playcapture_2_0 = Cv2. Videocapture (self.video_2_0) self.playcapture_2_1 = Cv2. VideocaptuRe (self.video_2_1) Self.playcapture_3_0 = Cv2. Videocapture (self.video_3_0) self.playcapture_3_1 = Cv2. Videocapture (self.video_3_1) self.playbutton_1 = Qpushbutton (u "conveying line") self.playbutton_2 = Qpushbutton (U "single Well ") Self.playbutton_3 = Qpushbutton (u" gas Collector station ") VBox = Qvboxlayout () #vertical layout vbox.add Widget (self.playbutton_1) vbox.addwidget (self.playbutton_2) vbox.addwidget (self.playbutton_3) se Lf.picturelabel = Qlabel ("Picture") Hbox=qhboxlayout () hbox.addstretch (0) Hbox.addlayout (
           

        VBox) Hbox.addwidget (Self.picturelabel) self.setlayout (Hbox) Self.set_background () # set Timeout self.timer_1 = Timer () # Save Video self.timer_2 = Timer () # Save video self.t Imer_3 = Timer () # Save Video self.playtimer_1 = Timer ("Updateplay ()") self.playtimer_2 = Timer ("Updatepla Y () ") Self.playtimer_3 = Timer ("Updateplay ()") Self.connect (self.playtimer_1, SIGNAL ("Updateplay ()"), self. Playvideo_1) Self.connect (self.playtimer_2, SIGNAL ("Updateplay ()"), self. playvideo_2) Self.connect (self.playtimer_3, SIGNAL ("Updateplay ()"), self. Playvideo_3) #self. Connect (Exitbutton, SIGNAL ("clicked ()"), app, SLOT ("Quit ()")) Self.connect (sel F.playbutton_1, SIGNAL ("clicked ()"), self. Videoplaypause_1) Self.connect (self.playbutton_2, SIGNAL ("clicked ()"), self. videoplaypause_2) Self.connect (Self.playbutton_3, SIGNAL ("clicked ()"), self. VIDEOPLAYPAUSE_3) def set_background (self): Self.image.load (r "d:/data/background.jpg") self.p  Icturelabel.setpixmap (Qpixmap.fromimage (self.image)) def playvideo_1 (self): if Self.next_video_flag==false
            
            and Self.status_1==1 and self.status_2!=1 and self.status_3!=1:stat,frame = Self.playCapture_1_0.read () If Stat==false: Self.next_video_flag=true Else: #print ' frame ' FRAME=CV2.CV
                . FromArray (frame) cv2.cv.SaveImage (' 2.jpg ', frame) self.image.load ("2.jpg") Self.pictureLabel.setPixmap (Qpixmap.fromimage (self.image)) if Self.next_video_flag==true and self.status_1==1 an 
                D self.status_2!=1 and self.status_3!=1:stat,frame = Self.playCapture_1_1.read () if Stat==false:
                Self.next_video_flag=false self.playTimer_1.stop () self.timer_1.stop () Self.set_background () Self.status_1, playstr= (1,u "conveying line") Self.playbutton_1.s Ettext (playstr) self.playcapture_1_0 = Cv2. Videocapture (SELF.VIDEO_1_0) self.status_1=0 Else:frame=cv2.cv.fromarray (fr AME) cv2.cv.SaveImage (' 2.jpg ', frame) self.image.load ("2. jpg ") Self.pictureLabel.setPixmap (Qpixmap.fromimage (self.image)) def playvideo_2 (self): If Self.next_video_flag==false and Self.status_2==1 and Self.status_1!=1 and Self.status_3!=1:stat,f  Rame = Self.playCapture_2_0.read () if stat==false:self.next_video_flag=true else : Frame=cv2.cv.fromarray (frame) cv2.cv.SaveImage (' 2.jpg ', frame) self.imag E.load ("2.jpg") Self.pictureLabel.setPixmap (Qpixmap.fromimage (self.image)) if SEL F.next_video_flag==true and Self.status_2==1 and self.status_1!=1 and self.status_3!=1:stat,frame = Self.pla Ycapture_2_1.read () if Stat==false:self.next_video_flag=false self.playtimer_ 2.stop () Self.timer_2.stop () Self.set_background () self.status_2, playstr=
              (1,u "single Well")  Self.playButton_2.setText (playstr) self.playcapture_2_0 = Cv2. Videocapture (SELF.VIDEO_2_0) self.status_2=0 Else:frame=cv2.cv.fromarray (fr AME) cv2.cv.SaveImage (' 2.jpg ', frame) self.image.load ("2.jpg") self.picture Label.setpixmap (Qpixmap.fromimage (self.image)) def playvideo_3 (self): if Self.next_video_ Flag==false and Self.status_3==1 and self.status_2!=1 and self.status_1!=1:stat,frame = Self.playcapture_3_0 . Read () if Stat==false:self.next_video_flag=true ELSE:FRAME=CV
                2.cv.fromarray (frame) cv2.cv.SaveImage (' 2.jpg ', frame) self.image.load ("2.jpg")  Self.pictureLabel.setPixmap (Qpixmap.fromimage (self.image)) if Self.next_video_flag==true and
    Self.status_3==1 and Self.status_2!=1 and self.status_1!=1:        Stat,frame = Self.playCapture_3_1.read () if Stat==false:self.next_video_flag=false
                Self.playTimer_3.stop () Self.timer_3.stop () Self.set_background () Self.status_3, playstr= (1,u "Gas collector Station") Self.playButton_3.setText (PLAYSTR) Self.playcaptur E_3_0 = Cv2. Videocapture (SELF.VIDEO_3_0) self.status_3=0 Else:frame=cv2.cv.fromarray (fr AME) cv2.cv.SaveImage (' 2.jpg ', frame) self.image.load ("2.jpg") self.picture Label.setpixmap (Qpixmap.fromimage (self.image)) def videoplaypause_1 (self): SELF.PLAYCAPTU Re_2_0 = Cv2. Videocapture (self.video_2_0) self.playcapture_2_1 = Cv2. Videocapture (self.video_2_1) self.playcapture_3_0 = Cv2. Videocapture (self.video_3_0) self.playcapture_3_1 = Cv2. Videocapture (self.video_3_1) Self.status_1, Playstr = ((1,u ' pause '), (0,u "conveying line"), (1,u ' pause '))
            [Self.status_1] Self.playButton_1.setText (PLAYSTR) If Self.status_1 is 1: #play video Self.timer_1.stop () Self.playTimer_1.start () else:self.playTimer_1.stop () def Vid Eoplaypause_2 (self): Self.playcapture_1_0 = Cv2. Videocapture (self.video_1_0) self.playcapture_1_1 = Cv2. Videocapture (self.video_1_1) self.playcapture_3_0 = Cv2. Videocapture (self.video_3_0) self.playcapture_3_1 = Cv2. Videocapture (Self.video_3_1) self.status_2, Playstr = ((1,u ' pause '), (0,u
        
        "Single Well"), (1,u ' pause ')) [self.status_2] Self.playButton_2.setText (PLAYSTR)
        If Self.status_2 is 1: #play video self.timer_2.stop () Self.playTimer_2.start () ElSe:self.playTimer_2.stop () def videoplaypause_3 (self): self.playcapture_1_ 0 = Cv2. Videocapture (self.video_1_0) self.playcapture_1_1 = Cv2. Videocapture (self.video_1_1) self.playcapture_2_0 = Cv2. Videocapture (self.video_2_0) self.playcapture_2_1 = Cv2.
                               Videocapture (self.video_2_1) Self.status_3, playstr= ((1,u ' pause '), (0,u "Gas collector Station"), (1,u ' pause '))
            [Self.status_3] Self.playButton_3.setText (PLAYSTR) If Self.status_3 is 1: #play video Self.timer_3.stop () Self.playTimer_3.start () Else:self.playTimer_3.stop () class timer
        (qthread): Def __init__ (self, signal = "updatetime ()", parent = None): Super (Timer, self). __init__ (parent) self.stoped = False Self.signal = Signal Self.mutex = Qmutex () def run (self): WI
   Th Qmutexlocker (Self.mutex):         self.stoped = False while True:if Self.stoped:return Self.emit (SIGNAL (self.signal)) Time.sleep (0.04) def stop (self): with Qmutexlocker (
            Self.mutex): self.stoped = True def isstoped (self): with Qmutexlocker (Self.mutex): return self.stoped If __name__ = = "__main__": Import pyside app = Qapplication (sys.argv) main = Mai
 Nwindow () main.show () Sys.exit (App.exec_ ())




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.