【发布时间】:2019-02-05 00:13:58
【问题描述】:
在下面的代码中,我在QLabel 中显示了电脑的网络摄像头。
但是,当尝试使用self.boton1 按钮拍照时,他没有拍照。
Self.boton1 连接到def take ()函数,这是我用来拍照的函数。
但它不起作用我希望你能帮助我:
尝试将self.boton1.clicked.connect (self.take (self.capture)) 放在setup_camera () 函数中,以将捕获的数据作为参数传递给take () 函数self. capture,但它不起作用
from PyQt5.QtWidgets import QMainWindow,QApplication
import cv2
from PyQt5 import QtCore
import numpy as np
from PyQt5 import QtGui
from PyQt5 import uic
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
uic.loadUi("f.ui",self)
self.boton1.clicked.connect(self.take)
self.video_size = QtCore.QSize(320,240)
self.setup_camera()
uic.loadUi("f.ui",self)
def setup_camera(self):
self.capture = cv2.VideoCapture(0)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,self.video_size.height())
#self.Bfoto.clicked.connect(lambda:self.take(self.capture))
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.display_video_stream)
self.timer.start(30)
def display_video_stream(self):
_,frame =self.capture.read()
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
frame = cv2.flip(frame,1)
image = QtGui.QImage(frame,frame.shape[1],frame.shape[0],frame.strides[0],QtGui.QImage.Format_RGB888)
self.label.setPixmap(QtGui.QPixmap.fromImage(image))
def take(self):
print("value")
cap = videoCapture(0)
leido,frame = cap.read()
if leido ==True:
cv2.imwrite("photo.png",frame)
print("ok")
else:
print("error")
cap.release()
app = QApplication([])
m = Main()
m.show()
app.exec_()
f.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>671</width>
<height>519</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="boton1">
<property name="geometry">
<rect>
<x>530</x>
<y>400</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>foto</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>100</x>
<y>30</y>
<width>481</width>
<height>311</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>671</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
希望通过按下self.boton按钮,触发def take ()功能并拍照
【问题讨论】:
标签: python python-3.x opencv