使用opencv调用摄像头并拍照,遇到分辨率的问题--python

 

# -*- coding: utf-8 -*-

 

# Form implementation generated from reading ui file 'myWindows.ui'

#

# Created by: PyQt5 UI code generator 5.9.2

#

# WARNING! All changes made in this file will be lost!

 

from PyQt5 import QtCore, QtGui, QtWidgets

 

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")

        MainWindow.resize(960, 720)

        self.centralwidget = QtWidgets.QWidget(MainWindow)

        self.centralwidget.setObjectName("centralwidget")

        self.label = QtWidgets.QLabel(self.centralwidget)

        self.label.setGeometry(QtCore.QRect(0, 0, 960, 720))

        self.label.setMidLineWidth(0)

        self.label.setText("")

        self.label.setObjectName("label")

        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QtWidgets.QMenuBar(MainWindow)

        self.menubar.setGeometry(QtCore.QRect(0, 0, 960, 30))

        self.menubar.setObjectName("menubar")

        self.menuFile = QtWidgets.QMenu(self.menubar)

        self.menuFile.setObjectName("menuFile")

        MainWindow.setMenuBar(self.menubar)

        self.statusbar = QtWidgets.QStatusBar(MainWindow)

        self.statusbar.setObjectName("statusbar")

        MainWindow.setStatusBar(self.statusbar)

        self.actionstart = QtWidgets.QAction(MainWindow)

        self.actionstart.setObjectName("actionstart")

        self.actionquit = QtWidgets.QAction(MainWindow)

        self.actionquit.setObjectName("actionquit")

        self.actionsnap = QtWidgets.QAction(MainWindow)

        self.actionsnap.setObjectName("actionsnap")

        self.menuFile.addAction(self.actionstart)

        self.menuFile.addAction(self.actionsnap)

        self.menuFile.addSeparator()

        self.menuFile.addAction(self.actionquit)

        self.menubar.addAction(self.menuFile.menuAction())

 

        self.retranslateUi(MainWindow)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)

 

    def retranslateUi(self, MainWindow):

        _translate = QtCore.QCoreApplication.translate

        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

        self.menuFile.setTitle(_translate("MainWindow", "File"))

        self.actionstart.setText(_translate("MainWindow", "start"))

        self.actionquit.setText(_translate("MainWindow", "quit"))

        self.actionsnap.setText(_translate("MainWindow", "snap"))

 

 



 

 

from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.Qt import QRegExp, QRegExpValidator

from myWindows import Ui_MainWindow

from PyQt5.QtCore import pyqtSlot

import cv2

 

import sys

import time

 

class MyWindow(Ui_MainWindow,QtWidgets.QMainWindow):

    def __init__(self, parent=None):

        super(MyWindow, self).__init__(parent)

        self.setupUi(self)

        print(self.geometry())

        self.i=0;

    def stopVideo(self):

        if self.cap.isOpened():

 

            self.cap.release()

        if self.timer_camera.isActive():

            self.timer_camera.stop()

 

    def startVideo(self,vwidth,vheight):

 

        self.timer_camera = QtCore.QTimer()

        self.cap = cv2.VideoCapture()

        self.vwidth=vwidth

        self.vheight=vheight

        print(str(self.vwidth) + " " + str(self.vheight))

        self.CAM_NUM = 1

        self.timer_camera.timeout.connect(self.show_camera)

        flag = self.cap.open(self.CAM_NUM)

        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.vwidth)

        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.vheight)

 

 

        if flag == False:

            msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确", buttons=QtWidgets.QMessageBox.Ok,defaultButton=QtWidgets.QMessageBox.Ok)

            # if msg==QtGui.QMessageBox.Cancel:

            #                     pass

        else:

            self.timer_camera.start(30)

 

    @pyqtSlot()

    def on_actionquit_triggered(self):

        """

        Slot documentation goes here.

        """

 

        self.stopVideo()

        print("on_stop_clicked")

        sys.exit(0)

 

    @pyqtSlot()

    def on_actionstart_triggered(self):

        """

        Slot documentation goes here.

        """

        print("下面开始预览")

        self.startVideo(1920, 1080)

 

    @pyqtSlot()

    def on_actionsnap_triggered(self):

        """

        Slot documentation goes here.

        """    

        ret, frame = self.cap.read()

        cv2.imwrite("image/fangjian5.jpeg", frame)

    

    def show_camera(self):

        flag, self.image = self.cap.read()

        # face = self.face_detect.align(self.image)

        # if face:

        #     pass

        #cv2.imwrite("image/fangjian3.jpg", self.image )

 

        show = cv2.resize(self.image, (960,720))

        show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)

        showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)

        self.label.setPixmap(QtGui.QPixmap.fromImage(showImage))
 

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)

    windows = MyWindow()

    windows.show()

    sys.exit(app.exec_())


为什么继续提高分辨率就出错了呢?

OpenCV Error: Assertion failed (ssize.area() > 0) in cv::resize, file ..\..\..\modules\imgproc\src\imgwarp.cpp, line 3229

这个错误貌似很难解决的,有没有大神指点一下?好像是再高一些的分辨率就溢出了?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值