在以下代码中,我在__test__
中显示了PC的网络摄像头。
但是,当尝试使用import React, { PureComponent } from "react";
import { Button } from "antd";
export default class NotFound extends PureComponent {
handlePageBack = () => this.props.history.push("/");
render = () => (
<div className="notfound">
<h1>404 - Not Found!</h1>
<Button type="default" onClick={this.handlePageBack}>
Go Back
</Button>
</div>
);
}
按钮拍照时,他不会拍照。
import React from "react";
import { shallowComponent } from "../../../tests/utils";
import NotFound from "../notfound";
const mockGoBack = jest.fn();
const initialProps = {
history: {
goBack: mockGoBack
}
};
/*
the shallowComponent function below is a custom function in "tests/utils/index.js" that
simplifies shallow mounting a component with props and state
*/
const wrapper = shallowComponent(<NotFound {...initialProps} />);
describe("Not Found", () => {
it("renders without errors", () => {
const notfoundComponent = wrapper.find("div.notfound");
expect(notfoundComponent).toHaveLength(1);
});
it("pushes back a page when Go Back button is clicked", () => {
wrapper.find("Button").simulate("click");
expect(mockGoBack).toHaveBeenCalled();
});
});
已连接到QLabel
功能,这是我用来拍照的功能。
但是它不起作用,希望您能对我有所帮助
尝试将self.boton1
放在Self.boton1
函数内部,以将捕获的数据def take ()
作为self.boton1.clicked.connect (self.take (self.capture))
函数的参数传递,但不起作用
setup_camera ()
f.ui
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_()
功能并拍照
答案 0 :(得分:0)
问题
您的python代码主要有两个问题。 1.您要在init()块中两次加载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()
解决方案
从代码中删除第二条uic.loadui()行
def init (自己): QMainWindow。初始化(个体) 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)
take()块应该像这样
print(“ value”) cap = self.capture leido,frame = cap.read()
if leido ==True:
cv2.imwrite("photo.png",frame)
print("ok")
else:
print("error")
cap.release()