繁体   English   中英

如何使用opencv拍照并同时可视化网络摄像头

[英]how to take a picture with opencv and visualize the webcam at the same time

在以下代码中,我在QLabel显示了PC的网络摄像头。 但是,当尝试使用self.boton1按钮拍照时,他不会拍照。 Self.boton1连接到def take ()函数,该函数是我用来拍照的函数。

但这不起作用,希望您能为我提供帮助:

尝试将self.boton1.clicked.connect (self.take (self.capture))放在setup_camera ()函数内,以将捕获self. capture的数据作为参数传递给take ()函数self. capture 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_()

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代码主要有两个问题。 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()

  1. 从代码中删除第二条uic.loadui()行

    def init (个体):QMainWindow。 初始化 (自己)uic.loadUi(“ f.ui”,自己)

     self.boton1.clicked.connect(self.take) self.video_size = QtCore.QSize(320,240) self.setup_camera() #uic.loadUi("f.ui",self) 
  2. 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() 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM