在下面的代码我显示了在QLabel
PC的网络摄像头。然而,试图采取与self.boton1
按钮的图片时,他并没有拍摄照片。 Self.boton1
连接到thedef take ()
function这是我使用拍照功能。
但它不工作,我希望你能帮助我:
试着将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。头生
<?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
,thedef take ()
function被触发并拍照
问题
目前主要有两个问题,您的Python代码。 1.要装载f.ui在初始化两次()块
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)
因此,在第二uic.loadUi()非常以前初始化得到消灭,为什么你的按钮单击事件不工作,多数民众赞成。
2.
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()
在该块中,你可以使用以前的self.capture对象,这样你只能一个对象时,它变得非常简单的工作。而且因为当你从这个程序,你可以不用cap.release(退出这个对象应该只发布)在这里
解
self.boton1.clicked.connect(self.take)
self.video_size = QtCore.QSize(320,240)
self.setup_camera()
#uic.loadUi("f.ui",self)
if leido ==True:
cv2.imwrite("photo.png",frame)
print("ok")
else:
print("error")
cap.release()