加载中…
个人资料
Benson
Benson
  • 博客等级:
  • 博客积分:0
  • 博客访问:99,224
  • 关注人气:22
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

Python - pyqt 和 opencv

(2013-01-15 17:10:58)
标签:

pyqt

opencv

it

分类: python

    pyqt 和 opencv学习  

    在网上找资料, 好多pyqt和opencv的东东, 学了一个星期的python, 找些好玩的, 才qt原来还支持python

才知道qt工具里面有python一部分. 至于opencv, 在官网上看见了python版本的应用, 之前做人脸识别的时候了解过. 最近发现一个前辈的博客写了一份pyqt+opencv的视频播放和录制的代码.(http://my.oschina.net/hebianxizao/blog/99418?from=20130103) (代码原创在这里)

    照着写了一个, 发现一个问题cv.iplimage没有imageData属性的? 如果有人知道告诉我一下,非常感谢.或则知道iplimage 转换为 QImage的方法告诉, 再次感谢. 我知道以下二种方法: frame 是 iplimage类型

        image = QImage(frame.imageData, frame.width, frame.height, QImage.Format_RGB888)
        image = image.rgbSwapped()
        self.pictureLabel.setPixmap(QPixmap.fromImage(image))

   运行时, 报错: cv.iplimage 没有 imageData属性. 以前用C++写的时候可以, 现在python不行.

        frame = cv.adaptors.Ipl2PIL(frame)
        frame = frame.convert('RGB').tostring('jpeg', 'RGB')
        self.image.loadFromData(QByteArray(frame))
        self.pictureLabel.setPixmap(QPixmap.fromImage(self.image))

   运行时, 报错: 找不到cv.adaptors. 这是链接博客里面那位前辈的办法.(iplimage->PIL)

 

   没办法, 使用一个最笨的方法了:

        cv.SaveImage("2.jpg", frame)
        self.image.load("2.jpg")
        self.pictureLabel.setPixmap(QPixmap.fromImage(self.image))

   把frame保存成文件2.jpg再用qt的image.load读入, 接着显示在label中. (实现结果, 效率不高T.T)

 

代码:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import cv2,cv as cv
import time

 

MPEG1VIDEO = 0x314D4950
camera = cv.CreateCameraCapture(0) # create camera

class MainWindow(QWidget):
    def __init__(self, parent = None):
        QWidget.__init__(self)
        self.resize(300,300)
        self.setWindowTitle('Video')
        self.status = 0 # 0: init 1:play vedio 2:camera
        self.image = QImage()

        #set property for video file
        self.videoWriter = cv.CreateVideoWriter("test.mpg",
                                                cv.CV_FOURCC('m','p','g','1'),
                                                25,
                                                (100,100),
                                                1)
        #set video file
        self.playCapture = cv.CreateFileCapture("test.avi")

        #init button
        self.captureButton = QPushButton("capture")
        self.playButton = QPushButton("play")
        exitButton = QPushButton("exit")

        # layout
        vbox = QVBoxLayout() #vertical layout
        vbox.addWidget(self.captureButton)
        vbox.addWidget(self.playButton)
        vbox.addWidget(exitButton)

        self.pictureLabel = QLabel("picture")
        hbox = QHBoxLayout()
        hbox.addLayout(vbox)
        hbox.addStretch(1)
        hbox.addWidget(self.pictureLabel)

        self.setLayout(hbox)
       
        # init
        if self.image.load("1.jpg"):
            self.pictureLabel.setPixmap(QPixmap.fromImage(self.image))

        # set timeout
        self.timer = Timer() # save video
        self.playTimer = Timer("updatePlay()")

        # signal-slot
        self.connect(self.timer, SIGNAL("updateTime()"), self.CaptureVGA)
        self.connect(self.captureButton, SIGNAL("clicked()"), self.PauseBegin)
        self.connect(self.playTimer, SIGNAL("updatePlay()"), self.PlayVideo)
        self.connect(exitButton, SIGNAL("clicked()"), app, SLOT("quit()"))
        self.connect(self.playButton, SIGNAL("clicked()"), self.VideoPlayPause)

 

    def PlayVideo(self):
        frame = cv.QueryFrame(self.playCapture)
        cv.SaveImage("2.jpg", frame)
        self.image.load("2.jpg")
        self.pictureLabel.setPixmap(QPixmap.fromImage(self.image))
        '''
        image = QImage(frame.imageData, frame.width, frame.height, QImage.Format_RGB888)
        image = image.rgbSwapped()
        self.pictureLabel.setPixmap(QPixmap.fromImage(image))
        '''
        '''
        frame = cv.adaptors.Ipl2PIL(frame)
        frame = frame.convert('RGB').tostring('jpeg', 'RGB')
        self.image.loadFromData(QByteArray(frame))
        self.pictureLabel.setPixmap(QPixmap.fromImage(self.image))
        '''
       
    def VideoPlayPause(self):
        self.status, playStr, captureStr = ((1,'pause','capture'),
                                            (0,'play','capture'),
                                            (1,'pause','capture'))[self.status]
        self.playButton.setText(playStr)
        self.captureButton.setText(captureStr)
        if self.status is 1: #play video
            self.timer.stop()
            self.playTimer.start()
        else:
            self.playTimer.stop()

 

    def PauseBegin(self):
        self.status, playStr, captureStr = ((2,'play','pause'),
                                            (2,'play','pause'),
                                            (0,'play','capture'))[self.status]
        self.captureButton.setText(captureStr)
        self.playButton.setText(playStr)
        print self.status, playStr, captureStr
        if self.status is 2: # save and play video
            self.timer.start()
            self.playTimer.stop()
        else:
            self.timer.stop()

 

    def CaptureVGA(self):
        frame = cv.QueryFrame(camera)
        cv.WriteFrame(self.videoWriter, frame)
        cv.SaveImage("2.jpg", frame)
        self.image.load("2.jpg")
        self.pictureLabel.setPixmap(QPixmap.fromImage(self.image))
        '''
        image = QImage(frame.imageData, frame.width, frame.height, QImage.Format_RGB888)
        image = image.rgbSwapped()
        self.pictureLabel.setPixmap(QPixmap.fromImage(image))
        '''
        '''
        frame = cv.adaptors.Ipl2PIL(frame)
        frame = frame.convert('RGB').tostring('jpeg', 'RGB')
        self.image.loadFromData(QByteArray(frame))
        self.pictureLabel.setPixmap(QPixmap.fromImage(self.image))
        '''

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):
        with 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__":
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

 

截图:

Python <wbr>- <wbr>pyqt <wbr>和 <wbr>opencv

Python <wbr>- <wbr>pyqt <wbr>和 <wbr>opencv

Python <wbr>- <wbr>pyqt <wbr>和 <wbr>opencv

    Any way, 一个星期的python学习, 还是挺好玩的, 写代码时候'顾名思义'的感觉很好玩, 好了放假那么久玩够了, 从现在开始继续奋战ACM, 回归刷题的日子. 今年的省赛继续努力. 对得起自己. 加油加油!!!

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有