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)
defPlayVideo(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))
'''
defVideoPlayPause(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()