我已经使用 pycapture2 和 PyQt5 制作了一个视频捕获程序。它似乎有效,但控制台显示错误:
QObject::killTimer: Timers cannot be stopped from another thread
QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
我通过使用 QTimer 解决了这个问题,但速度很慢(2 帧/秒)。
我想使用QThread,如何解决这个问题?
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread
import cv2
import PyCapture2
import numpy as np
class Ui_MainWindow(object):
def __init__(self, parent=None):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(600, 400)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(150, 20, 400, 300))
self.graphicsView.setObjectName("graphicsView")
MainWindow.setCentralWidget(self.centralwidget)
self.Thread = CapThread()
self.Thread.start()
class CapThread(QThread):
def __init__(self):
QThread.__init__(self)
def run(QMainWindow):
bus = PyCapture2.BusManager()
uid = bus.getCameraFromIndex(0)
c = PyCapture2.Camera()
c.connect(uid)
while True:
c.startCapture()
img = c.retrieveBuffer()
c.stopCapture()
cv_img1 = np.array(img.getData(), dtype="uint8").reshape((img.getRows(), img.getCols()));
cv_img = cv2.cvtColor(cv_img1, cv2.COLOR_BAYER_BG2BGR)
cv_img = cv2.resize(cv_img,(380,270))
height, width, dim = cv_img.shape
bytesPerLine = dim * width
image = QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap.fromImage(image))
scene = QtWidgets.QGraphicsScene()
scene.addItem(item)
ui.graphicsView.setScene(scene)
白衣染霜花
相关分类